2022-12-07 14:23:12 +00:00
|
|
|
import type { ConfigPropertyRepresentation } from "@keycloak/keycloak-admin-client/lib/defs/authenticatorConfigInfoRepresentation";
|
|
|
|
import type ClientProfileRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientProfileRepresentation";
|
|
|
|
import type ComponentTypeRepresentation from "@keycloak/keycloak-admin-client/lib/defs/componentTypeRepresentation";
|
2021-10-26 20:16:19 +00:00
|
|
|
import {
|
|
|
|
ActionGroup,
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
FormGroup,
|
|
|
|
PageSection,
|
|
|
|
Select,
|
|
|
|
SelectOption,
|
|
|
|
SelectVariant,
|
|
|
|
} from "@patternfly/react-core";
|
2022-12-07 14:23:12 +00:00
|
|
|
import { useState } from "react";
|
|
|
|
import { Controller, FormProvider, useForm } from "react-hook-form";
|
2021-10-26 20:16:19 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2022-12-07 14:23:12 +00:00
|
|
|
import { Link, useNavigate } from "react-router-dom-v5-compat";
|
|
|
|
|
2021-10-26 20:16:19 +00:00
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
2022-12-07 14:23:12 +00:00
|
|
|
import { DynamicComponents } from "../components/dynamic/DynamicComponents";
|
|
|
|
import { FormAccess } from "../components/form-access/FormAccess";
|
2021-10-26 20:16:19 +00:00
|
|
|
import { HelpItem } from "../components/help-enabler/HelpItem";
|
2022-12-07 14:23:12 +00:00
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
2021-10-26 20:16:19 +00:00
|
|
|
import { useAdminClient, useFetch } from "../context/auth/AdminClient";
|
2022-12-07 14:23:12 +00:00
|
|
|
import { useServerInfo } from "../context/server-info/ServerInfoProvider";
|
|
|
|
import { useParams } from "../utils/useParams";
|
2021-11-02 17:11:04 +00:00
|
|
|
import { ClientProfileParams, toClientProfile } from "./routes/ClientProfile";
|
2021-11-10 14:26:43 +00:00
|
|
|
import type { ExecutorParams } from "./routes/Executor";
|
2021-10-26 20:16:19 +00:00
|
|
|
|
2021-11-10 14:26:43 +00:00
|
|
|
type ExecutorForm = {
|
2022-09-05 21:31:39 +00:00
|
|
|
config?: object;
|
2021-11-10 14:26:43 +00:00
|
|
|
executor: string;
|
|
|
|
};
|
2021-10-26 20:16:19 +00:00
|
|
|
|
|
|
|
const defaultValues: ExecutorForm = {
|
2021-11-10 14:26:43 +00:00
|
|
|
config: {},
|
2021-10-26 20:16:19 +00:00
|
|
|
executor: "",
|
|
|
|
};
|
|
|
|
|
2021-10-29 16:11:06 +00:00
|
|
|
export default function ExecutorForm() {
|
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 { realm, profileName } = useParams<ClientProfileParams>();
|
2021-11-10 14:26:43 +00:00
|
|
|
const { executorName } = useParams<ExecutorParams>();
|
2021-10-26 20:16:19 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
|
|
|
const [selectExecutorTypeOpen, setSelectExecutorTypeOpen] = useState(false);
|
|
|
|
const serverInfo = useServerInfo();
|
2022-07-14 13:02:28 +00:00
|
|
|
const { adminClient } = useAdminClient();
|
2021-10-26 20:16:19 +00:00
|
|
|
const executorTypes =
|
|
|
|
serverInfo.componentTypes?.[
|
|
|
|
"org.keycloak.services.clientpolicy.executor.ClientPolicyExecutorProvider"
|
|
|
|
];
|
|
|
|
const [executors, setExecutors] = useState<ComponentTypeRepresentation[]>([]);
|
|
|
|
const [executorProperties, setExecutorProperties] = useState<
|
|
|
|
ConfigPropertyRepresentation[]
|
|
|
|
>([]);
|
|
|
|
const [globalProfiles, setGlobalProfiles] = useState<
|
|
|
|
ClientProfileRepresentation[]
|
|
|
|
>([]);
|
|
|
|
const [profiles, setProfiles] = useState<ClientProfileRepresentation[]>([]);
|
2022-03-07 14:27:10 +00:00
|
|
|
const form = useForm({ defaultValues });
|
2022-02-16 14:53:45 +00:00
|
|
|
const { control, reset, handleSubmit } = form;
|
2021-11-10 14:26:43 +00:00
|
|
|
const editMode = !!executorName;
|
2021-10-26 20:16:19 +00:00
|
|
|
|
2022-02-16 14:53:45 +00:00
|
|
|
const setupForm = (profiles: ClientProfileRepresentation[]) => {
|
|
|
|
const profile = profiles.find((profile) => profile.name === profileName);
|
|
|
|
const executor = profile?.executors?.find(
|
|
|
|
(executor) => executor.executor === executorName
|
|
|
|
);
|
|
|
|
if (executor) reset({ config: executor.configuration });
|
|
|
|
};
|
|
|
|
|
2021-10-26 20:16:19 +00:00
|
|
|
useFetch(
|
|
|
|
() =>
|
|
|
|
adminClient.clientPolicies.listProfiles({ includeGlobalProfiles: true }),
|
|
|
|
(profiles) => {
|
2022-02-16 14:53:45 +00:00
|
|
|
setGlobalProfiles(profiles.globalProfiles!);
|
|
|
|
setProfiles(profiles.profiles!);
|
2021-11-10 14:26:43 +00:00
|
|
|
|
2022-02-16 14:53:45 +00:00
|
|
|
setupForm(profiles.profiles!);
|
|
|
|
setupForm(profiles.globalProfiles!);
|
2021-10-26 20:16:19 +00:00
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
const save = async () => {
|
|
|
|
const formValues = form.getValues();
|
|
|
|
const updatedProfiles = profiles.map((profile) => {
|
|
|
|
if (profile.name !== profileName) {
|
|
|
|
return profile;
|
|
|
|
}
|
|
|
|
|
|
|
|
const executors = (profile.executors ?? []).concat({
|
|
|
|
executor: formValues.executor,
|
2022-09-05 21:31:39 +00:00
|
|
|
configuration: formValues.config || {},
|
2021-10-26 20:16:19 +00:00
|
|
|
});
|
|
|
|
|
2021-11-10 14:26:43 +00:00
|
|
|
if (editMode) {
|
2022-09-05 21:31:39 +00:00
|
|
|
const profileExecutor = profile.executors!.find(
|
|
|
|
(executor) => executor.executor === executorName
|
|
|
|
);
|
2021-11-10 14:26:43 +00:00
|
|
|
profileExecutor!.configuration = {
|
|
|
|
...profileExecutor!.configuration,
|
|
|
|
...formValues.config,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (editMode) {
|
|
|
|
return profile;
|
|
|
|
}
|
2021-10-26 20:16:19 +00:00
|
|
|
return {
|
|
|
|
...profile,
|
|
|
|
executors,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
await adminClient.clientPolicies.createProfiles({
|
|
|
|
profiles: updatedProfiles,
|
|
|
|
globalProfiles: globalProfiles,
|
|
|
|
});
|
2021-11-10 14:26:43 +00:00
|
|
|
addAlert(
|
|
|
|
editMode
|
|
|
|
? t("realm-settings:updateExecutorSuccess")
|
|
|
|
: t("realm-settings:addExecutorSuccess"),
|
|
|
|
AlertVariant.success
|
|
|
|
);
|
|
|
|
|
2022-08-16 13:09:14 +00:00
|
|
|
navigate(toClientProfile({ realm, profileName }));
|
2021-10-26 20:16:19 +00:00
|
|
|
} catch (error) {
|
2021-11-10 14:26:43 +00:00
|
|
|
addError(
|
|
|
|
editMode
|
|
|
|
? "realm-settings:updateExecutorError"
|
|
|
|
: "realm-settings:addExecutorError",
|
|
|
|
error
|
|
|
|
);
|
2021-10-26 20:16:19 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-11-10 14:26:43 +00:00
|
|
|
const globalProfile = globalProfiles.find(
|
|
|
|
(globalProfile) => globalProfile.name === profileName
|
|
|
|
);
|
|
|
|
|
|
|
|
const profileExecutorType = executorTypes?.find(
|
|
|
|
(executor) => executor.id === executorName
|
|
|
|
);
|
|
|
|
|
|
|
|
const editedProfileExecutors =
|
|
|
|
profileExecutorType?.properties.map<ConfigPropertyRepresentation>(
|
|
|
|
(property) => {
|
|
|
|
const globalDefaultValues = editMode ? property.defaultValue : "";
|
|
|
|
return {
|
|
|
|
...property,
|
|
|
|
defaultValue: globalDefaultValues,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-10-26 20:16:19 +00:00
|
|
|
return (
|
|
|
|
<>
|
2021-11-10 14:26:43 +00:00
|
|
|
<ViewHeader
|
|
|
|
titleKey={editMode ? executorName : t("addExecutor")}
|
|
|
|
divider
|
|
|
|
/>
|
2021-10-26 20:16:19 +00:00
|
|
|
<PageSection variant="light">
|
2022-02-16 14:53:45 +00:00
|
|
|
<FormAccess
|
|
|
|
isHorizontal
|
|
|
|
role="manage-realm"
|
|
|
|
className="pf-u-mt-lg"
|
|
|
|
isReadOnly={!!globalProfile}
|
|
|
|
>
|
2021-10-26 20:16:19 +00:00
|
|
|
<FormGroup
|
|
|
|
label={t("executorType")}
|
|
|
|
fieldId="kc-executorType"
|
|
|
|
labelIcon={
|
|
|
|
executors.length > 0 && executors[0].helpText! !== "" ? (
|
|
|
|
<HelpItem
|
|
|
|
helpText={executors[0].helpText}
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId="realm-settings:executorTypeHelpText"
|
2021-10-26 20:16:19 +00:00
|
|
|
/>
|
2021-11-10 14:26:43 +00:00
|
|
|
) : editMode ? (
|
|
|
|
<HelpItem
|
|
|
|
helpText={profileExecutorType?.helpText}
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId="realm-settings:executorTypeHelpText"
|
2021-11-10 14:26:43 +00:00
|
|
|
/>
|
2021-10-26 20:16:19 +00:00
|
|
|
) : undefined
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="executor"
|
|
|
|
defaultValue=""
|
|
|
|
control={control}
|
2022-03-07 14:27:10 +00:00
|
|
|
render={({ value }) => (
|
2021-10-26 20:16:19 +00:00
|
|
|
<Select
|
|
|
|
toggleId="kc-executor"
|
|
|
|
placeholderText="Select an executor"
|
|
|
|
onToggle={(isOpen) => setSelectExecutorTypeOpen(isOpen)}
|
|
|
|
onSelect={(_, value) => {
|
2022-03-07 14:27:10 +00:00
|
|
|
reset({ ...defaultValues, executor: value.toString() });
|
2021-10-26 20:16:19 +00:00
|
|
|
const selectedExecutor = executorTypes?.filter(
|
|
|
|
(type) => type.id === value
|
|
|
|
);
|
|
|
|
setExecutors(selectedExecutor ?? []);
|
|
|
|
setExecutorProperties(
|
|
|
|
selectedExecutor?.[0].properties ?? []
|
|
|
|
);
|
|
|
|
setSelectExecutorTypeOpen(false);
|
|
|
|
}}
|
2021-11-10 14:26:43 +00:00
|
|
|
selections={editMode ? executorName : value}
|
2021-10-26 20:16:19 +00:00
|
|
|
variant={SelectVariant.single}
|
|
|
|
data-testid="executorType-select"
|
|
|
|
aria-label={t("executorType")}
|
|
|
|
isOpen={selectExecutorTypeOpen}
|
|
|
|
maxHeight={580}
|
2021-11-10 14:26:43 +00:00
|
|
|
isDisabled={editMode}
|
2021-10-26 20:16:19 +00:00
|
|
|
>
|
|
|
|
{executorTypes?.map((option) => (
|
|
|
|
<SelectOption
|
|
|
|
selected={option.id === value}
|
|
|
|
key={option.id}
|
|
|
|
value={option.id}
|
|
|
|
description={option.helpText}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormProvider {...form}>
|
2022-03-07 14:27:10 +00:00
|
|
|
<DynamicComponents
|
|
|
|
properties={
|
|
|
|
editMode ? editedProfileExecutors! : executorProperties
|
|
|
|
}
|
|
|
|
/>
|
2021-10-26 20:16:19 +00:00
|
|
|
</FormProvider>
|
2021-11-10 14:26:43 +00:00
|
|
|
{!globalProfile && (
|
|
|
|
<ActionGroup>
|
|
|
|
<Button
|
|
|
|
variant="primary"
|
|
|
|
onClick={() => handleSubmit(save)()}
|
|
|
|
data-testid="addExecutor-saveBtn"
|
|
|
|
>
|
|
|
|
{editMode ? t("common:save") : t("common:add")}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
variant="link"
|
|
|
|
component={(props) => (
|
|
|
|
<Link
|
|
|
|
{...props}
|
|
|
|
to={toClientProfile({ realm, profileName })}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
data-testid="addExecutor-cancelBtn"
|
|
|
|
>
|
|
|
|
{t("common:cancel")}
|
|
|
|
</Button>
|
|
|
|
</ActionGroup>
|
|
|
|
)}
|
|
|
|
</FormAccess>
|
|
|
|
{editMode && globalProfile && (
|
|
|
|
<div className="kc-backToProfile">
|
2021-10-26 20:16:19 +00:00
|
|
|
<Button
|
|
|
|
component={(props) => (
|
2022-03-07 14:27:10 +00:00
|
|
|
<Link {...props} to={toClientProfile({ realm, profileName })} />
|
2021-10-26 20:16:19 +00:00
|
|
|
)}
|
2021-11-10 14:26:43 +00:00
|
|
|
variant="primary"
|
2021-10-26 20:16:19 +00:00
|
|
|
>
|
2021-11-10 14:26:43 +00:00
|
|
|
{t("realm-settings:back")}
|
2021-10-26 20:16:19 +00:00
|
|
|
</Button>
|
2021-11-10 14:26:43 +00:00
|
|
|
</div>
|
|
|
|
)}
|
2021-10-26 20:16:19 +00:00
|
|
|
</PageSection>
|
|
|
|
</>
|
|
|
|
);
|
2021-10-29 16:11:06 +00:00
|
|
|
}
|