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,
|
2022-04-26 10:11:17 +00:00
|
|
|
Tab,
|
|
|
|
TabTitleText,
|
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";
|
2022-04-21 15:03:48 +00:00
|
|
|
import type { IRowData } from "@patternfly/react-table";
|
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";
|
2022-02-02 11:44:52 +00:00
|
|
|
import helpUrls from "../help-urls";
|
|
|
|
import { KeycloakSpinner } from "../components/keycloak-spinner/KeycloakSpinner";
|
2022-04-26 10:11:17 +00:00
|
|
|
import { PermissionsTab } from "../components/permission-tab/PermissionTab";
|
|
|
|
import { toUsers, UserTab } from "./routes/Users";
|
|
|
|
import {
|
|
|
|
routableTab,
|
|
|
|
RoutableTabs,
|
|
|
|
} from "../components/routable-tabs/RoutableTabs";
|
2022-06-13 09:07:53 +00:00
|
|
|
import { useAccess } from "../context/access/Access";
|
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");
|
2022-07-14 13:02:28 +00:00
|
|
|
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();
|
2022-02-02 11:44:52 +00:00
|
|
|
const [userStorage, setUserStorage] = useState<ComponentRepresentation[]>();
|
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
|
|
|
|
2022-04-28 18:55:06 +00:00
|
|
|
const [key, setKey] = useState(0);
|
2022-03-15 09:44:46 +00:00
|
|
|
const refresh = () => setKey(key + 1);
|
2020-12-09 21:55:17 +00:00
|
|
|
|
2022-06-13 09:07:53 +00:00
|
|
|
const { hasAccess } = useAccess();
|
|
|
|
const isManager = hasAccess("manage-users");
|
|
|
|
|
2021-04-29 06:28:59 +00:00
|
|
|
useFetch(
|
2022-02-02 11:44:52 +00:00
|
|
|
async () => {
|
2021-04-29 06:28:59 +00:00
|
|
|
const testParams = {
|
|
|
|
type: "org.keycloak.storage.UserStorageProvider",
|
|
|
|
};
|
2021-02-14 18:29:43 +00:00
|
|
|
|
2022-02-02 11:44:52 +00:00
|
|
|
try {
|
|
|
|
return await Promise.all([
|
|
|
|
adminClient.components.find(testParams),
|
|
|
|
adminClient.realms.findOne({ realm: realmName }),
|
|
|
|
]);
|
|
|
|
} catch {
|
2022-04-28 18:55:06 +00:00
|
|
|
return [[{}], undefined] as [
|
2022-02-02 11:44:52 +00:00
|
|
|
ComponentRepresentation[],
|
|
|
|
RealmRepresentation | undefined
|
|
|
|
];
|
|
|
|
}
|
2021-04-29 06:28:59 +00:00
|
|
|
},
|
2021-09-02 20:16:32 +00:00
|
|
|
([storageProviders, realm]) => {
|
2022-02-02 11:44:52 +00:00
|
|
|
setUserStorage(storageProviders);
|
2021-09-02 20:16:32 +00:00
|
|
|
setRealm(realm);
|
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 {
|
2022-04-21 15:03:48 +00:00
|
|
|
const users = await adminClient.users.find({
|
|
|
|
briefRepresentation: true,
|
|
|
|
...params,
|
|
|
|
});
|
2021-02-14 18:29:43 +00:00
|
|
|
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) {
|
2022-03-15 09:44:46 +00:00
|
|
|
if (userStorage?.length) {
|
|
|
|
addError("users:noUsersFoundErrorStorage", error);
|
|
|
|
} else {
|
|
|
|
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 }));
|
|
|
|
|
2022-02-02 11:44:52 +00:00
|
|
|
if (!userStorage) {
|
|
|
|
return <KeycloakSpinner />;
|
|
|
|
}
|
|
|
|
|
|
|
|
//should *only* list users when no user federation is configured
|
|
|
|
const listUsers = !(userStorage.length > 0);
|
|
|
|
|
2021-10-27 09:19:13 +00:00
|
|
|
const toolbar = (
|
|
|
|
<>
|
|
|
|
<ToolbarItem>
|
|
|
|
<Button data-testid="add-user" onClick={goToCreate}>
|
|
|
|
{t("addUser")}
|
|
|
|
</Button>
|
|
|
|
</ToolbarItem>
|
|
|
|
{!realm?.bruteForceProtected ? (
|
|
|
|
<ToolbarItem>
|
|
|
|
<Button
|
|
|
|
variant={ButtonVariant.plain}
|
|
|
|
onClick={toggleDeleteDialog}
|
2022-02-28 17:48:52 +00:00
|
|
|
data-testid="delete-user-btn"
|
2021-10-27 09:19:13 +00:00
|
|
|
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
|
|
|
|
2022-04-26 10:11:17 +00:00
|
|
|
const route = (tab: UserTab) =>
|
|
|
|
routableTab({
|
|
|
|
to: toUsers({
|
|
|
|
realm: realmName,
|
|
|
|
tab,
|
|
|
|
}),
|
|
|
|
history,
|
|
|
|
});
|
|
|
|
|
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-12-21 15:32:53 +00:00
|
|
|
<ViewHeader
|
|
|
|
titleKey="users:title"
|
|
|
|
subKey="users:usersExplain"
|
|
|
|
helpUrl={helpUrls.usersUrl}
|
2022-04-26 10:11:17 +00:00
|
|
|
divider={false}
|
2021-12-21 15:32:53 +00:00
|
|
|
/>
|
2021-03-31 13:16:58 +00:00
|
|
|
<PageSection
|
|
|
|
data-testid="users-page"
|
|
|
|
variant="light"
|
|
|
|
className="pf-u-p-0"
|
|
|
|
>
|
2022-04-26 10:11:17 +00:00
|
|
|
<RoutableTabs
|
|
|
|
data-testid="user-tabs"
|
|
|
|
defaultLocation={toUsers({
|
|
|
|
realm: realmName,
|
|
|
|
tab: "list",
|
|
|
|
})}
|
|
|
|
isBox
|
|
|
|
mountOnEnter
|
|
|
|
>
|
|
|
|
<Tab
|
|
|
|
id="list"
|
|
|
|
data-testid="listTab"
|
|
|
|
title={<TabTitleText>{t("userList")}</TabTitleText>}
|
|
|
|
{...route("list")}
|
|
|
|
>
|
|
|
|
<KeycloakDataTable
|
|
|
|
key={key}
|
|
|
|
loader={loader}
|
|
|
|
isPaginated
|
|
|
|
ariaLabelKey="users:title"
|
|
|
|
searchPlaceholderKey="users:searchForUser"
|
|
|
|
canSelectAll
|
|
|
|
onSelect={(rows) => setSelectedRows([...rows])}
|
|
|
|
emptyState={
|
|
|
|
!listUsers ? (
|
|
|
|
<>
|
|
|
|
<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>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<ListEmptyState
|
|
|
|
message={t("noUsersFound")}
|
|
|
|
instructions={t("emptyInstructions")}
|
|
|
|
primaryActionText={t("createNewUser")}
|
|
|
|
onPrimaryAction={goToCreate}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
2022-06-13 09:07:53 +00:00
|
|
|
toolbarItem={isManager ? toolbar : undefined}
|
2022-04-26 10:11:17 +00:00
|
|
|
actionResolver={(rowData: IRowData) => {
|
|
|
|
const user: UserRepresentation = rowData.data;
|
|
|
|
if (!user.access?.manage) return [];
|
2022-04-21 15:03:48 +00:00
|
|
|
|
2022-04-26 10:11:17 +00:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
title: t("common:delete"),
|
|
|
|
onClick: () => {
|
|
|
|
setSelectedRows([user]);
|
|
|
|
toggleDeleteDialog();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}}
|
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
name: "username",
|
|
|
|
displayKey: "users:username",
|
|
|
|
cellRenderer: UserDetailLink,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "email",
|
|
|
|
displayKey: "users:email",
|
|
|
|
cellRenderer: ValidatedEmail,
|
2022-04-21 15:03:48 +00:00
|
|
|
},
|
2022-04-26 10:11:17 +00:00
|
|
|
{
|
|
|
|
name: "lastName",
|
|
|
|
displayKey: "users:lastName",
|
|
|
|
cellFormatters: [emptyFormatter()],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "firstName",
|
|
|
|
displayKey: "users:firstName",
|
|
|
|
cellFormatters: [emptyFormatter()],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "status",
|
|
|
|
displayKey: "users:status",
|
|
|
|
cellRenderer: StatusRow,
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</Tab>
|
|
|
|
<Tab
|
|
|
|
id="permissions"
|
|
|
|
data-testid="permissionsTab"
|
|
|
|
title={<TabTitleText>{t("common:permissions")}</TabTitleText>}
|
|
|
|
{...route("permissions")}
|
|
|
|
>
|
|
|
|
<PermissionsTab type="users" />
|
|
|
|
</Tab>
|
|
|
|
</RoutableTabs>
|
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
|
|
|
}
|