2022-08-03 12:12:07 +00:00
|
|
|
import { useState } from "react";
|
2022-08-22 15:51:19 +00:00
|
|
|
import { Link } from "react-router-dom-v5-compat";
|
2020-11-24 20:11:13 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import {
|
2020-12-04 21:08:11 +00:00
|
|
|
AlertVariant,
|
2020-11-24 20:11:13 +00:00
|
|
|
Button,
|
2022-07-27 15:11:00 +00:00
|
|
|
ButtonVariant,
|
2020-11-24 20:11:13 +00:00
|
|
|
Dropdown,
|
|
|
|
DropdownItem,
|
2020-12-07 13:42:08 +00:00
|
|
|
KebabToggle,
|
|
|
|
ToolbarItem,
|
2020-11-24 20:11:13 +00:00
|
|
|
} from "@patternfly/react-core";
|
2021-08-26 08:39:35 +00:00
|
|
|
import type ClientScopeRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientScopeRepresentation";
|
2020-11-24 20:11:13 +00:00
|
|
|
|
2021-03-19 12:43:32 +00:00
|
|
|
import { useAdminClient } from "../../context/auth/AdminClient";
|
2020-11-24 20:11:13 +00:00
|
|
|
import { ListEmptyState } from "../../components/list-empty-state/ListEmptyState";
|
|
|
|
import { AddScopeDialog } from "./AddScopeDialog";
|
|
|
|
import {
|
|
|
|
ClientScope,
|
2021-04-06 07:29:11 +00:00
|
|
|
CellDropdown,
|
2021-09-14 15:48:48 +00:00
|
|
|
AllClientScopes,
|
|
|
|
AllClientScopeType,
|
|
|
|
changeClientScope,
|
|
|
|
addClientScope,
|
|
|
|
removeClientScope,
|
2021-04-06 07:29:11 +00:00
|
|
|
} from "../../components/client-scope/ClientScopeTypes";
|
2020-12-04 21:08:11 +00:00
|
|
|
import { useAlerts } from "../../components/alert/Alerts";
|
2021-03-19 12:43:32 +00:00
|
|
|
import { KeycloakDataTable } from "../../components/table-toolbar/KeycloakDataTable";
|
2021-09-14 15:48:48 +00:00
|
|
|
import {
|
|
|
|
nameFilter,
|
|
|
|
SearchDropdown,
|
|
|
|
SearchToolbar,
|
|
|
|
SearchType,
|
|
|
|
typeFilter,
|
|
|
|
} from "../../client-scopes/details/SearchFilter";
|
2022-02-21 12:14:20 +00:00
|
|
|
import { ChangeTypeDropdown } from "../../client-scopes/ChangeTypeDropdown";
|
2020-11-24 20:11:13 +00:00
|
|
|
|
2022-02-21 12:14:20 +00:00
|
|
|
import { toDedicatedScope } from "../routes/DedicatedScopeDetails";
|
|
|
|
import { useRealm } from "../../context/realm-context/RealmContext";
|
2022-05-09 10:44:07 +00:00
|
|
|
import useLocaleSort, { mapByKey } from "../../utils/useLocaleSort";
|
2022-07-27 15:11:00 +00:00
|
|
|
import { useAccess } from "../../context/access/Access";
|
|
|
|
import { useConfirmDialog } from "../../components/confirm-dialog/ConfirmDialog";
|
2022-03-14 10:17:03 +00:00
|
|
|
|
|
|
|
import "./client-scopes.css";
|
2021-04-07 05:42:30 +00:00
|
|
|
|
2020-11-24 20:11:13 +00:00
|
|
|
export type ClientScopesProps = {
|
|
|
|
clientId: string;
|
|
|
|
protocol: string;
|
2021-11-23 14:59:04 +00:00
|
|
|
clientName: string;
|
2022-05-30 09:23:24 +00:00
|
|
|
fineGrainedAccess?: boolean;
|
2020-11-24 20:11:13 +00:00
|
|
|
};
|
|
|
|
|
2021-09-14 15:48:48 +00:00
|
|
|
export type Row = ClientScopeRepresentation & {
|
|
|
|
type: AllClientScopeType;
|
|
|
|
description?: string;
|
2020-11-24 20:11:13 +00:00
|
|
|
};
|
|
|
|
|
2022-02-21 12:14:20 +00:00
|
|
|
const DEDICATED_ROW = "dedicated";
|
|
|
|
|
2021-11-23 14:59:04 +00:00
|
|
|
export const ClientScopes = ({
|
|
|
|
clientId,
|
|
|
|
protocol,
|
|
|
|
clientName,
|
2022-05-30 09:23:24 +00:00
|
|
|
fineGrainedAccess,
|
2021-11-23 14:59:04 +00:00
|
|
|
}: ClientScopesProps) => {
|
2020-11-24 20:11:13 +00:00
|
|
|
const { t } = useTranslation("clients");
|
2022-07-14 13:02:28 +00:00
|
|
|
const { adminClient } = useAdminClient();
|
2021-07-28 12:01:42 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
2022-02-21 12:14:20 +00:00
|
|
|
const { realm } = useRealm();
|
2022-05-09 10:44:07 +00:00
|
|
|
const localeSort = useLocaleSort();
|
2020-12-04 21:08:11 +00:00
|
|
|
|
2021-09-14 15:48:48 +00:00
|
|
|
const [searchType, setSearchType] = useState<SearchType>("name");
|
|
|
|
|
|
|
|
const [searchTypeType, setSearchTypeType] = useState<AllClientScopes>(
|
|
|
|
AllClientScopes.none
|
|
|
|
);
|
|
|
|
|
2020-11-24 20:11:13 +00:00
|
|
|
const [addDialogOpen, setAddDialogOpen] = useState(false);
|
|
|
|
|
|
|
|
const [rest, setRest] = useState<ClientScopeRepresentation[]>();
|
2022-10-03 10:47:50 +00:00
|
|
|
const [selectedRows, setSelectedRowState] = useState<Row[]>([]);
|
|
|
|
const setSelectedRows = (rows: Row[]) =>
|
|
|
|
setSelectedRowState(rows.filter(({ id }) => id !== DEDICATED_ROW));
|
2020-11-24 20:11:13 +00:00
|
|
|
|
2021-01-05 13:39:27 +00:00
|
|
|
const [key, setKey] = useState(0);
|
2022-02-21 12:14:20 +00:00
|
|
|
const refresh = () => setKey(key + 1);
|
|
|
|
const isDedicatedRow = (value: Row) => value.id === DEDICATED_ROW;
|
2020-11-24 20:11:13 +00:00
|
|
|
|
2022-05-17 07:52:19 +00:00
|
|
|
const { hasAccess } = useAccess();
|
2022-05-30 09:23:24 +00:00
|
|
|
const isManager = hasAccess("manage-clients") || fineGrainedAccess;
|
2022-05-17 07:52:19 +00:00
|
|
|
|
2021-09-14 15:48:48 +00:00
|
|
|
const loader = async (first?: number, max?: number, search?: string) => {
|
2021-07-05 11:24:10 +00:00
|
|
|
const defaultClientScopes =
|
|
|
|
await adminClient.clients.listDefaultClientScopes({ id: clientId });
|
|
|
|
const optionalClientScopes =
|
|
|
|
await adminClient.clients.listOptionalClientScopes({ id: clientId });
|
2021-03-19 12:43:32 +00:00
|
|
|
const clientScopes = await adminClient.clientScopes.find();
|
2020-11-24 20:11:13 +00:00
|
|
|
|
2021-03-19 12:43:32 +00:00
|
|
|
const find = (id: string) =>
|
2022-08-05 09:16:10 +00:00
|
|
|
clientScopes.find((clientScope) => id === clientScope.id);
|
2020-11-24 20:11:13 +00:00
|
|
|
|
2021-03-19 12:43:32 +00:00
|
|
|
const optional = optionalClientScopes.map((c) => {
|
|
|
|
const scope = find(c.id!);
|
2021-09-14 15:48:48 +00:00
|
|
|
const row: Row = {
|
2021-03-19 12:43:32 +00:00
|
|
|
...c,
|
|
|
|
type: ClientScope.optional,
|
2022-08-05 09:16:10 +00:00
|
|
|
description: scope?.description,
|
2021-09-14 15:48:48 +00:00
|
|
|
};
|
|
|
|
return row;
|
2021-03-19 12:43:32 +00:00
|
|
|
});
|
2020-11-24 20:11:13 +00:00
|
|
|
|
2021-03-19 12:43:32 +00:00
|
|
|
const defaultScopes = defaultClientScopes.map((c) => {
|
|
|
|
const scope = find(c.id!);
|
2021-09-14 15:48:48 +00:00
|
|
|
const row: Row = {
|
2021-03-19 12:43:32 +00:00
|
|
|
...c,
|
|
|
|
type: ClientScope.default,
|
2022-08-05 09:16:10 +00:00
|
|
|
description: scope?.description,
|
2021-09-14 15:48:48 +00:00
|
|
|
};
|
|
|
|
return row;
|
2021-03-19 12:43:32 +00:00
|
|
|
});
|
2020-11-24 20:11:13 +00:00
|
|
|
|
2021-03-19 12:43:32 +00:00
|
|
|
const rows = [...optional, ...defaultScopes];
|
|
|
|
const names = rows.map((row) => row.name);
|
|
|
|
setRest(
|
|
|
|
clientScopes
|
|
|
|
.filter((scope) => !names.includes(scope.name))
|
|
|
|
.filter((scope) => scope.protocol === protocol)
|
2021-01-05 13:39:27 +00:00
|
|
|
);
|
2020-12-04 21:08:11 +00:00
|
|
|
|
2021-09-14 15:48:48 +00:00
|
|
|
const filter =
|
|
|
|
searchType === "name" ? nameFilter(search) : typeFilter(searchTypeType);
|
2022-02-21 12:14:20 +00:00
|
|
|
const firstNum = Number(first);
|
2022-05-09 10:44:07 +00:00
|
|
|
const page = localeSort(rows.filter(filter), mapByKey("name")).slice(
|
|
|
|
firstNum,
|
|
|
|
firstNum + Number(max)
|
|
|
|
);
|
2022-05-30 09:23:24 +00:00
|
|
|
if (firstNum === 0 && isManager) {
|
2022-02-21 12:14:20 +00:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
id: DEDICATED_ROW,
|
|
|
|
name: t("dedicatedScopeName", { clientName }),
|
|
|
|
type: AllClientScopes.none,
|
|
|
|
description: t("dedicatedScopeDescription"),
|
|
|
|
},
|
|
|
|
...page,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
return page;
|
2021-03-19 12:43:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const TypeSelector = (scope: Row) => (
|
2021-08-26 12:15:28 +00:00
|
|
|
<CellDropdown
|
2022-05-17 07:52:19 +00:00
|
|
|
isDisabled={isDedicatedRow(scope) || !isManager}
|
2021-08-26 12:15:28 +00:00
|
|
|
clientScope={scope}
|
|
|
|
type={scope.type}
|
|
|
|
onSelect={async (value) => {
|
|
|
|
try {
|
2021-09-14 15:48:48 +00:00
|
|
|
await changeClientScope(
|
2021-08-26 12:15:28 +00:00
|
|
|
adminClient,
|
|
|
|
clientId,
|
|
|
|
scope,
|
|
|
|
scope.type,
|
|
|
|
value as ClientScope
|
|
|
|
);
|
|
|
|
addAlert(t("clientScopeSuccess"), AlertVariant.success);
|
|
|
|
refresh();
|
|
|
|
} catch (error) {
|
|
|
|
addError("clients:clientScopeError", error);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
2021-03-19 12:43:32 +00:00
|
|
|
);
|
2020-11-24 20:11:13 +00:00
|
|
|
|
2022-07-27 15:11:00 +00:00
|
|
|
const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({
|
|
|
|
titleKey: t("client-scopes:deleteClientScope", {
|
|
|
|
count: selectedRows.length,
|
|
|
|
name: selectedRows[0]?.name,
|
|
|
|
}),
|
|
|
|
messageKey: "client-scopes:deleteConfirm",
|
|
|
|
continueButtonLabel: "common:delete",
|
|
|
|
continueButtonVariant: ButtonVariant.danger,
|
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
|
|
|
await removeClientScope(
|
|
|
|
adminClient,
|
|
|
|
clientId,
|
|
|
|
selectedRows[0],
|
|
|
|
selectedRows[0].type as ClientScope
|
|
|
|
);
|
|
|
|
addAlert(t("clientScopeRemoveSuccess"), AlertVariant.success);
|
|
|
|
refresh();
|
|
|
|
} catch (error) {
|
|
|
|
addError("clients:clientScopeRemoveError", error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-05-17 07:52:19 +00:00
|
|
|
const ManagerToolbarItems = () => {
|
2022-08-08 10:06:14 +00:00
|
|
|
const [kebabOpen, setKebabOpen] = useState(false);
|
|
|
|
|
2022-05-17 07:52:19 +00:00
|
|
|
if (!isManager) return <span />;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-07-27 15:11:00 +00:00
|
|
|
<DeleteConfirm />
|
2022-05-17 07:52:19 +00:00
|
|
|
<ToolbarItem>
|
|
|
|
<Button onClick={() => setAddDialogOpen(true)}>
|
|
|
|
{t("addClientScope")}
|
|
|
|
</Button>
|
|
|
|
</ToolbarItem>
|
|
|
|
<ToolbarItem>
|
|
|
|
<ChangeTypeDropdown
|
|
|
|
clientId={clientId}
|
|
|
|
selectedRows={selectedRows}
|
|
|
|
refresh={refresh}
|
|
|
|
/>
|
|
|
|
</ToolbarItem>
|
|
|
|
<ToolbarItem>
|
|
|
|
<Dropdown
|
|
|
|
toggle={<KebabToggle onToggle={() => setKebabOpen(!kebabOpen)} />}
|
|
|
|
isOpen={kebabOpen}
|
|
|
|
isPlain
|
|
|
|
dropdownItems={[
|
|
|
|
<DropdownItem
|
|
|
|
key="deleteAll"
|
|
|
|
isDisabled={selectedRows.length === 0}
|
|
|
|
onClick={async () => {
|
|
|
|
try {
|
|
|
|
await Promise.all(
|
2022-10-03 10:47:50 +00:00
|
|
|
selectedRows.map((row) =>
|
|
|
|
removeClientScope(
|
2022-05-17 07:52:19 +00:00
|
|
|
adminClient,
|
|
|
|
clientId,
|
|
|
|
{ ...row },
|
|
|
|
row.type as ClientScope
|
2022-10-03 10:47:50 +00:00
|
|
|
)
|
|
|
|
)
|
2022-05-17 07:52:19 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
setKebabOpen(false);
|
2022-08-08 10:06:14 +00:00
|
|
|
setSelectedRows([]);
|
2022-10-03 10:47:50 +00:00
|
|
|
addAlert(t("clients:clientScopeRemoveSuccess"));
|
2022-05-17 07:52:19 +00:00
|
|
|
refresh();
|
|
|
|
} catch (error) {
|
|
|
|
addError("clients:clientScopeRemoveError", error);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t("common:remove")}
|
|
|
|
</DropdownItem>,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</ToolbarItem>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-11-24 20:11:13 +00:00
|
|
|
return (
|
|
|
|
<>
|
2020-12-04 21:08:11 +00:00
|
|
|
{rest && (
|
|
|
|
<AddScopeDialog
|
|
|
|
clientScopes={rest}
|
2021-11-23 14:59:04 +00:00
|
|
|
clientName={clientName!}
|
2020-12-04 21:08:11 +00:00
|
|
|
open={addDialogOpen}
|
|
|
|
toggleDialog={() => setAddDialogOpen(!addDialogOpen)}
|
|
|
|
onAdd={async (scopes) => {
|
|
|
|
try {
|
|
|
|
await Promise.all(
|
|
|
|
scopes.map(
|
|
|
|
async (scope) =>
|
2021-09-14 15:48:48 +00:00
|
|
|
await addClientScope(
|
2020-12-04 21:08:11 +00:00
|
|
|
adminClient,
|
|
|
|
clientId,
|
|
|
|
scope.scope,
|
2021-11-23 14:59:04 +00:00
|
|
|
scope.type!
|
2020-12-04 21:08:11 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
addAlert(t("clientScopeSuccess"), AlertVariant.success);
|
2021-01-05 13:39:27 +00:00
|
|
|
refresh();
|
2020-12-04 21:08:11 +00:00
|
|
|
} catch (error) {
|
2021-07-28 12:01:42 +00:00
|
|
|
addError("clients:clientScopeError", error);
|
2020-12-04 21:08:11 +00:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
|
2021-03-19 12:43:32 +00:00
|
|
|
<KeycloakDataTable
|
|
|
|
key={key}
|
|
|
|
loader={loader}
|
|
|
|
ariaLabelKey="clients:clientScopeList"
|
2021-09-14 15:48:48 +00:00
|
|
|
searchPlaceholderKey={
|
|
|
|
searchType === "name" ? "clients:searchByName" : undefined
|
|
|
|
}
|
2021-04-07 05:42:30 +00:00
|
|
|
canSelectAll
|
2021-09-14 15:48:48 +00:00
|
|
|
isPaginated
|
|
|
|
isSearching={searchType === "type"}
|
2021-03-19 12:43:32 +00:00
|
|
|
onSelect={(rows) => setSelectedRows([...rows])}
|
|
|
|
searchTypeComponent={
|
2021-09-14 15:48:48 +00:00
|
|
|
<SearchDropdown
|
|
|
|
searchType={searchType}
|
|
|
|
onSelect={(searchType) => setSearchType(searchType)}
|
2021-03-19 12:43:32 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
toolbarItem={
|
|
|
|
<>
|
2021-09-14 15:48:48 +00:00
|
|
|
<SearchToolbar
|
|
|
|
searchType={searchType}
|
|
|
|
type={searchTypeType}
|
|
|
|
onSelect={(searchType) => setSearchType(searchType)}
|
|
|
|
onType={(value) => {
|
|
|
|
setSearchTypeType(value);
|
|
|
|
refresh();
|
|
|
|
}}
|
|
|
|
/>
|
2022-05-17 07:52:19 +00:00
|
|
|
<ManagerToolbarItems />
|
2021-03-19 12:43:32 +00:00
|
|
|
</>
|
|
|
|
}
|
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
name: "name",
|
2021-04-07 05:42:30 +00:00
|
|
|
displayKey: "clients:assignedClientScope",
|
2022-02-21 12:14:20 +00:00
|
|
|
cellRenderer: (row) => {
|
|
|
|
if (isDedicatedRow(row)) {
|
|
|
|
return (
|
|
|
|
<Link to={toDedicatedScope({ realm, clientId })}>
|
|
|
|
{row.name}
|
|
|
|
</Link>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return row.name;
|
|
|
|
},
|
2021-03-19 12:43:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "type",
|
|
|
|
displayKey: "clients:assignedType",
|
|
|
|
cellRenderer: TypeSelector,
|
|
|
|
},
|
|
|
|
{ name: "description" },
|
|
|
|
]}
|
2022-05-17 07:52:19 +00:00
|
|
|
actions={
|
|
|
|
isManager
|
|
|
|
? [
|
|
|
|
{
|
|
|
|
title: t("common:remove"),
|
|
|
|
onRowClick: async (row) => {
|
2022-07-27 15:11:00 +00:00
|
|
|
setSelectedRows([row]);
|
|
|
|
toggleDeleteDialog();
|
2022-05-17 07:52:19 +00:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
: []
|
|
|
|
}
|
2021-03-19 12:43:32 +00:00
|
|
|
emptyState={
|
|
|
|
<ListEmptyState
|
|
|
|
message={t("clients:emptyClientScopes")}
|
|
|
|
instructions={t("clients:emptyClientScopesInstructions")}
|
|
|
|
primaryActionText={t("clients:emptyClientScopesPrimaryAction")}
|
|
|
|
onPrimaryAction={() => setAddDialogOpen(true)}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>
|
2020-11-24 20:11:13 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|