2020-12-09 21:55:17 +00:00
|
|
|
import {
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
ButtonVariant,
|
2021-09-02 20:16:32 +00:00
|
|
|
Dropdown,
|
|
|
|
DropdownItem,
|
2021-10-27 09:19:13 +00:00
|
|
|
EmptyState,
|
|
|
|
InputGroup,
|
2021-09-02 20:16:32 +00:00
|
|
|
KebabToggle,
|
2020-12-09 21:55:17 +00:00
|
|
|
Label,
|
|
|
|
PageSection,
|
2021-08-25 14:48:11 +00:00
|
|
|
Text,
|
|
|
|
TextContent,
|
2021-10-27 09:19:13 +00:00
|
|
|
TextInput,
|
|
|
|
Toolbar,
|
|
|
|
ToolbarContent,
|
2020-12-09 21:55:17 +00:00
|
|
|
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,
|
2021-10-27 09:19:13 +00:00
|
|
|
SearchIcon,
|
2021-02-14 18:29:43 +00:00
|
|
|
WarningTriangleIcon,
|
|
|
|
} from "@patternfly/react-icons";
|
2021-11-18 16:45:56 +00:00
|
|
|
import type RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation";
|
|
|
|
import type ComponentRepresentation from "@keycloak/keycloak-admin-client/lib/defs/componentRepresentation";
|
2021-08-26 08:39:35 +00:00
|
|
|
import type UserRepresentation from "@keycloak/keycloak-admin-client/lib/defs/userRepresentation";
|
2021-07-21 09:30:18 +00:00
|
|
|
import React, { useState } from "react";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2021-10-27 09:19:13 +00:00
|
|
|
import { Link, useHistory } from "react-router-dom";
|
2020-12-09 21:55:17 +00:00
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
2021-07-21 09:30:18 +00:00
|
|
|
import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog";
|
2021-02-14 18:29:43 +00:00
|
|
|
import { ListEmptyState } from "../components/list-empty-state/ListEmptyState";
|
2021-07-21 09:30:18 +00:00
|
|
|
import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable";
|
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
|
|
|
import { useAdminClient, useFetch } from "../context/auth/AdminClient";
|
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
2021-02-14 18:29:43 +00:00
|
|
|
import { emptyFormatter } from "../util";
|
2021-08-10 13:07:59 +00:00
|
|
|
import { toUser } from "./routes/User";
|
2021-10-27 09:19:13 +00:00
|
|
|
import { toAddUser } from "./routes/AddUser";
|
2020-12-16 06:58:00 +00:00
|
|
|
|
2021-11-18 16:45:56 +00:00
|
|
|
import "./user-section.css";
|
|
|
|
|
2020-12-16 06:58:00 +00:00
|
|
|
type BruteUser = UserRepresentation & {
|
|
|
|
brute?: Record<string, object>;
|
|
|
|
};
|
2020-09-09 09:07:17 +00:00
|
|
|
|
2021-10-29 16:11:06 +00:00
|
|
|
export default function UsersSection() {
|
2020-12-09 21:55:17 +00:00
|
|
|
const { t } = useTranslation("users");
|
|
|
|
const adminClient = useAdminClient();
|
2021-07-28 12:01:42 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
2021-07-21 09:30:18 +00:00
|
|
|
const { realm: realmName } = useRealm();
|
2021-03-03 13:53:42 +00:00
|
|
|
const history = useHistory();
|
2021-02-14 18:29:43 +00:00
|
|
|
const [listUsers, setListUsers] = useState(false);
|
2021-10-27 09:19:13 +00:00
|
|
|
const [searchUser, setSearchUser] = useState<string>();
|
2021-11-18 16:45:56 +00:00
|
|
|
const [realm, setRealm] = useState<RealmRepresentation | undefined>();
|
2021-09-02 20:16:32 +00:00
|
|
|
const [kebabOpen, setKebabOpen] = useState(false);
|
2021-02-14 18:29:43 +00:00
|
|
|
const [selectedRows, setSelectedRows] = useState<UserRepresentation[]>([]);
|
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-04-29 06:28:59 +00:00
|
|
|
useFetch(
|
|
|
|
() => {
|
|
|
|
const testParams = {
|
|
|
|
type: "org.keycloak.storage.UserStorageProvider",
|
|
|
|
};
|
2021-02-14 18:29:43 +00:00
|
|
|
|
2021-09-02 20:16:32 +00:00
|
|
|
return Promise.all([
|
|
|
|
adminClient.components.find(testParams),
|
|
|
|
adminClient.realms.findOne({ realm: realmName }),
|
2021-11-18 16:45:56 +00:00
|
|
|
]).catch(
|
|
|
|
() =>
|
|
|
|
[[], undefined] as [
|
|
|
|
ComponentRepresentation[],
|
|
|
|
RealmRepresentation | undefined
|
|
|
|
]
|
|
|
|
);
|
2021-04-29 06:28:59 +00:00
|
|
|
},
|
2021-09-02 20:16:32 +00:00
|
|
|
([storageProviders, realm]) => {
|
2021-08-25 14:48:11 +00:00
|
|
|
//should *only* list users when no user federation is configured
|
2021-09-02 20:16:32 +00:00
|
|
|
setListUsers(!(storageProviders.length > 0));
|
|
|
|
setRealm(realm);
|
2021-08-25 14:48:11 +00:00
|
|
|
refresh();
|
2021-04-29 06:28:59 +00:00
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
2021-02-14 18:29:43 +00:00
|
|
|
|
2021-03-11 20:23:08 +00:00
|
|
|
const UserDetailLink = (user: UserRepresentation) => (
|
2021-08-26 12:15:28 +00:00
|
|
|
<Link
|
|
|
|
key={user.username}
|
|
|
|
to={toUser({ realm: realmName, id: user.id!, tab: "settings" })}
|
|
|
|
>
|
|
|
|
{user.username}
|
|
|
|
</Link>
|
2021-03-11 20:23:08 +00:00
|
|
|
);
|
|
|
|
|
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-08-25 14:48:11 +00:00
|
|
|
|
2021-10-27 09:19:13 +00:00
|
|
|
const searchParam = search || searchUser || "";
|
2021-02-14 18:29:43 +00:00
|
|
|
if (searchParam) {
|
|
|
|
params.search = 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
|
|
|
}
|
2021-08-25 14:48:11 +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 });
|
|
|
|
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-07-28 12:01:42 +00:00
|
|
|
addError("users:noUsersFoundError", error);
|
2021-02-14 18:29:43 +00:00
|
|
|
return [];
|
2020-12-09 21:55:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-02 20:16:32 +00:00
|
|
|
const [toggleUnlockUsersDialog, UnlockUsersConfirm] = useConfirmDialog({
|
|
|
|
titleKey: "users:unlockAllUsers",
|
|
|
|
messageKey: "users:unlockUsersConfirm",
|
|
|
|
continueButtonLabel: "users:unlock",
|
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
|
|
|
await adminClient.attackDetection.delAll();
|
|
|
|
refresh();
|
|
|
|
addAlert(t("unlockUsersSuccess"), AlertVariant.success);
|
|
|
|
} catch (error) {
|
|
|
|
addError("users:unlockUsersError", error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
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) {
|
2021-07-28 12:01:42 +00:00
|
|
|
addError("users:userDeletedError", error);
|
2021-02-14 18:29:43 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
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-10-27 09:19:13 +00:00
|
|
|
const goToCreate = () => history.push(toAddUser({ realm: realmName }));
|
|
|
|
|
|
|
|
const toolbar = (
|
|
|
|
<>
|
|
|
|
<ToolbarItem>
|
|
|
|
<Button data-testid="add-user" onClick={goToCreate}>
|
|
|
|
{t("addUser")}
|
|
|
|
</Button>
|
|
|
|
</ToolbarItem>
|
|
|
|
{!realm?.bruteForceProtected ? (
|
|
|
|
<ToolbarItem>
|
|
|
|
<Button
|
|
|
|
variant={ButtonVariant.plain}
|
|
|
|
onClick={toggleDeleteDialog}
|
|
|
|
isDisabled={selectedRows.length === 0}
|
|
|
|
>
|
|
|
|
{t("deleteUser")}
|
|
|
|
</Button>
|
|
|
|
</ToolbarItem>
|
|
|
|
) : (
|
|
|
|
<ToolbarItem>
|
|
|
|
<Dropdown
|
|
|
|
toggle={<KebabToggle onToggle={(open) => setKebabOpen(open)} />}
|
|
|
|
isOpen={kebabOpen}
|
|
|
|
isPlain
|
|
|
|
dropdownItems={[
|
|
|
|
<DropdownItem
|
|
|
|
key="deleteUser"
|
|
|
|
component="button"
|
|
|
|
isDisabled={selectedRows.length === 0}
|
|
|
|
onClick={() => {
|
|
|
|
toggleDeleteDialog();
|
|
|
|
setKebabOpen(false);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t("deleteUser")}
|
|
|
|
</DropdownItem>,
|
|
|
|
|
|
|
|
<DropdownItem
|
|
|
|
key="unlock"
|
|
|
|
component="button"
|
|
|
|
onClick={() => {
|
|
|
|
toggleUnlockUsersDialog();
|
|
|
|
setKebabOpen(false);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t("unlockAllUsers")}
|
|
|
|
</DropdownItem>,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</ToolbarItem>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
2021-03-03 13:53:42 +00:00
|
|
|
|
2020-09-18 08:04:55 +00:00
|
|
|
return (
|
|
|
|
<>
|
2021-02-14 18:29:43 +00:00
|
|
|
<DeleteConfirm />
|
2021-09-02 20:16:32 +00:00
|
|
|
<UnlockUsersConfirm />
|
2021-04-21 13:18:45 +00:00
|
|
|
<ViewHeader titleKey="users:title" />
|
2021-03-31 13:16:58 +00:00
|
|
|
<PageSection
|
|
|
|
data-testid="users-page"
|
|
|
|
variant="light"
|
|
|
|
className="pf-u-p-0"
|
|
|
|
>
|
2021-08-25 14:48:11 +00:00
|
|
|
<KeycloakDataTable
|
|
|
|
key={key}
|
|
|
|
loader={loader}
|
|
|
|
isPaginated
|
|
|
|
ariaLabelKey="users:title"
|
|
|
|
searchPlaceholderKey="users:searchForUser"
|
|
|
|
canSelectAll
|
|
|
|
onSelect={(rows) => setSelectedRows([...rows])}
|
|
|
|
emptyState={
|
|
|
|
!listUsers ? (
|
2021-10-27 09:19:13 +00:00
|
|
|
<>
|
|
|
|
<Toolbar>
|
|
|
|
<ToolbarContent>
|
|
|
|
<ToolbarItem>
|
|
|
|
<InputGroup>
|
|
|
|
<TextInput
|
|
|
|
name="search-input"
|
|
|
|
type="search"
|
|
|
|
aria-label={t("search")}
|
|
|
|
placeholder={t("users:searchForUser")}
|
|
|
|
onChange={(value) => {
|
|
|
|
setSearchUser(value);
|
|
|
|
}}
|
|
|
|
onKeyDown={(e) => {
|
|
|
|
if (e.key === "Enter") {
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
variant={ButtonVariant.control}
|
|
|
|
aria-label={t("common:search")}
|
|
|
|
onClick={refresh}
|
|
|
|
>
|
|
|
|
<SearchIcon />
|
|
|
|
</Button>
|
|
|
|
</InputGroup>
|
|
|
|
</ToolbarItem>
|
|
|
|
{toolbar}
|
|
|
|
</ToolbarContent>
|
|
|
|
</Toolbar>
|
|
|
|
<EmptyState data-testid="empty-state" variant="large">
|
|
|
|
<TextContent className="kc-search-users-text">
|
|
|
|
<Text>{t("searchForUserDescription")}</Text>
|
|
|
|
</TextContent>
|
|
|
|
</EmptyState>
|
|
|
|
</>
|
2021-08-25 14:48:11 +00:00
|
|
|
) : (
|
|
|
|
<ListEmptyState
|
|
|
|
message={t("noUsersFound")}
|
|
|
|
instructions={t("emptyInstructions")}
|
|
|
|
primaryActionText={t("createNewUser")}
|
|
|
|
onPrimaryAction={goToCreate}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
2021-10-27 09:19:13 +00:00
|
|
|
toolbarItem={toolbar}
|
2021-08-25 14:48:11 +00:00
|
|
|
actions={[
|
|
|
|
{
|
|
|
|
title: t("common:delete"),
|
|
|
|
onRowClick: (user) => {
|
|
|
|
setSelectedRows([user]);
|
|
|
|
toggleDeleteDialog();
|
2021-02-14 18:29:43 +00:00
|
|
|
},
|
2021-08-25 14:48:11 +00:00
|
|
|
},
|
|
|
|
]}
|
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
name: "username",
|
|
|
|
displayKey: "users:username",
|
|
|
|
cellRenderer: UserDetailLink,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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
|
|
|
</>
|
|
|
|
);
|
2021-10-29 16:11:06 +00:00
|
|
|
}
|