2023-05-03 13:51:02 +00:00
|
|
|
import type PolicyProviderRepresentation from "@keycloak/keycloak-admin-client/lib/defs/policyProviderRepresentation";
|
|
|
|
import type PolicyRepresentation from "@keycloak/keycloak-admin-client/lib/defs/policyRepresentation";
|
2022-01-21 14:10:36 +00:00
|
|
|
import {
|
|
|
|
Alert,
|
|
|
|
AlertVariant,
|
|
|
|
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-21 14:10:36 +00:00
|
|
|
|
2023-05-03 13:51:02 +00:00
|
|
|
import { adminClient } from "../../admin-client";
|
|
|
|
import { useAlerts } from "../../components/alert/Alerts";
|
2022-01-21 14:10:36 +00:00
|
|
|
import { useConfirmDialog } from "../../components/confirm-dialog/ConfirmDialog";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { KeycloakSpinner } from "../../components/keycloak-spinner/KeycloakSpinner";
|
|
|
|
import { ListEmptyState } from "../../components/list-empty-state/ListEmptyState";
|
2022-01-21 14:10:36 +00:00
|
|
|
import { PaginatingTableToolbar } from "../../components/table-toolbar/PaginatingTableToolbar";
|
|
|
|
import { useRealm } from "../../context/realm-context/RealmContext";
|
|
|
|
import { toUpperCase } from "../../util";
|
2023-05-03 15:40:27 +00:00
|
|
|
import { useFetch } from "../../utils/useFetch";
|
2022-01-21 14:10:36 +00:00
|
|
|
import useToggle from "../../utils/useToggle";
|
|
|
|
import { toCreatePolicy } from "../routes/NewPolicy";
|
2022-03-21 06:45:49 +00:00
|
|
|
import { toPermissionDetails } from "../routes/PermissionDetails";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { toPolicyDetails } from "../routes/PolicyDetails";
|
2022-03-21 06:45:49 +00:00
|
|
|
import { DetailDescriptionLink } from "./DetailDescription";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { MoreLabel } from "./MoreLabel";
|
|
|
|
import { NewPolicyDialog } from "./NewPolicyDialog";
|
|
|
|
import { SearchDropdown, SearchForm } from "./SearchDropdown";
|
2022-01-21 14:10:36 +00:00
|
|
|
|
|
|
|
type PoliciesProps = {
|
|
|
|
clientId: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type ExpandablePolicyRepresentation = PolicyRepresentation & {
|
|
|
|
dependentPolicies?: PolicyRepresentation[];
|
|
|
|
isExpanded: boolean;
|
|
|
|
};
|
|
|
|
|
2023-02-09 16:31:16 +00:00
|
|
|
const DependentPoliciesRenderer = ({
|
|
|
|
row,
|
|
|
|
}: {
|
|
|
|
row: ExpandablePolicyRepresentation;
|
|
|
|
}) => {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{row.dependentPolicies?.[0]?.name}{" "}
|
|
|
|
<MoreLabel array={row.dependentPolicies} />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-01-21 14:10:36 +00:00
|
|
|
export const AuthorizationPolicies = ({ clientId }: PoliciesProps) => {
|
2023-09-08 13:17:17 +00:00
|
|
|
const { t } = useTranslation();
|
2022-01-21 14:10:36 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
|
|
|
const { realm } = useRealm();
|
2022-08-16 13:09:14 +00:00
|
|
|
const navigate = useNavigate();
|
2022-01-21 14:10:36 +00:00
|
|
|
|
|
|
|
const [policies, setPolicies] = useState<ExpandablePolicyRepresentation[]>();
|
|
|
|
const [selectedPolicy, setSelectedPolicy] =
|
|
|
|
useState<ExpandablePolicyRepresentation>();
|
|
|
|
const [policyProviders, setPolicyProviders] =
|
|
|
|
useState<PolicyProviderRepresentation[]>();
|
|
|
|
|
|
|
|
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<SearchForm>({});
|
2022-01-21 14:10:36 +00:00
|
|
|
const [newDialog, toggleDialog] = useToggle();
|
|
|
|
|
|
|
|
useFetch(
|
|
|
|
async () => {
|
|
|
|
const policies = await adminClient.clients.listPolicies({
|
|
|
|
first,
|
2022-04-21 14:53:30 +00:00
|
|
|
max: max + 1,
|
2022-01-21 14:10:36 +00:00
|
|
|
id: clientId,
|
|
|
|
permission: "false",
|
2022-03-16 13:57:50 +00:00
|
|
|
...search,
|
2022-01-21 14:10:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return await Promise.all([
|
|
|
|
adminClient.clients.listPolicyProviders({ id: clientId }),
|
2022-10-31 11:29:53 +00:00
|
|
|
...(policies || []).map(async (policy) => {
|
2022-01-21 14:10:36 +00:00
|
|
|
const dependentPolicies =
|
|
|
|
await adminClient.clients.listDependentPolicies({
|
|
|
|
id: clientId,
|
|
|
|
policyId: policy.id!,
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
...policy,
|
|
|
|
dependentPolicies,
|
|
|
|
isExpanded: false,
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
([providers, ...policies]) => {
|
|
|
|
setPolicyProviders(
|
2023-07-11 14:03:21 +00:00
|
|
|
providers.filter((p) => p.type !== "resource" && p.type !== "scope"),
|
2022-01-21 14:10:36 +00:00
|
|
|
);
|
|
|
|
setPolicies(policies);
|
|
|
|
},
|
2023-07-11 14:03:21 +00:00
|
|
|
[key, search, first, max],
|
2022-01-21 14:10:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({
|
2023-09-13 14:05:17 +00:00
|
|
|
titleKey: "deletePolicy",
|
2022-01-21 14:10:36 +00:00
|
|
|
children: (
|
|
|
|
<>
|
|
|
|
{t("deletePolicyConfirm")}
|
|
|
|
{selectedPolicy?.dependentPolicies &&
|
|
|
|
selectedPolicy.dependentPolicies.length > 0 && (
|
|
|
|
<Alert
|
|
|
|
variant="warning"
|
|
|
|
isInline
|
|
|
|
isPlain
|
2023-06-29 11:59:07 +00:00
|
|
|
component="p"
|
2022-01-21 14:10:36 +00:00
|
|
|
title={t("deletePolicyWarning")}
|
|
|
|
className="pf-u-pt-lg"
|
|
|
|
>
|
|
|
|
<p className="pf-u-pt-xs">
|
|
|
|
{selectedPolicy.dependentPolicies.map((policy) => (
|
|
|
|
<strong key={policy.id} className="pf-u-pr-md">
|
|
|
|
{policy.name}
|
|
|
|
</strong>
|
|
|
|
))}
|
|
|
|
</p>
|
|
|
|
</Alert>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
),
|
2023-09-13 14:05:17 +00:00
|
|
|
continueButtonLabel: "confirm",
|
2022-01-21 14:10:36 +00:00
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
|
|
|
await adminClient.clients.delPolicy({
|
|
|
|
id: clientId,
|
|
|
|
policyId: selectedPolicy?.id!,
|
|
|
|
});
|
|
|
|
addAlert(t("policyDeletedSuccess"), AlertVariant.success);
|
|
|
|
refresh();
|
|
|
|
} catch (error) {
|
2023-09-13 14:05:17 +00:00
|
|
|
addError("policyDeletedError", error);
|
2022-01-21 14:10:36 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!policies) {
|
|
|
|
return <KeycloakSpinner />;
|
|
|
|
}
|
|
|
|
|
2022-03-16 13:57:50 +00:00
|
|
|
const noData = policies.length === 0;
|
|
|
|
const searching = Object.keys(search).length !== 0;
|
2022-01-21 14:10:36 +00:00
|
|
|
return (
|
|
|
|
<PageSection variant="light" className="pf-u-p-0">
|
|
|
|
<DeleteConfirm />
|
2022-03-16 13:57:50 +00:00
|
|
|
{(!noData || searching) && (
|
2022-01-21 14:10:36 +00:00
|
|
|
<>
|
|
|
|
{newDialog && (
|
|
|
|
<NewPolicyDialog
|
|
|
|
policyProviders={policyProviders}
|
|
|
|
onSelect={(p) =>
|
2022-08-16 13:09:14 +00:00
|
|
|
navigate(
|
2023-07-11 14:03:21 +00:00
|
|
|
toCreatePolicy({ id: clientId, realm, policyType: p.type! }),
|
2022-01-21 14:10:36 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
toggleDialog={toggleDialog}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<PaginatingTableToolbar
|
|
|
|
count={policies.length}
|
|
|
|
first={first}
|
|
|
|
max={max}
|
|
|
|
onNextClick={setFirst}
|
|
|
|
onPreviousClick={setFirst}
|
|
|
|
onPerPageSelect={(first, max) => {
|
|
|
|
setFirst(first);
|
|
|
|
setMax(max);
|
|
|
|
}}
|
|
|
|
toolbarItem={
|
2022-03-16 13:57:50 +00:00
|
|
|
<>
|
|
|
|
<ToolbarItem>
|
|
|
|
<SearchDropdown
|
|
|
|
types={policyProviders}
|
|
|
|
search={search}
|
|
|
|
onSearch={setSearch}
|
|
|
|
/>
|
|
|
|
</ToolbarItem>
|
|
|
|
<ToolbarItem>
|
|
|
|
<Button data-testid="createPolicy" onClick={toggleDialog}>
|
|
|
|
{t("createPolicy")}
|
|
|
|
</Button>
|
|
|
|
</ToolbarItem>
|
|
|
|
</>
|
2022-01-21 14:10:36 +00:00
|
|
|
}
|
|
|
|
>
|
2022-03-16 13:57:50 +00:00
|
|
|
{!noData && (
|
|
|
|
<TableComposable aria-label={t("resources")} variant="compact">
|
|
|
|
<Thead>
|
2022-01-21 14:10:36 +00:00
|
|
|
<Tr>
|
2023-06-30 17:24:35 +00:00
|
|
|
<Th aria-hidden="true" />
|
2023-09-14 09:01:15 +00:00
|
|
|
<Th>{t("name")}</Th>
|
|
|
|
<Th>{t("type")}</Th>
|
2022-03-16 13:57:50 +00:00
|
|
|
<Th>{t("dependentPermission")}</Th>
|
2023-09-14 09:01:15 +00:00
|
|
|
<Th>{t("description")}</Th>
|
2023-06-30 17:24:35 +00:00
|
|
|
<Th aria-hidden="true" />
|
2022-01-21 14:10:36 +00:00
|
|
|
</Tr>
|
2022-03-16 13:57:50 +00:00
|
|
|
</Thead>
|
|
|
|
{policies.map((policy, rowIndex) => (
|
|
|
|
<Tbody key={policy.id} isExpanded={policy.isExpanded}>
|
|
|
|
<Tr>
|
|
|
|
<Td
|
|
|
|
expand={{
|
|
|
|
rowIndex,
|
|
|
|
isExpanded: policy.isExpanded,
|
|
|
|
onToggle: (_, rowIndex) => {
|
|
|
|
const rows = policies.map((policy, index) =>
|
|
|
|
index === rowIndex
|
|
|
|
? { ...policy, isExpanded: !policy.isExpanded }
|
2023-07-11 14:03:21 +00:00
|
|
|
: policy,
|
2022-03-16 13:57:50 +00:00
|
|
|
);
|
|
|
|
setPolicies(rows);
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Td data-testid={`name-column-${policy.name}`}>
|
|
|
|
<Link
|
|
|
|
to={toPolicyDetails({
|
|
|
|
realm,
|
|
|
|
id: clientId,
|
|
|
|
policyType: policy.type!,
|
|
|
|
policyId: policy.id!,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{policy.name}
|
|
|
|
</Link>
|
|
|
|
</Td>
|
|
|
|
<Td>{toUpperCase(policy.type!)}</Td>
|
|
|
|
<Td>
|
|
|
|
<DependentPoliciesRenderer row={policy} />
|
|
|
|
</Td>
|
|
|
|
<Td>{policy.description}</Td>
|
|
|
|
<Td
|
|
|
|
actions={{
|
|
|
|
items: [
|
|
|
|
{
|
2023-09-14 09:01:15 +00:00
|
|
|
title: t("delete"),
|
2022-03-16 13:57:50 +00:00
|
|
|
onClick: async () => {
|
|
|
|
setSelectedPolicy(policy);
|
|
|
|
toggleDeleteDialog();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Tr>
|
|
|
|
<Tr
|
|
|
|
key={`child-${policy.id}`}
|
|
|
|
isExpanded={policy.isExpanded}
|
|
|
|
>
|
|
|
|
<Td />
|
|
|
|
<Td colSpan={4}>
|
|
|
|
<ExpandableRowContent>
|
|
|
|
{policy.isExpanded && (
|
|
|
|
<DescriptionList
|
|
|
|
isHorizontal
|
|
|
|
className="keycloak_resource_details"
|
|
|
|
>
|
2022-03-21 06:45:49 +00:00
|
|
|
<DetailDescriptionLink
|
2022-03-16 13:57:50 +00:00
|
|
|
name="dependentPermission"
|
|
|
|
array={policy.dependentPolicies}
|
|
|
|
convert={(p) => p.name!}
|
2022-03-21 06:45:49 +00:00
|
|
|
link={(permission) =>
|
|
|
|
toPermissionDetails({
|
|
|
|
realm,
|
|
|
|
id: clientId,
|
|
|
|
permissionId: permission.id!,
|
|
|
|
permissionType: permission.type!,
|
|
|
|
})
|
|
|
|
}
|
2022-03-16 13:57:50 +00:00
|
|
|
/>
|
|
|
|
</DescriptionList>
|
|
|
|
)}
|
|
|
|
</ExpandableRowContent>
|
|
|
|
</Td>
|
|
|
|
</Tr>
|
|
|
|
</Tbody>
|
|
|
|
))}
|
|
|
|
</TableComposable>
|
|
|
|
)}
|
2022-01-21 14:10:36 +00:00
|
|
|
</PaginatingTableToolbar>
|
|
|
|
</>
|
|
|
|
)}
|
2022-03-16 13:57:50 +00:00
|
|
|
{noData && searching && (
|
|
|
|
<ListEmptyState
|
|
|
|
isSearchVariant
|
2023-09-14 09:01:15 +00:00
|
|
|
message={t("noSearchResults")}
|
|
|
|
instructions={t("noSearchResultsInstructions")}
|
2022-03-16 13:57:50 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{noData && !searching && (
|
2022-01-21 14:10:36 +00:00
|
|
|
<>
|
|
|
|
{newDialog && (
|
|
|
|
<NewPolicyDialog
|
|
|
|
policyProviders={policyProviders?.filter(
|
2023-07-11 14:03:21 +00:00
|
|
|
(p) => p.type !== "aggregate",
|
2022-01-21 14:10:36 +00:00
|
|
|
)}
|
|
|
|
onSelect={(p) =>
|
2022-08-16 13:09:14 +00:00
|
|
|
navigate(
|
2023-07-11 14:03:21 +00:00
|
|
|
toCreatePolicy({ id: clientId, realm, policyType: p.type! }),
|
2022-01-21 14:10:36 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
toggleDialog={toggleDialog}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<ListEmptyState
|
|
|
|
message={t("emptyPolicies")}
|
|
|
|
instructions={t("emptyPoliciesInstructions")}
|
|
|
|
primaryActionText={t("createPolicy")}
|
|
|
|
onPrimaryAction={toggleDialog}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</PageSection>
|
|
|
|
);
|
|
|
|
};
|