2023-05-03 13:51:02 +00:00
|
|
|
import type PolicyRepresentation from "@keycloak/keycloak-admin-client/lib/defs/policyRepresentation";
|
|
|
|
import type ScopeRepresentation from "@keycloak/keycloak-admin-client/lib/defs/scopeRepresentation";
|
2022-01-04 09:17:43 +00:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
DescriptionList,
|
|
|
|
PageSection,
|
|
|
|
ToolbarItem,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
import {
|
|
|
|
ExpandableRowContent,
|
|
|
|
TableComposable,
|
|
|
|
Tbody,
|
|
|
|
Td,
|
|
|
|
Th,
|
|
|
|
Thead,
|
|
|
|
Tr,
|
|
|
|
} from "@patternfly/react-table";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { useState } from "react";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { Link, useNavigate } from "react-router-dom";
|
2022-01-04 09:17:43 +00:00
|
|
|
|
2023-05-03 13:51:02 +00:00
|
|
|
import { adminClient } from "../../admin-client";
|
2022-01-04 09:17:43 +00:00
|
|
|
import { KeycloakSpinner } from "../../components/keycloak-spinner/KeycloakSpinner";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { ListEmptyState } from "../../components/list-empty-state/ListEmptyState";
|
2022-01-04 09:17:43 +00:00
|
|
|
import { PaginatingTableToolbar } from "../../components/table-toolbar/PaginatingTableToolbar";
|
|
|
|
import { useRealm } from "../../context/realm-context/RealmContext";
|
2023-05-03 15:40:27 +00:00
|
|
|
import { useFetch } from "../../utils/useFetch";
|
2022-01-04 09:17:43 +00:00
|
|
|
import useToggle from "../../utils/useToggle";
|
2022-01-19 10:11:40 +00:00
|
|
|
import { toNewPermission } from "../routes/NewPermission";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { toNewScope } from "../routes/NewScope";
|
2022-03-21 06:45:49 +00:00
|
|
|
import { toPermissionDetails } from "../routes/PermissionDetails";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { toResourceDetails } from "../routes/Resource";
|
|
|
|
import { toScopeDetails } from "../routes/Scope";
|
|
|
|
import { DeleteScopeDialog } from "./DeleteScopeDialog";
|
|
|
|
import { DetailDescriptionLink } from "./DetailDescription";
|
2022-01-04 09:17:43 +00:00
|
|
|
|
|
|
|
type ScopesProps = {
|
|
|
|
clientId: string;
|
2023-11-28 13:07:11 +00:00
|
|
|
isDisabled?: boolean;
|
2022-01-04 09:17:43 +00:00
|
|
|
};
|
|
|
|
|
2023-01-13 12:07:13 +00:00
|
|
|
export type PermissionScopeRepresentation = ScopeRepresentation & {
|
2022-01-04 09:17:43 +00:00
|
|
|
permissions?: PolicyRepresentation[];
|
2023-01-13 12:07:13 +00:00
|
|
|
isLoaded: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
type ExpandableRow = {
|
|
|
|
id: string;
|
2022-01-04 09:17:43 +00:00
|
|
|
isExpanded: boolean;
|
|
|
|
};
|
|
|
|
|
2023-11-28 13:07:11 +00:00
|
|
|
export const AuthorizationScopes = ({
|
|
|
|
clientId,
|
|
|
|
isDisabled = false,
|
|
|
|
}: ScopesProps) => {
|
2023-09-08 13:17:17 +00:00
|
|
|
const { t } = useTranslation();
|
2022-08-16 13:09:14 +00:00
|
|
|
const navigate = useNavigate();
|
2022-01-04 09:17:43 +00:00
|
|
|
const { realm } = useRealm();
|
|
|
|
|
|
|
|
const [deleteDialog, toggleDeleteDialog] = useToggle();
|
2023-01-13 12:07:13 +00:00
|
|
|
const [scopes, setScopes] = useState<PermissionScopeRepresentation[]>();
|
2022-01-04 09:17:43 +00:00
|
|
|
const [selectedScope, setSelectedScope] =
|
2023-01-13 12:07:13 +00:00
|
|
|
useState<PermissionScopeRepresentation>();
|
|
|
|
const [collapsed, setCollapsed] = useState<ExpandableRow[]>([]);
|
2022-01-04 09:17:43 +00:00
|
|
|
|
|
|
|
const [key, setKey] = useState(0);
|
|
|
|
const refresh = () => setKey(key + 1);
|
|
|
|
|
|
|
|
const [max, setMax] = useState(10);
|
|
|
|
const [first, setFirst] = useState(0);
|
2022-03-16 13:57:50 +00:00
|
|
|
const [search, setSearch] = useState("");
|
2022-01-04 09:17:43 +00:00
|
|
|
|
|
|
|
useFetch(
|
2023-02-16 14:53:16 +00:00
|
|
|
() => {
|
2022-01-04 09:17:43 +00:00
|
|
|
const params = {
|
|
|
|
first,
|
2022-04-21 14:53:30 +00:00
|
|
|
max: max + 1,
|
2022-01-04 09:17:43 +00:00
|
|
|
deep: false,
|
2022-03-16 13:57:50 +00:00
|
|
|
name: search,
|
2022-01-04 09:17:43 +00:00
|
|
|
};
|
2023-02-16 14:53:16 +00:00
|
|
|
return adminClient.clients.listAllScopes({
|
2022-01-04 09:17:43 +00:00
|
|
|
...params,
|
|
|
|
id: clientId,
|
|
|
|
});
|
|
|
|
},
|
2023-01-13 12:07:13 +00:00
|
|
|
(scopes) => {
|
2023-02-16 14:53:16 +00:00
|
|
|
setScopes(scopes.map((s) => ({ ...s, isLoaded: false })));
|
2023-01-13 12:07:13 +00:00
|
|
|
setCollapsed(scopes.map((s) => ({ id: s.id!, isExpanded: false })));
|
|
|
|
},
|
2023-07-11 14:03:21 +00:00
|
|
|
[key, search, first, max],
|
2022-01-04 09:17:43 +00:00
|
|
|
);
|
|
|
|
|
2023-01-13 12:07:13 +00:00
|
|
|
const getScope = (id: string) => scopes?.find((scope) => scope.id === id)!;
|
|
|
|
const isExpanded = (id: string | undefined) =>
|
|
|
|
collapsed.find((c) => c.id === id)?.isExpanded || false;
|
|
|
|
|
|
|
|
useFetch(
|
2023-02-16 14:53:16 +00:00
|
|
|
() => {
|
2023-01-13 12:07:13 +00:00
|
|
|
const newlyOpened = collapsed
|
|
|
|
.filter((row) => row.isExpanded)
|
|
|
|
.map(({ id }) => getScope(id))
|
|
|
|
.filter((s) => !s.isLoaded);
|
|
|
|
|
2023-02-16 14:53:16 +00:00
|
|
|
return Promise.all(
|
|
|
|
newlyOpened.map(async (scope) => {
|
|
|
|
const [resources, permissions] = await Promise.all([
|
|
|
|
adminClient.clients.listAllResourcesByScope({
|
|
|
|
id: clientId,
|
|
|
|
scopeId: scope.id!,
|
|
|
|
}),
|
|
|
|
adminClient.clients.listAllPermissionsByScope({
|
|
|
|
id: clientId,
|
|
|
|
scopeId: scope.id!,
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...scope,
|
|
|
|
resources,
|
|
|
|
permissions,
|
|
|
|
isLoaded: true,
|
|
|
|
};
|
2023-07-11 14:03:21 +00:00
|
|
|
}),
|
2023-01-13 12:07:13 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
(resourcesScopes) => {
|
|
|
|
let result = [...(scopes || [])];
|
|
|
|
resourcesScopes.forEach((resourceScope) => {
|
|
|
|
const index = scopes?.findIndex(
|
2023-07-11 14:03:21 +00:00
|
|
|
(scope) => resourceScope.id === scope.id,
|
2023-01-13 12:07:13 +00:00
|
|
|
)!;
|
|
|
|
result = [
|
|
|
|
...result.slice(0, index),
|
|
|
|
resourceScope,
|
|
|
|
...result.slice(index + 1),
|
|
|
|
];
|
|
|
|
});
|
|
|
|
|
|
|
|
setScopes(result);
|
|
|
|
},
|
2023-07-11 14:03:21 +00:00
|
|
|
[collapsed],
|
2022-05-09 10:42:11 +00:00
|
|
|
);
|
2022-01-04 09:17:43 +00:00
|
|
|
|
|
|
|
if (!scopes) {
|
|
|
|
return <KeycloakSpinner />;
|
|
|
|
}
|
|
|
|
|
2022-03-16 13:57:50 +00:00
|
|
|
const noData = scopes.length === 0;
|
|
|
|
const searching = search !== "";
|
2022-01-04 09:17:43 +00:00
|
|
|
return (
|
|
|
|
<PageSection variant="light" className="pf-u-p-0">
|
|
|
|
<DeleteScopeDialog
|
|
|
|
clientId={clientId}
|
|
|
|
open={deleteDialog}
|
|
|
|
toggleDialog={toggleDeleteDialog}
|
|
|
|
selectedScope={selectedScope}
|
|
|
|
refresh={refresh}
|
|
|
|
/>
|
2022-03-16 13:57:50 +00:00
|
|
|
{(!noData || searching) && (
|
2022-01-04 09:17:43 +00:00
|
|
|
<PaginatingTableToolbar
|
|
|
|
count={scopes.length}
|
|
|
|
first={first}
|
|
|
|
max={max}
|
|
|
|
onNextClick={setFirst}
|
|
|
|
onPreviousClick={setFirst}
|
|
|
|
onPerPageSelect={(first, max) => {
|
|
|
|
setFirst(first);
|
|
|
|
setMax(max);
|
|
|
|
}}
|
2022-03-16 13:57:50 +00:00
|
|
|
inputGroupName="search"
|
|
|
|
inputGroupPlaceholder={t("searchByName")}
|
|
|
|
inputGroupOnEnter={setSearch}
|
2022-01-04 09:17:43 +00:00
|
|
|
toolbarItem={
|
|
|
|
<ToolbarItem>
|
|
|
|
<Button
|
|
|
|
data-testid="createAuthorizationScope"
|
|
|
|
component={(props) => (
|
|
|
|
<Link {...props} to={toNewScope({ realm, id: clientId })} />
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{t("createAuthorizationScope")}
|
|
|
|
</Button>
|
|
|
|
</ToolbarItem>
|
|
|
|
}
|
|
|
|
>
|
2022-03-16 13:57:50 +00:00
|
|
|
{!noData && (
|
|
|
|
<TableComposable aria-label={t("scopes")} variant="compact">
|
|
|
|
<Thead>
|
2022-01-04 09:17:43 +00:00
|
|
|
<Tr>
|
2023-07-11 12:04:32 +00:00
|
|
|
<Th aria-hidden="true" />
|
2023-09-14 09:01:15 +00:00
|
|
|
<Th>{t("name")}</Th>
|
2023-03-22 12:29:53 +00:00
|
|
|
<Th>{t("displayName")}</Th>
|
2023-07-11 12:04:32 +00:00
|
|
|
<Th aria-hidden="true" />
|
|
|
|
<Th aria-hidden="true" />
|
2022-01-04 09:17:43 +00:00
|
|
|
</Tr>
|
2022-03-16 13:57:50 +00:00
|
|
|
</Thead>
|
|
|
|
{scopes.map((scope, rowIndex) => (
|
2023-01-13 12:07:13 +00:00
|
|
|
<Tbody key={scope.id} isExpanded={isExpanded(scope.id)}>
|
2022-03-16 13:57:50 +00:00
|
|
|
<Tr>
|
|
|
|
<Td
|
|
|
|
expand={{
|
|
|
|
rowIndex,
|
2023-01-13 12:07:13 +00:00
|
|
|
isExpanded: isExpanded(scope.id),
|
|
|
|
onToggle: (_event, index, isExpanded) => {
|
|
|
|
setCollapsed([
|
|
|
|
...collapsed.slice(0, index),
|
|
|
|
{ id: scope.id!, isExpanded },
|
|
|
|
...collapsed.slice(index + 1),
|
|
|
|
]);
|
2022-03-16 13:57:50 +00:00
|
|
|
},
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Td data-testid={`name-column-${scope.name}`}>
|
|
|
|
<Link
|
|
|
|
to={toScopeDetails({
|
|
|
|
realm,
|
|
|
|
id: clientId,
|
|
|
|
scopeId: scope.id!,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{scope.name}
|
|
|
|
</Link>
|
|
|
|
</Td>
|
2023-03-22 12:29:53 +00:00
|
|
|
<Td>{scope.displayName}</Td>
|
2022-03-16 13:57:50 +00:00
|
|
|
<Td width={10}>
|
|
|
|
<Button
|
|
|
|
variant="link"
|
|
|
|
component={(props) => (
|
|
|
|
<Link
|
|
|
|
{...props}
|
|
|
|
to={toNewPermission({
|
|
|
|
realm,
|
|
|
|
id: clientId,
|
|
|
|
permissionType: "scope",
|
|
|
|
selectedId: scope.id,
|
|
|
|
})}
|
2022-01-12 16:01:54 +00:00
|
|
|
/>
|
2022-03-16 13:57:50 +00:00
|
|
|
)}
|
|
|
|
>
|
|
|
|
{t("createPermission")}
|
|
|
|
</Button>
|
|
|
|
</Td>
|
|
|
|
<Td
|
|
|
|
isActionCell
|
|
|
|
actions={{
|
|
|
|
items: [
|
|
|
|
{
|
2023-09-14 09:01:15 +00:00
|
|
|
title: t("delete"),
|
2022-03-16 13:57:50 +00:00
|
|
|
onClick: () => {
|
|
|
|
setSelectedScope(scope);
|
|
|
|
toggleDeleteDialog();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Tr>
|
2023-01-13 12:07:13 +00:00
|
|
|
<Tr
|
|
|
|
key={`child-${scope.id}`}
|
|
|
|
isExpanded={isExpanded(scope.id)}
|
|
|
|
>
|
2022-03-16 13:57:50 +00:00
|
|
|
<Td />
|
|
|
|
<Td colSpan={4}>
|
|
|
|
<ExpandableRowContent>
|
2023-02-16 14:53:16 +00:00
|
|
|
{isExpanded(scope.id) && scope.isLoaded ? (
|
2022-03-16 13:57:50 +00:00
|
|
|
<DescriptionList
|
|
|
|
isHorizontal
|
|
|
|
className="keycloak_resource_details"
|
|
|
|
>
|
2022-03-21 06:45:49 +00:00
|
|
|
<DetailDescriptionLink
|
2022-03-16 13:57:50 +00:00
|
|
|
name="resources"
|
|
|
|
array={scope.resources}
|
|
|
|
convert={(r) => r.name!}
|
2022-03-21 06:45:49 +00:00
|
|
|
link={(r) =>
|
|
|
|
toResourceDetails({
|
|
|
|
id: clientId,
|
|
|
|
realm,
|
|
|
|
resourceId: r._id!,
|
|
|
|
})
|
|
|
|
}
|
2022-03-16 13:57:50 +00:00
|
|
|
/>
|
2022-03-21 06:45:49 +00:00
|
|
|
<DetailDescriptionLink
|
2022-03-16 13:57:50 +00:00
|
|
|
name="associatedPermissions"
|
|
|
|
array={scope.permissions}
|
|
|
|
convert={(p) => p.name!}
|
2022-03-21 06:45:49 +00:00
|
|
|
link={(p) =>
|
|
|
|
toPermissionDetails({
|
|
|
|
id: clientId,
|
|
|
|
realm,
|
|
|
|
permissionId: p.id!,
|
|
|
|
permissionType: p.type!,
|
|
|
|
})
|
|
|
|
}
|
2022-03-16 13:57:50 +00:00
|
|
|
/>
|
|
|
|
</DescriptionList>
|
2023-02-16 14:53:16 +00:00
|
|
|
) : (
|
|
|
|
<KeycloakSpinner />
|
2022-03-16 13:57:50 +00:00
|
|
|
)}
|
|
|
|
</ExpandableRowContent>
|
|
|
|
</Td>
|
|
|
|
</Tr>
|
|
|
|
</Tbody>
|
|
|
|
))}
|
|
|
|
</TableComposable>
|
|
|
|
)}
|
2022-01-04 09:17:43 +00:00
|
|
|
</PaginatingTableToolbar>
|
|
|
|
)}
|
2022-03-16 13:57:50 +00:00
|
|
|
{noData && !searching && (
|
2022-01-04 09:17:43 +00:00
|
|
|
<ListEmptyState
|
|
|
|
message={t("emptyAuthorizationScopes")}
|
|
|
|
instructions={t("emptyAuthorizationInstructions")}
|
2023-11-28 13:07:11 +00:00
|
|
|
isDisabled={isDisabled}
|
2022-08-16 13:09:14 +00:00
|
|
|
onPrimaryAction={() => navigate(toNewScope({ id: clientId, realm }))}
|
2022-01-04 09:17:43 +00:00
|
|
|
primaryActionText={t("createAuthorizationScope")}
|
|
|
|
/>
|
|
|
|
)}
|
2022-03-16 13:57:50 +00:00
|
|
|
{noData && searching && (
|
|
|
|
<ListEmptyState
|
|
|
|
isSearchVariant
|
2023-11-28 13:07:11 +00:00
|
|
|
isDisabled={isDisabled}
|
2023-09-14 09:01:15 +00:00
|
|
|
message={t("noSearchResults")}
|
|
|
|
instructions={t("noSearchResultsInstructions")}
|
2022-03-16 13:57:50 +00:00
|
|
|
/>
|
|
|
|
)}
|
2022-01-04 09:17:43 +00:00
|
|
|
</PageSection>
|
|
|
|
);
|
|
|
|
};
|