2023-05-03 13:51:02 +00:00
|
|
|
import type ClientProfileRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientProfileRepresentation";
|
|
|
|
import type RoleRepresentation from "@keycloak/keycloak-admin-client/lib/defs/roleRepresentation";
|
2021-11-16 11:10:10 +00:00
|
|
|
import { Button, Label, Modal, ModalVariant } from "@patternfly/react-core";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { useState } from "react";
|
2021-10-29 14:19:57 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2023-05-03 13:51:02 +00:00
|
|
|
|
|
|
|
import { adminClient } from "../admin-client";
|
2021-11-16 11:10:10 +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";
|
|
|
|
import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable";
|
2023-05-03 15:40:27 +00:00
|
|
|
import { useFetch } from "../utils/useFetch";
|
2021-10-29 14:19:57 +00:00
|
|
|
|
|
|
|
type ClientProfile = ClientProfileRepresentation & {
|
|
|
|
global: boolean;
|
|
|
|
};
|
|
|
|
|
2023-03-21 10:36:20 +00:00
|
|
|
const AliasRenderer = ({ name, global }: ClientProfile) => {
|
|
|
|
const { t } = useTranslation("roles");
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{name}{" "}
|
|
|
|
{global && <Label color="blue">{t("realm-settings:global")}</Label>}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-10-29 14:19:57 +00:00
|
|
|
export type AddClientProfileModalProps = {
|
|
|
|
open: boolean;
|
|
|
|
toggleDialog: () => void;
|
|
|
|
onConfirm: (newReps: RoleRepresentation[]) => void;
|
|
|
|
allProfiles: string[];
|
|
|
|
};
|
|
|
|
|
|
|
|
export const AddClientProfileModal = (props: AddClientProfileModalProps) => {
|
|
|
|
const { t } = useTranslation("roles");
|
|
|
|
const [selectedRows, setSelectedRows] = useState<RoleRepresentation[]>([]);
|
|
|
|
|
|
|
|
const [tableProfiles, setTableProfiles] = useState<ClientProfile[]>();
|
|
|
|
|
|
|
|
useFetch(
|
|
|
|
() =>
|
|
|
|
adminClient.clientPolicies.listProfiles({
|
|
|
|
includeGlobalProfiles: true,
|
|
|
|
}),
|
|
|
|
(allProfiles) => {
|
|
|
|
const globalProfiles = allProfiles.globalProfiles?.map(
|
|
|
|
(globalProfiles) => ({
|
|
|
|
...globalProfiles,
|
|
|
|
global: true,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const profiles = allProfiles.profiles?.map((profiles) => ({
|
|
|
|
...profiles,
|
|
|
|
global: false,
|
|
|
|
}));
|
|
|
|
|
|
|
|
setTableProfiles([...(globalProfiles ?? []), ...(profiles ?? [])]);
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
2022-02-18 10:51:03 +00:00
|
|
|
const loader = async () =>
|
|
|
|
tableProfiles?.filter((item) => !props.allProfiles.includes(item.name!)) ??
|
|
|
|
[];
|
2021-10-29 14:19:57 +00:00
|
|
|
|
|
|
|
if (!tableProfiles) {
|
2021-11-16 11:10:10 +00:00
|
|
|
return <KeycloakSpinner />;
|
2021-10-29 14:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
data-testid="addClientProfile"
|
|
|
|
title={t("realm-settings:addClientProfile")}
|
|
|
|
isOpen={props.open}
|
|
|
|
onClose={props.toggleDialog}
|
|
|
|
variant={ModalVariant.large}
|
|
|
|
actions={[
|
|
|
|
<Button
|
|
|
|
key="add"
|
|
|
|
data-testid="add-client-profile-button"
|
|
|
|
variant="primary"
|
|
|
|
isDisabled={!selectedRows.length}
|
|
|
|
onClick={() => {
|
|
|
|
props.toggleDialog();
|
|
|
|
props.onConfirm(selectedRows);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t("common:add")}
|
|
|
|
</Button>,
|
|
|
|
<Button
|
|
|
|
key="cancel"
|
|
|
|
variant="link"
|
|
|
|
onClick={() => {
|
|
|
|
props.toggleDialog();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t("common:cancel")}
|
|
|
|
</Button>,
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
<KeycloakDataTable
|
|
|
|
loader={loader}
|
|
|
|
ariaLabelKey="realm-settings:profilesList"
|
|
|
|
searchPlaceholderKey="realm-settings:searchProfile"
|
|
|
|
canSelectAll
|
|
|
|
onSelect={(rows) => {
|
|
|
|
setSelectedRows([...rows]);
|
|
|
|
}}
|
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
name: "name",
|
|
|
|
displayKey: "realm-settings:clientProfileName",
|
|
|
|
cellRenderer: AliasRenderer,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "description",
|
|
|
|
displayKey: "common:description",
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
emptyState={
|
|
|
|
<ListEmptyState
|
|
|
|
hasIcon
|
|
|
|
message={t("noRoles")}
|
|
|
|
instructions={t("noRolesInstructions")}
|
|
|
|
primaryActionText={t("createRole")}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|