2022-08-03 12:12:07 +00:00
|
|
|
import { Fragment, useEffect, useMemo, useState } from "react";
|
2021-10-26 20:16:19 +00:00
|
|
|
import {
|
|
|
|
ActionGroup,
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
ButtonVariant,
|
|
|
|
DataList,
|
|
|
|
DataListCell,
|
|
|
|
DataListItem,
|
|
|
|
DataListItemCells,
|
|
|
|
DataListItemRow,
|
|
|
|
Divider,
|
|
|
|
DropdownItem,
|
|
|
|
Flex,
|
|
|
|
FlexItem,
|
|
|
|
FormGroup,
|
|
|
|
PageSection,
|
|
|
|
Text,
|
|
|
|
TextVariants,
|
|
|
|
ValidatedOptions,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
import { FormAccess } from "../components/form-access/FormAccess";
|
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
2022-08-22 15:51:19 +00:00
|
|
|
import { useParams } from "react-router-dom";
|
|
|
|
import { Link, useNavigate } from "react-router-dom-v5-compat";
|
2021-10-26 20:16:19 +00:00
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
|
|
|
import { useAdminClient, useFetch } from "../context/auth/AdminClient";
|
|
|
|
import type ClientProfileRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientProfileRepresentation";
|
|
|
|
import { HelpItem } from "../components/help-enabler/HelpItem";
|
2022-04-21 15:03:26 +00:00
|
|
|
import { KeycloakTextInput } from "../components/keycloak-text-input/KeycloakTextInput";
|
2022-05-11 09:46:14 +00:00
|
|
|
import { KeycloakTextArea } from "../components/keycloak-text-area/KeycloakTextArea";
|
2021-10-26 20:16:19 +00:00
|
|
|
import { PlusCircleIcon, TrashIcon } from "@patternfly/react-icons";
|
2022-02-16 11:39:08 +00:00
|
|
|
import "./realm-settings-section.css";
|
2021-10-26 20:16:19 +00:00
|
|
|
import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog";
|
|
|
|
import { toAddExecutor } from "./routes/AddExecutor";
|
|
|
|
import { useServerInfo } from "../context/server-info/ServerInfoProvider";
|
2021-11-02 17:11:04 +00:00
|
|
|
import { ClientProfileParams, toClientProfile } from "./routes/ClientProfile";
|
2021-11-10 14:26:43 +00:00
|
|
|
import { toExecutor } from "./routes/Executor";
|
2021-11-24 16:19:02 +00:00
|
|
|
import { toClientPolicies } from "./routes/ClientPolicies";
|
2021-10-26 20:16:19 +00:00
|
|
|
|
|
|
|
type ClientProfileForm = Required<ClientProfileRepresentation>;
|
|
|
|
|
|
|
|
const defaultValues: ClientProfileForm = {
|
|
|
|
name: "",
|
|
|
|
description: "",
|
|
|
|
executors: [],
|
|
|
|
};
|
|
|
|
|
2021-10-29 16:11:06 +00:00
|
|
|
export default function ClientProfileForm() {
|
2021-10-26 20:16:19 +00:00
|
|
|
const { t } = useTranslation("realm-settings");
|
2022-08-16 13:09:14 +00:00
|
|
|
const navigate = useNavigate();
|
2021-10-26 20:16:19 +00:00
|
|
|
const {
|
|
|
|
handleSubmit,
|
|
|
|
setValue,
|
|
|
|
register,
|
2022-04-08 12:37:31 +00:00
|
|
|
formState: { isDirty, errors },
|
2021-10-26 20:16:19 +00:00
|
|
|
} = useForm<ClientProfileForm>({
|
|
|
|
defaultValues,
|
|
|
|
mode: "onChange",
|
|
|
|
});
|
|
|
|
|
|
|
|
const { addAlert, addError } = useAlerts();
|
2022-07-14 13:02:28 +00:00
|
|
|
const { adminClient } = useAdminClient();
|
2021-10-26 20:16:19 +00:00
|
|
|
const [globalProfiles, setGlobalProfiles] = useState<
|
|
|
|
ClientProfileRepresentation[]
|
|
|
|
>([]);
|
|
|
|
const [profiles, setProfiles] = useState<ClientProfileRepresentation[]>([]);
|
|
|
|
const { realm, profileName } = useParams<ClientProfileParams>();
|
|
|
|
const serverInfo = useServerInfo();
|
|
|
|
const executorTypes = useMemo(
|
|
|
|
() =>
|
|
|
|
serverInfo.componentTypes?.[
|
|
|
|
"org.keycloak.services.clientpolicy.executor.ClientPolicyExecutorProvider"
|
|
|
|
],
|
|
|
|
[]
|
|
|
|
);
|
2022-03-28 10:55:21 +00:00
|
|
|
const [executorToDelete, setExecutorToDelete] = useState<{
|
|
|
|
idx: number;
|
|
|
|
name: string;
|
|
|
|
}>();
|
2021-10-26 20:16:19 +00:00
|
|
|
const editMode = profileName ? true : false;
|
|
|
|
const [key, setKey] = useState(0);
|
|
|
|
const reload = () => setKey(new Date().getTime());
|
|
|
|
|
|
|
|
useFetch(
|
|
|
|
() =>
|
|
|
|
adminClient.clientPolicies.listProfiles({ includeGlobalProfiles: true }),
|
|
|
|
(profiles) => {
|
|
|
|
setGlobalProfiles(profiles.globalProfiles ?? []);
|
|
|
|
setProfiles(profiles.profiles ?? []);
|
|
|
|
},
|
|
|
|
[key]
|
|
|
|
);
|
|
|
|
|
|
|
|
const save = async (form: ClientProfileForm) => {
|
|
|
|
const updatedProfiles = editMode ? patchProfiles(form) : addProfile(form);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await adminClient.clientPolicies.createProfiles({
|
|
|
|
profiles: updatedProfiles,
|
|
|
|
globalProfiles: globalProfiles,
|
|
|
|
});
|
|
|
|
|
|
|
|
addAlert(
|
|
|
|
editMode
|
|
|
|
? t("realm-settings:updateClientProfileSuccess")
|
|
|
|
: t("realm-settings:createClientProfileSuccess"),
|
|
|
|
AlertVariant.success
|
|
|
|
);
|
|
|
|
|
2022-08-16 13:09:14 +00:00
|
|
|
navigate(toClientProfile({ realm, profileName: form.name }));
|
2021-10-26 20:16:19 +00:00
|
|
|
} catch (error) {
|
|
|
|
addError(
|
|
|
|
editMode
|
|
|
|
? "realm-settings:updateClientProfileError"
|
|
|
|
: "realm-settings:createClientProfileError",
|
|
|
|
error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const patchProfiles = (data: ClientProfileRepresentation) =>
|
|
|
|
profiles.map((profile) => {
|
|
|
|
if (profile.name !== profileName) {
|
|
|
|
return profile;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...profile,
|
|
|
|
name: data.name,
|
|
|
|
description: data.description,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
const addProfile = (data: ClientProfileRepresentation) =>
|
|
|
|
profiles.concat({
|
|
|
|
...data,
|
|
|
|
executors: [],
|
|
|
|
});
|
|
|
|
|
|
|
|
const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({
|
|
|
|
titleKey: executorToDelete?.name!
|
|
|
|
? t("deleteExecutorProfileConfirmTitle")
|
|
|
|
: t("deleteClientProfileConfirmTitle"),
|
|
|
|
messageKey: executorToDelete?.name!
|
|
|
|
? t("deleteExecutorProfileConfirm", {
|
|
|
|
executorName: executorToDelete.name!,
|
|
|
|
})
|
|
|
|
: t("deleteClientProfileConfirm", {
|
|
|
|
profileName,
|
|
|
|
}),
|
|
|
|
continueButtonLabel: t("delete"),
|
|
|
|
continueButtonVariant: ButtonVariant.danger,
|
|
|
|
|
|
|
|
onConfirm: async () => {
|
|
|
|
if (executorToDelete?.name!) {
|
|
|
|
profileExecutors.splice(executorToDelete.idx!, 1);
|
|
|
|
try {
|
|
|
|
await adminClient.clientPolicies.createProfiles({
|
|
|
|
profiles: profiles,
|
|
|
|
globalProfiles,
|
|
|
|
});
|
|
|
|
addAlert(t("deleteExecutorSuccess"), AlertVariant.success);
|
2022-08-16 13:09:14 +00:00
|
|
|
navigate(toClientProfile({ realm, profileName }));
|
2021-10-26 20:16:19 +00:00
|
|
|
} catch (error) {
|
|
|
|
addError(t("deleteExecutorError"), error);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const updatedProfiles = profiles.filter(
|
|
|
|
(profile) => profile.name !== profileName
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await adminClient.clientPolicies.createProfiles({
|
|
|
|
profiles: updatedProfiles,
|
|
|
|
globalProfiles,
|
|
|
|
});
|
|
|
|
addAlert(t("deleteClientSuccess"), AlertVariant.success);
|
2022-08-16 13:09:14 +00:00
|
|
|
navigate(toClientPolicies({ realm, tab: "profiles" }));
|
2021-10-26 20:16:19 +00:00
|
|
|
} catch (error) {
|
|
|
|
addError(t("deleteClientError"), error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const profile = profiles.find((profile) => profile.name === profileName);
|
|
|
|
const profileExecutors = profile?.executors || [];
|
|
|
|
const globalProfile = globalProfiles.find(
|
|
|
|
(globalProfile) => globalProfile.name === profileName
|
|
|
|
);
|
|
|
|
const globalProfileExecutors = globalProfile?.executors || [];
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setValue("name", globalProfile?.name ?? profile?.name);
|
|
|
|
setValue("description", globalProfile?.description ?? profile?.description);
|
|
|
|
}, [profiles]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<DeleteConfirm />
|
|
|
|
<ViewHeader
|
|
|
|
titleKey={editMode ? profileName : t("newClientProfile")}
|
|
|
|
badges={[
|
|
|
|
{
|
|
|
|
id: "global-client-profile-badge",
|
|
|
|
text: globalProfile ? t("global") : "",
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
divider
|
|
|
|
dropdownItems={
|
2022-06-01 13:21:29 +00:00
|
|
|
editMode && !globalProfile
|
2021-10-26 20:16:19 +00:00
|
|
|
? [
|
|
|
|
<DropdownItem
|
|
|
|
key="delete"
|
|
|
|
value="delete"
|
|
|
|
onClick={toggleDeleteDialog}
|
|
|
|
data-testid="deleteClientProfileDropdown"
|
|
|
|
>
|
|
|
|
{t("deleteClientProfile")}
|
|
|
|
</DropdownItem>,
|
|
|
|
]
|
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<PageSection variant="light">
|
|
|
|
<FormAccess isHorizontal role="view-realm" className="pf-u-mt-lg">
|
|
|
|
<FormGroup
|
|
|
|
label={t("newClientProfileName")}
|
|
|
|
fieldId="kc-name"
|
|
|
|
helperText={t("createClientProfileNameHelperText")}
|
|
|
|
isRequired
|
|
|
|
helperTextInvalid={t("common:required")}
|
|
|
|
validated={
|
|
|
|
errors.name ? ValidatedOptions.error : ValidatedOptions.default
|
|
|
|
}
|
|
|
|
>
|
2022-04-21 15:03:26 +00:00
|
|
|
<KeycloakTextInput
|
2021-10-26 20:16:19 +00:00
|
|
|
ref={register({ required: true })}
|
|
|
|
name="name"
|
|
|
|
type="text"
|
|
|
|
id="name"
|
|
|
|
aria-label={t("name")}
|
|
|
|
data-testid="client-profile-name"
|
|
|
|
isReadOnly={!!globalProfile}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup label={t("common:description")} fieldId="kc-description">
|
2022-05-11 09:46:14 +00:00
|
|
|
<KeycloakTextArea
|
2021-10-26 20:16:19 +00:00
|
|
|
ref={register()}
|
|
|
|
name="description"
|
|
|
|
type="text"
|
|
|
|
id="description"
|
|
|
|
aria-label={t("description")}
|
|
|
|
data-testid="client-profile-description"
|
|
|
|
isReadOnly={!!globalProfile}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<ActionGroup>
|
|
|
|
{!globalProfile && (
|
|
|
|
<Button
|
|
|
|
variant="primary"
|
|
|
|
onClick={() => handleSubmit(save)()}
|
|
|
|
data-testid="saveCreateProfile"
|
|
|
|
isDisabled={!isDirty}
|
|
|
|
>
|
|
|
|
{t("common:save")}
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
{editMode && !globalProfile && (
|
|
|
|
<Button
|
|
|
|
id={"reloadProfile"}
|
|
|
|
variant="link"
|
|
|
|
data-testid={"reloadProfile"}
|
|
|
|
isDisabled={!isDirty}
|
|
|
|
onClick={reload}
|
|
|
|
>
|
|
|
|
{t("realm-settings:reload")}
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
{!editMode && !globalProfile && (
|
|
|
|
<Button
|
|
|
|
id={"cancelCreateProfile"}
|
2022-08-08 21:45:28 +00:00
|
|
|
variant="link"
|
2021-10-26 20:16:19 +00:00
|
|
|
component={(props) => (
|
|
|
|
<Link
|
|
|
|
{...props}
|
2022-02-08 22:10:46 +00:00
|
|
|
to={toClientPolicies({ realm, tab: "profiles" })}
|
2021-10-26 20:16:19 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
data-testid={"cancelCreateProfile"}
|
|
|
|
>
|
|
|
|
{t("common:cancel")}
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
</ActionGroup>
|
|
|
|
{editMode && (
|
|
|
|
<>
|
|
|
|
<Flex>
|
|
|
|
<FlexItem>
|
|
|
|
<Text className="kc-executors" component={TextVariants.h1}>
|
|
|
|
{t("executors")}
|
|
|
|
<HelpItem
|
2021-12-14 14:56:36 +00:00
|
|
|
helpText="realm-settings:executorsHelpText"
|
|
|
|
fieldLabelId="realm-settings:executors"
|
2021-10-26 20:16:19 +00:00
|
|
|
/>
|
|
|
|
</Text>
|
|
|
|
</FlexItem>
|
|
|
|
{profile && (
|
|
|
|
<FlexItem align={{ default: "alignRight" }}>
|
|
|
|
<Button
|
|
|
|
id="addExecutor"
|
|
|
|
component={(props) => (
|
|
|
|
<Link
|
|
|
|
{...props}
|
|
|
|
to={toAddExecutor({
|
|
|
|
realm,
|
|
|
|
profileName,
|
|
|
|
})}
|
2021-11-10 14:26:43 +00:00
|
|
|
/>
|
2021-10-26 20:16:19 +00:00
|
|
|
)}
|
|
|
|
variant="link"
|
|
|
|
className="kc-addExecutor"
|
2021-11-01 10:58:31 +00:00
|
|
|
data-testid="addExecutor"
|
2021-10-26 20:16:19 +00:00
|
|
|
icon={<PlusCircleIcon />}
|
|
|
|
>
|
|
|
|
{t("realm-settings:addExecutor")}
|
|
|
|
</Button>
|
|
|
|
</FlexItem>
|
|
|
|
)}
|
|
|
|
</Flex>
|
|
|
|
{profileExecutors.length > 0 && (
|
|
|
|
<DataList aria-label={t("executors")} isCompact>
|
|
|
|
{profileExecutors.map((executor, idx) => (
|
|
|
|
<DataListItem
|
|
|
|
aria-labelledby={"executors-list-item"}
|
|
|
|
key={executor.executor}
|
|
|
|
id={executor.executor}
|
|
|
|
>
|
|
|
|
<DataListItemRow data-testid="executors-list-row">
|
|
|
|
<DataListItemCells
|
|
|
|
dataListCells={[
|
|
|
|
<DataListCell
|
|
|
|
key="executor"
|
|
|
|
data-testid="executor-type"
|
|
|
|
>
|
2021-11-10 14:26:43 +00:00
|
|
|
{executor.configuration ? (
|
|
|
|
<Button
|
|
|
|
component={(props) => (
|
|
|
|
<Link
|
|
|
|
{...props}
|
|
|
|
to={toExecutor({
|
|
|
|
realm,
|
|
|
|
profileName,
|
|
|
|
executorName: executor.executor!,
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
variant="link"
|
|
|
|
data-testid="editExecutor"
|
2021-10-26 20:16:19 +00:00
|
|
|
>
|
|
|
|
{executor.executor}
|
2021-11-10 14:26:43 +00:00
|
|
|
</Button>
|
2021-10-26 20:16:19 +00:00
|
|
|
) : (
|
2021-11-10 14:26:43 +00:00
|
|
|
<span className="kc-unclickable-executor">
|
|
|
|
{executor.executor}
|
|
|
|
</span>
|
2021-10-26 20:16:19 +00:00
|
|
|
)}
|
|
|
|
{executorTypes
|
|
|
|
?.filter(
|
|
|
|
(type) => type.id === executor.executor
|
|
|
|
)
|
|
|
|
.map((type) => (
|
2022-01-31 07:19:44 +00:00
|
|
|
<Fragment key={type.id}>
|
2021-10-26 20:16:19 +00:00
|
|
|
<HelpItem
|
|
|
|
key={type.id}
|
|
|
|
helpText={type.helpText}
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId="realm-settings:executorTypeTextHelpText"
|
2021-10-26 20:16:19 +00:00
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
variant="link"
|
|
|
|
isInline
|
|
|
|
icon={
|
|
|
|
<TrashIcon
|
|
|
|
key={`executorType-trash-icon-${type.id}`}
|
|
|
|
className="kc-executor-trash-icon"
|
|
|
|
data-testid="deleteExecutor"
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
onClick={() => {
|
|
|
|
toggleDeleteDialog();
|
|
|
|
setExecutorToDelete({
|
|
|
|
idx: idx,
|
|
|
|
name: type.id,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
></Button>
|
2022-01-31 07:19:44 +00:00
|
|
|
</Fragment>
|
2021-10-26 20:16:19 +00:00
|
|
|
))}
|
|
|
|
</DataListCell>,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</DataListItemRow>
|
|
|
|
</DataListItem>
|
|
|
|
))}
|
|
|
|
</DataList>
|
|
|
|
)}
|
|
|
|
{globalProfileExecutors.length > 0 && (
|
|
|
|
<>
|
|
|
|
<DataList aria-label={t("executors")} isCompact>
|
|
|
|
{globalProfileExecutors.map((executor) => (
|
|
|
|
<DataListItem
|
|
|
|
aria-labelledby={"global-executors-list-item"}
|
|
|
|
key={executor.executor}
|
|
|
|
id={executor.executor}
|
|
|
|
>
|
|
|
|
<DataListItemRow data-testid="global-executors-list-row">
|
|
|
|
<DataListItemCells
|
|
|
|
dataListCells={[
|
|
|
|
<DataListCell
|
|
|
|
key="executor"
|
|
|
|
data-testid="global-executor-type"
|
|
|
|
>
|
2021-11-10 14:26:43 +00:00
|
|
|
{Object.keys(executor.configuration!).length !==
|
|
|
|
0 ? (
|
|
|
|
<Button
|
|
|
|
component={(props) => (
|
|
|
|
<Link
|
|
|
|
{...props}
|
|
|
|
to={toExecutor({
|
|
|
|
realm,
|
|
|
|
profileName,
|
|
|
|
executorName: executor.executor!,
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
variant="link"
|
|
|
|
data-testid="editExecutor"
|
2021-10-26 20:16:19 +00:00
|
|
|
>
|
|
|
|
{executor.executor}
|
2021-11-10 14:26:43 +00:00
|
|
|
</Button>
|
2021-10-26 20:16:19 +00:00
|
|
|
) : (
|
2021-11-10 14:26:43 +00:00
|
|
|
<span className="kc-unclickable-executor">
|
|
|
|
{executor.executor}
|
|
|
|
</span>
|
2021-10-26 20:16:19 +00:00
|
|
|
)}
|
|
|
|
{executorTypes
|
|
|
|
?.filter(
|
|
|
|
(type) => type.id === executor.executor
|
|
|
|
)
|
|
|
|
.map((type) => (
|
|
|
|
<HelpItem
|
|
|
|
key={type.id}
|
|
|
|
helpText={type.helpText}
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId="realm-settings:executorTypeTextHelpText"
|
2021-10-26 20:16:19 +00:00
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</DataListCell>,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</DataListItemRow>
|
|
|
|
</DataListItem>
|
|
|
|
))}
|
|
|
|
</DataList>
|
|
|
|
<Button
|
|
|
|
id="backToClientPolicies"
|
|
|
|
component={(props) => (
|
|
|
|
<Link
|
|
|
|
{...props}
|
2022-02-08 22:10:46 +00:00
|
|
|
to={toClientPolicies({ realm, tab: "profiles" })}
|
2021-10-26 20:16:19 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
variant="primary"
|
|
|
|
className="kc-backToPolicies"
|
|
|
|
data-testid="backToClientPolicies"
|
|
|
|
>
|
|
|
|
{t("realm-settings:back")}
|
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{profileExecutors.length === 0 &&
|
|
|
|
globalProfileExecutors.length === 0 && (
|
|
|
|
<>
|
|
|
|
<Divider />
|
|
|
|
<Text
|
|
|
|
className="kc-emptyExecutors"
|
|
|
|
component={TextVariants.h6}
|
|
|
|
>
|
|
|
|
{t("realm-settings:emptyExecutors")}
|
|
|
|
</Text>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</FormAccess>
|
|
|
|
</PageSection>
|
|
|
|
</>
|
|
|
|
);
|
2021-10-29 16:11:06 +00:00
|
|
|
}
|