2021-02-14 18:29:43 +00:00
|
|
|
import React, { useContext, useEffect, useState } from "react";
|
2021-02-23 08:52:40 +00:00
|
|
|
import { useErrorHandler } from "react-error-boundary";
|
2020-12-09 21:55:17 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import {
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
ButtonVariant,
|
|
|
|
Label,
|
|
|
|
PageSection,
|
|
|
|
ToolbarItem,
|
2021-02-14 18:29:43 +00:00
|
|
|
Tooltip,
|
2020-12-09 21:55:17 +00:00
|
|
|
} from "@patternfly/react-core";
|
2021-02-14 18:29:43 +00:00
|
|
|
import {
|
|
|
|
ExclamationCircleIcon,
|
|
|
|
InfoCircleIcon,
|
|
|
|
WarningTriangleIcon,
|
|
|
|
} from "@patternfly/react-icons";
|
2020-12-09 21:55:17 +00:00
|
|
|
import UserRepresentation from "keycloak-admin/lib/defs/userRepresentation";
|
|
|
|
|
2021-02-14 18:29:43 +00:00
|
|
|
import { asyncStateFetch, useAdminClient } from "../context/auth/AdminClient";
|
2020-12-09 21:55:17 +00:00
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
2020-12-11 10:28:38 +00:00
|
|
|
import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable";
|
2020-12-09 21:55:17 +00:00
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
2020-12-16 06:58:00 +00:00
|
|
|
import { RealmContext } from "../context/realm-context/RealmContext";
|
2021-02-14 18:29:43 +00:00
|
|
|
import { SearchUser } from "./SearchUser";
|
|
|
|
import { ListEmptyState } from "../components/list-empty-state/ListEmptyState";
|
|
|
|
import { emptyFormatter } from "../util";
|
|
|
|
import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog";
|
|
|
|
|
|
|
|
import "./user-section.css";
|
2021-03-11 20:23:08 +00:00
|
|
|
import { Link, useHistory, useRouteMatch } from "react-router-dom";
|
2020-12-16 06:58:00 +00:00
|
|
|
|
|
|
|
type BruteUser = UserRepresentation & {
|
|
|
|
brute?: Record<string, object>;
|
|
|
|
};
|
2020-09-09 09:07:17 +00:00
|
|
|
|
2020-09-10 18:04:03 +00:00
|
|
|
export const UsersSection = () => {
|
2020-12-09 21:55:17 +00:00
|
|
|
const { t } = useTranslation("users");
|
2021-02-23 08:52:40 +00:00
|
|
|
const handleError = useErrorHandler();
|
2020-12-09 21:55:17 +00:00
|
|
|
const adminClient = useAdminClient();
|
|
|
|
const { addAlert } = useAlerts();
|
2020-12-16 06:58:00 +00:00
|
|
|
const { realm: realmName } = useContext(RealmContext);
|
2021-03-03 13:53:42 +00:00
|
|
|
const history = useHistory();
|
|
|
|
const { url } = useRouteMatch();
|
2021-02-14 18:29:43 +00:00
|
|
|
const [listUsers, setListUsers] = useState(false);
|
|
|
|
const [initialSearch, setInitialSearch] = useState("");
|
|
|
|
const [selectedRows, setSelectedRows] = useState<UserRepresentation[]>([]);
|
2021-03-22 08:14:24 +00:00
|
|
|
const [search, setSearch] = useState("");
|
2020-12-16 06:58:00 +00:00
|
|
|
|
2020-12-09 21:55:17 +00:00
|
|
|
const [key, setKey] = useState("");
|
|
|
|
const refresh = () => setKey(`${new Date().getTime()}`);
|
|
|
|
|
2021-02-14 18:29:43 +00:00
|
|
|
useEffect(() => {
|
|
|
|
return asyncStateFetch(
|
|
|
|
() => {
|
|
|
|
const testParams = {
|
|
|
|
type: "org.keycloak.storage.UserStorageProvider",
|
|
|
|
};
|
|
|
|
|
|
|
|
return Promise.all([
|
|
|
|
adminClient.components.find(testParams),
|
|
|
|
adminClient.users.count(),
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
(response) => {
|
|
|
|
//should *only* list users when no user federation is configured and uses count > 100
|
|
|
|
setListUsers(
|
|
|
|
!((response[0] && response[0].length > 0) || response[1] > 100)
|
|
|
|
);
|
2021-02-23 08:52:40 +00:00
|
|
|
},
|
|
|
|
handleError
|
2021-02-14 18:29:43 +00:00
|
|
|
);
|
|
|
|
}, []);
|
|
|
|
|
2021-03-11 20:23:08 +00:00
|
|
|
const UserDetailLink = (user: UserRepresentation) => (
|
|
|
|
<>
|
2021-04-15 10:23:36 +00:00
|
|
|
<Link key={user.username} to={`${url}/${user.id}/settings`}>
|
2021-03-11 20:23:08 +00:00
|
|
|
{user.username}
|
|
|
|
</Link>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
|
2020-12-09 21:55:17 +00:00
|
|
|
const loader = async (first?: number, max?: number, search?: string) => {
|
|
|
|
const params: { [name: string]: string | number } = {
|
|
|
|
first: first!,
|
|
|
|
max: max!,
|
|
|
|
};
|
2021-02-14 18:29:43 +00:00
|
|
|
const searchParam = search || initialSearch || "";
|
|
|
|
if (searchParam) {
|
|
|
|
params.search = searchParam;
|
2021-03-22 08:14:24 +00:00
|
|
|
setSearch(searchParam);
|
2020-12-09 21:55:17 +00:00
|
|
|
}
|
2020-12-16 06:58:00 +00:00
|
|
|
|
2021-02-14 18:29:43 +00:00
|
|
|
if (!listUsers && !searchParam) {
|
|
|
|
return [];
|
2020-12-16 06:58:00 +00:00
|
|
|
}
|
2020-12-09 21:55:17 +00:00
|
|
|
try {
|
2021-02-14 18:29:43 +00:00
|
|
|
const users = await adminClient.users.find({ ...params });
|
|
|
|
const realm = await adminClient.realms.findOne({ realm: realmName });
|
|
|
|
if (realm?.bruteForceProtected) {
|
|
|
|
const brutes = await Promise.all(
|
|
|
|
users.map((user: BruteUser) =>
|
|
|
|
adminClient.attackDetection.findOne({
|
|
|
|
id: user.id!,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
|
|
|
for (let index = 0; index < users.length; index++) {
|
|
|
|
const user: BruteUser = users[index];
|
|
|
|
user.brute = brutes[index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return users;
|
2020-12-09 21:55:17 +00:00
|
|
|
} catch (error) {
|
2021-02-14 18:29:43 +00:00
|
|
|
addAlert(t("noUsersFoundError", { error }), AlertVariant.danger);
|
|
|
|
return [];
|
2020-12-09 21:55:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-14 18:29:43 +00:00
|
|
|
const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({
|
|
|
|
titleKey: "users:deleteConfirm",
|
|
|
|
messageKey: t("deleteConfirmDialog", { count: selectedRows.length }),
|
|
|
|
continueButtonLabel: "delete",
|
|
|
|
continueButtonVariant: ButtonVariant.danger,
|
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
|
|
|
for (const user of selectedRows) {
|
|
|
|
await adminClient.users.del({ id: user.id! });
|
|
|
|
}
|
|
|
|
setSelectedRows([]);
|
|
|
|
refresh();
|
|
|
|
addAlert(t("userDeletedSuccess"), AlertVariant.success);
|
|
|
|
} catch (error) {
|
|
|
|
addAlert(t("userDeletedError", { error }), AlertVariant.danger);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-12-16 06:58:00 +00:00
|
|
|
const StatusRow = (user: BruteUser) => {
|
2020-12-09 21:55:17 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{!user.enabled && (
|
2020-12-16 06:58:00 +00:00
|
|
|
<Label key={user.id} color="red" icon={<InfoCircleIcon />}>
|
2020-12-09 21:55:17 +00:00
|
|
|
{t("disabled")}
|
|
|
|
</Label>
|
|
|
|
)}
|
2020-12-16 06:58:00 +00:00
|
|
|
{user.brute?.disabled && (
|
|
|
|
<Label key={user.id} color="orange" icon={<WarningTriangleIcon />}>
|
|
|
|
{t("temporaryDisabled")}
|
|
|
|
</Label>
|
|
|
|
)}
|
2021-02-14 18:29:43 +00:00
|
|
|
{user.enabled && !user.brute?.disabled && "—"}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const ValidatedEmail = (user: UserRepresentation) => {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{!user.emailVerified && (
|
|
|
|
<Tooltip
|
|
|
|
key={`email-verified-${user.id}`}
|
|
|
|
content={<>{t("notVerified")}</>}
|
|
|
|
>
|
|
|
|
<ExclamationCircleIcon className="keycloak__user-section__email-verified" />
|
|
|
|
</Tooltip>
|
|
|
|
)}{" "}
|
|
|
|
{emptyFormatter()(user.email)}
|
2020-12-09 21:55:17 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-03-03 13:53:42 +00:00
|
|
|
const goToCreate = () => history.push(`${url}/add-user`);
|
|
|
|
|
2020-09-18 08:04:55 +00:00
|
|
|
return (
|
|
|
|
<>
|
2021-02-14 18:29:43 +00:00
|
|
|
<DeleteConfirm />
|
|
|
|
<ViewHeader titleKey="users:title" subKey="" />
|
2021-03-31 13:16:58 +00:00
|
|
|
<PageSection
|
|
|
|
data-testid="users-page"
|
|
|
|
variant="light"
|
|
|
|
className="pf-u-p-0"
|
|
|
|
>
|
2021-02-14 18:29:43 +00:00
|
|
|
{!listUsers && !initialSearch && (
|
|
|
|
<SearchUser
|
|
|
|
onSearch={(search) => {
|
|
|
|
setInitialSearch(search);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{(listUsers || initialSearch) && (
|
|
|
|
<KeycloakDataTable
|
|
|
|
key={key}
|
|
|
|
loader={loader}
|
|
|
|
isPaginated
|
|
|
|
ariaLabelKey="users:title"
|
|
|
|
searchPlaceholderKey="users:searchForUser"
|
|
|
|
canSelectAll
|
|
|
|
onSelect={(rows) => setSelectedRows([...rows])}
|
|
|
|
emptyState={
|
2021-03-22 08:14:24 +00:00
|
|
|
!search ? (
|
|
|
|
<ListEmptyState
|
|
|
|
message={t("noUsersFound")}
|
|
|
|
instructions={t("emptyInstructions")}
|
|
|
|
primaryActionText={t("createNewUser")}
|
|
|
|
onPrimaryAction={goToCreate}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
""
|
|
|
|
)
|
2021-02-14 18:29:43 +00:00
|
|
|
}
|
|
|
|
toolbarItem={
|
|
|
|
<>
|
|
|
|
<ToolbarItem>
|
2021-03-03 19:12:16 +00:00
|
|
|
<Button data-testid="add-user" onClick={goToCreate}>
|
|
|
|
{t("addUser")}
|
|
|
|
</Button>
|
2021-02-14 18:29:43 +00:00
|
|
|
</ToolbarItem>
|
|
|
|
<ToolbarItem>
|
|
|
|
<Button
|
|
|
|
variant={ButtonVariant.plain}
|
|
|
|
onClick={toggleDeleteDialog}
|
|
|
|
>
|
|
|
|
{t("deleteUser")}
|
|
|
|
</Button>
|
|
|
|
</ToolbarItem>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
actions={[
|
|
|
|
{
|
|
|
|
title: t("common:delete"),
|
|
|
|
onRowClick: (user) => {
|
|
|
|
setSelectedRows([user]);
|
|
|
|
toggleDeleteDialog();
|
|
|
|
},
|
2020-12-09 21:55:17 +00:00
|
|
|
},
|
2021-02-14 18:29:43 +00:00
|
|
|
]}
|
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
name: "username",
|
|
|
|
displayKey: "users:username",
|
2021-03-11 20:23:08 +00:00
|
|
|
cellRenderer: UserDetailLink,
|
2021-02-14 18:29:43 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "email",
|
|
|
|
displayKey: "users:email",
|
|
|
|
cellRenderer: ValidatedEmail,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "lastName",
|
|
|
|
displayKey: "users:lastName",
|
|
|
|
cellFormatters: [emptyFormatter()],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "firstName",
|
|
|
|
displayKey: "users:firstName",
|
|
|
|
cellFormatters: [emptyFormatter()],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "status",
|
|
|
|
displayKey: "users:status",
|
|
|
|
cellRenderer: StatusRow,
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
)}
|
2020-12-09 21:55:17 +00:00
|
|
|
</PageSection>
|
2020-09-18 08:04:55 +00:00
|
|
|
</>
|
|
|
|
);
|
2020-09-09 09:07:17 +00:00
|
|
|
};
|