2023-01-26 09:31:07 +00:00
|
|
|
import type { ConfigPropertyRepresentation } from "@keycloak/keycloak-admin-client/lib/defs/authenticatorConfigInfoRepresentation";
|
|
|
|
import type ClientPolicyConditionRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientPolicyConditionRepresentation";
|
|
|
|
import type ClientPolicyRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientPolicyRepresentation";
|
|
|
|
import type ComponentTypeRepresentation from "@keycloak/keycloak-admin-client/lib/defs/componentTypeRepresentation";
|
2021-10-21 18:40:02 +00:00
|
|
|
import {
|
|
|
|
ActionGroup,
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
FormGroup,
|
|
|
|
PageSection,
|
|
|
|
Select,
|
|
|
|
SelectOption,
|
|
|
|
SelectVariant,
|
|
|
|
} from "@patternfly/react-core";
|
2023-01-26 09:31:07 +00:00
|
|
|
import { camelCase } from "lodash-es";
|
|
|
|
import { useState } from "react";
|
|
|
|
import { Controller, FormProvider, useForm } from "react-hook-form";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { useNavigate, useParams } from "react-router-dom";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { HelpItem } from "ui-shared";
|
2021-10-21 18:40:02 +00:00
|
|
|
|
2023-05-03 13:51:02 +00:00
|
|
|
import { adminClient } from "../admin-client";
|
2023-01-26 09:31:07 +00:00
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
|
|
|
import { DynamicComponents } from "../components/dynamic/DynamicComponents";
|
2023-05-24 12:11:06 +00:00
|
|
|
import { FormAccess } from "../components/form/FormAccess";
|
2023-01-26 09:31:07 +00:00
|
|
|
import { FormPanel } from "../components/scroll-form/FormPanel";
|
2021-10-21 18:40:02 +00:00
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
2023-01-26 09:31:07 +00:00
|
|
|
import { useServerInfo } from "../context/server-info/ServerInfoProvider";
|
2023-05-03 15:40:27 +00:00
|
|
|
import { useFetch } from "../utils/useFetch";
|
2022-10-06 10:29:16 +00:00
|
|
|
import { toEditClientPolicy } from "./routes/EditClientPolicy";
|
2021-11-09 18:27:25 +00:00
|
|
|
import type { EditClientPolicyConditionParams } from "./routes/EditCondition";
|
2022-02-21 16:30:57 +00:00
|
|
|
|
2021-11-01 22:18:27 +00:00
|
|
|
export type ItemType = { value: string };
|
2021-10-21 18:40:02 +00:00
|
|
|
|
2021-11-22 12:41:43 +00:00
|
|
|
type ConfigProperty = ConfigPropertyRepresentation & {
|
2023-01-26 09:31:07 +00:00
|
|
|
conditions: any;
|
2021-11-22 12:41:43 +00:00
|
|
|
config: any;
|
|
|
|
};
|
|
|
|
|
2021-10-29 16:11:06 +00:00
|
|
|
export default function NewClientPolicyCondition() {
|
2023-09-08 13:17:17 +00:00
|
|
|
const { t } = useTranslation();
|
2021-10-21 18:40:02 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
2022-08-16 13:09:14 +00:00
|
|
|
const navigate = useNavigate();
|
2021-10-21 18:40:02 +00:00
|
|
|
const { realm } = useRealm();
|
|
|
|
|
|
|
|
const [openConditionType, setOpenConditionType] = useState(false);
|
|
|
|
const [policies, setPolicies] = useState<ClientPolicyRepresentation[]>([]);
|
2021-11-23 14:59:04 +00:00
|
|
|
|
2021-10-21 18:40:02 +00:00
|
|
|
const [condition, setCondition] = useState<
|
|
|
|
ClientPolicyConditionRepresentation[]
|
|
|
|
>([]);
|
2021-11-09 18:27:25 +00:00
|
|
|
const [conditionData, setConditionData] =
|
|
|
|
useState<ClientPolicyConditionRepresentation>();
|
2021-10-21 18:40:02 +00:00
|
|
|
const [conditionType, setConditionType] = useState("");
|
2021-11-01 22:18:27 +00:00
|
|
|
const [conditionProperties, setConditionProperties] = useState<
|
|
|
|
ConfigPropertyRepresentation[]
|
|
|
|
>([]);
|
2021-10-21 18:40:02 +00:00
|
|
|
|
2022-10-06 10:29:16 +00:00
|
|
|
const { policyName, conditionName } =
|
|
|
|
useParams<EditClientPolicyConditionParams>();
|
2021-10-21 18:40:02 +00:00
|
|
|
|
|
|
|
const serverInfo = useServerInfo();
|
2023-01-26 09:31:07 +00:00
|
|
|
const form = useForm<ConfigProperty>();
|
2021-10-21 18:40:02 +00:00
|
|
|
|
|
|
|
const conditionTypes =
|
|
|
|
serverInfo.componentTypes?.[
|
|
|
|
"org.keycloak.services.clientpolicy.condition.ClientPolicyConditionProvider"
|
|
|
|
];
|
|
|
|
|
2022-02-21 16:30:57 +00:00
|
|
|
const setupForm = (condition: ClientPolicyConditionRepresentation) => {
|
|
|
|
form.reset({ config: condition.configuration || {} });
|
2021-11-09 18:27:25 +00:00
|
|
|
};
|
|
|
|
|
2021-10-21 18:40:02 +00:00
|
|
|
useFetch(
|
|
|
|
() => adminClient.clientPolicies.listPolicies(),
|
2021-11-23 14:59:04 +00:00
|
|
|
|
2021-10-21 18:40:02 +00:00
|
|
|
(policies) => {
|
|
|
|
setPolicies(policies.policies ?? []);
|
2021-11-23 14:59:04 +00:00
|
|
|
|
2021-11-09 18:27:25 +00:00
|
|
|
if (conditionName) {
|
|
|
|
const currentPolicy = policies.policies?.find(
|
2023-07-11 14:03:21 +00:00
|
|
|
(item) => item.name === policyName,
|
2021-11-09 18:27:25 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const typeAndConfigData = currentPolicy?.conditions?.find(
|
2023-07-11 14:03:21 +00:00
|
|
|
(item) => item.condition === conditionName,
|
2021-11-09 18:27:25 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const currentCondition = conditionTypes?.find(
|
2023-07-11 14:03:21 +00:00
|
|
|
(condition) => condition.id === conditionName,
|
2021-11-09 18:27:25 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
setConditionData(typeAndConfigData!);
|
|
|
|
setConditionProperties(currentCondition?.properties!);
|
2022-02-21 16:30:57 +00:00
|
|
|
setupForm(typeAndConfigData!);
|
2021-11-09 18:27:25 +00:00
|
|
|
}
|
2021-10-21 18:40:02 +00:00
|
|
|
},
|
2023-07-11 14:03:21 +00:00
|
|
|
[],
|
2021-10-21 18:40:02 +00:00
|
|
|
);
|
|
|
|
|
2021-11-22 12:41:43 +00:00
|
|
|
const save = async (configPolicy: ConfigProperty) => {
|
|
|
|
const configValues = configPolicy.config;
|
2021-11-09 18:27:25 +00:00
|
|
|
|
2021-11-23 14:59:04 +00:00
|
|
|
const writeConfig = () => {
|
|
|
|
return conditionProperties.reduce((r: any, p) => {
|
2022-02-21 16:30:57 +00:00
|
|
|
r[p.name!] = configValues[p.name!];
|
2021-11-22 12:41:43 +00:00
|
|
|
return r;
|
|
|
|
}, {});
|
2021-11-23 14:59:04 +00:00
|
|
|
};
|
2021-11-09 18:27:25 +00:00
|
|
|
|
2021-11-01 22:18:27 +00:00
|
|
|
const updatedPolicies = policies.map((policy) => {
|
|
|
|
if (policy.name !== policyName) {
|
|
|
|
return policy;
|
|
|
|
}
|
|
|
|
|
2021-11-09 18:27:25 +00:00
|
|
|
let conditions = policy.conditions ?? [];
|
|
|
|
|
|
|
|
if (conditionName) {
|
|
|
|
const createdCondition = {
|
|
|
|
condition: conditionData?.condition,
|
|
|
|
configuration: writeConfig(),
|
|
|
|
};
|
|
|
|
|
|
|
|
const index = conditions.findIndex(
|
2023-07-11 14:03:21 +00:00
|
|
|
(condition) => conditionName === condition.condition,
|
2021-11-09 18:27:25 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (index === -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const newConditions = [
|
|
|
|
...conditions.slice(0, index),
|
|
|
|
createdCondition,
|
|
|
|
...conditions.slice(index + 1),
|
|
|
|
];
|
|
|
|
|
|
|
|
return {
|
|
|
|
...policy,
|
|
|
|
conditions: newConditions,
|
|
|
|
};
|
|
|
|
}
|
2021-11-01 22:18:27 +00:00
|
|
|
|
2021-11-09 18:27:25 +00:00
|
|
|
conditions = conditions.concat({
|
2021-11-01 22:18:27 +00:00
|
|
|
condition: condition[0].condition,
|
|
|
|
configuration: writeConfig(),
|
|
|
|
});
|
2021-10-21 18:40:02 +00:00
|
|
|
|
2021-11-01 22:18:27 +00:00
|
|
|
return {
|
|
|
|
...policy,
|
|
|
|
conditions,
|
|
|
|
};
|
2021-11-09 18:27:25 +00:00
|
|
|
}) as ClientPolicyRepresentation[];
|
2021-10-21 18:40:02 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await adminClient.clientPolicies.updatePolicy({
|
2021-11-01 22:18:27 +00:00
|
|
|
policies: updatedPolicies,
|
2021-10-21 18:40:02 +00:00
|
|
|
});
|
2021-11-01 22:18:27 +00:00
|
|
|
setPolicies(updatedPolicies);
|
2022-10-06 10:29:16 +00:00
|
|
|
navigate(toEditClientPolicy({ realm, policyName: policyName! }));
|
2021-10-21 18:40:02 +00:00
|
|
|
addAlert(
|
2021-11-09 18:27:25 +00:00
|
|
|
conditionName
|
2023-09-25 07:06:56 +00:00
|
|
|
? t("updateClientConditionSuccess")
|
|
|
|
: t("createClientConditionSuccess"),
|
2023-07-11 14:03:21 +00:00
|
|
|
AlertVariant.success,
|
2021-10-21 18:40:02 +00:00
|
|
|
);
|
|
|
|
} catch (error) {
|
2023-09-25 07:06:56 +00:00
|
|
|
addError("createClientConditionError", error);
|
2021-10-21 18:40:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<PageSection variant="light">
|
2021-11-09 18:27:25 +00:00
|
|
|
<FormPanel
|
|
|
|
className="kc-login-screen"
|
|
|
|
title={conditionName ? t("editCondition") : t("addCondition")}
|
|
|
|
>
|
2021-10-21 18:40:02 +00:00
|
|
|
<FormAccess
|
|
|
|
isHorizontal
|
|
|
|
role="manage-realm"
|
|
|
|
className="pf-u-mt-lg"
|
2021-11-22 12:41:43 +00:00
|
|
|
onSubmit={form.handleSubmit(save)}
|
2021-10-21 18:40:02 +00:00
|
|
|
>
|
|
|
|
<FormGroup
|
|
|
|
label={t("conditionType")}
|
|
|
|
fieldId="conditionType"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText={
|
|
|
|
conditionType
|
2023-09-20 08:52:43 +00:00
|
|
|
? `${camelCase(conditionType.replace(/-/g, " "))}Help`
|
|
|
|
: "conditionsHelp"
|
2021-10-21 18:40:02 +00:00
|
|
|
}
|
2023-09-25 07:06:56 +00:00
|
|
|
fieldLabelId="conditionType"
|
2021-10-21 18:40:02 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="conditions"
|
|
|
|
defaultValue={"any-client"}
|
2021-11-22 12:41:43 +00:00
|
|
|
control={form.control}
|
2023-01-26 09:31:07 +00:00
|
|
|
render={({ field }) => (
|
2021-10-21 18:40:02 +00:00
|
|
|
<Select
|
|
|
|
placeholderText={t("selectACondition")}
|
2021-11-22 12:41:43 +00:00
|
|
|
className="kc-conditionType-select"
|
2021-11-09 18:27:25 +00:00
|
|
|
data-testid="conditionType-select"
|
2021-10-21 18:40:02 +00:00
|
|
|
toggleId="provider"
|
2021-11-09 18:27:25 +00:00
|
|
|
isDisabled={!!conditionName}
|
2021-10-21 18:40:02 +00:00
|
|
|
onToggle={(toggle) => setOpenConditionType(toggle)}
|
|
|
|
onSelect={(_, value) => {
|
2023-01-26 09:31:07 +00:00
|
|
|
field.onChange(value);
|
2021-11-01 22:18:27 +00:00
|
|
|
setConditionProperties(
|
2023-07-11 14:03:21 +00:00
|
|
|
(value as ComponentTypeRepresentation).properties,
|
2021-11-01 22:18:27 +00:00
|
|
|
);
|
2021-10-21 18:40:02 +00:00
|
|
|
setConditionType((value as ComponentTypeRepresentation).id);
|
|
|
|
setCondition([
|
|
|
|
{
|
|
|
|
condition: (value as ComponentTypeRepresentation).id,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
setOpenConditionType(false);
|
|
|
|
}}
|
2021-11-09 18:27:25 +00:00
|
|
|
selections={conditionName ? conditionName : conditionType}
|
2021-10-21 18:40:02 +00:00
|
|
|
variant={SelectVariant.single}
|
|
|
|
aria-label={t("conditionType")}
|
|
|
|
isOpen={openConditionType}
|
|
|
|
>
|
|
|
|
{conditionTypes?.map((condition) => (
|
|
|
|
<SelectOption
|
2023-01-26 09:31:07 +00:00
|
|
|
selected={condition.id === field.value}
|
2021-10-21 18:40:02 +00:00
|
|
|
description={t(
|
2023-11-02 16:03:47 +00:00
|
|
|
camelCase(condition.id.replace(/-/g, " ")),
|
2021-10-21 18:40:02 +00:00
|
|
|
)}
|
|
|
|
key={condition.id}
|
|
|
|
value={condition}
|
|
|
|
>
|
|
|
|
{condition.id}
|
|
|
|
</SelectOption>
|
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
2021-11-22 12:41:43 +00:00
|
|
|
|
2021-11-01 22:18:27 +00:00
|
|
|
<FormProvider {...form}>
|
2022-02-21 16:30:57 +00:00
|
|
|
<DynamicComponents properties={conditionProperties} />
|
2021-11-01 22:18:27 +00:00
|
|
|
</FormProvider>
|
2021-10-21 18:40:02 +00:00
|
|
|
<ActionGroup>
|
|
|
|
<Button
|
|
|
|
variant="primary"
|
|
|
|
type="submit"
|
2021-11-09 18:27:25 +00:00
|
|
|
data-testid="addCondition-saveBtn"
|
|
|
|
isDisabled={conditionType === "" && !conditionName}
|
2021-10-21 18:40:02 +00:00
|
|
|
>
|
2023-09-14 09:01:15 +00:00
|
|
|
{conditionName ? t("save") : t("add")}
|
2021-10-21 18:40:02 +00:00
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
variant="link"
|
2021-11-09 18:27:25 +00:00
|
|
|
data-testid="addCondition-cancelBtn"
|
2021-10-21 18:40:02 +00:00
|
|
|
onClick={() =>
|
2022-10-06 10:29:16 +00:00
|
|
|
navigate(toEditClientPolicy({ realm, policyName: policyName! }))
|
2021-10-21 18:40:02 +00:00
|
|
|
}
|
|
|
|
>
|
2023-09-14 09:01:15 +00:00
|
|
|
{t("cancel")}
|
2021-10-21 18:40:02 +00:00
|
|
|
</Button>
|
|
|
|
</ActionGroup>
|
|
|
|
</FormAccess>
|
|
|
|
</FormPanel>
|
|
|
|
</PageSection>
|
|
|
|
);
|
2021-10-29 16:11:06 +00:00
|
|
|
}
|