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"; import { ActionGroup, AlertVariant, Button, FormGroup, PageSection, Select, SelectOption, SelectVariant, } from "@patternfly/react-core"; 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"; import { FormPanel, HelpItem } from "ui-shared"; import { adminClient } from "../admin-client"; import { useAlerts } from "../components/alert/Alerts"; import { DynamicComponents } from "../components/dynamic/DynamicComponents"; import { FormAccess } from "../components/form/FormAccess"; import { useRealm } from "../context/realm-context/RealmContext"; import { useServerInfo } from "../context/server-info/ServerInfoProvider"; import { useFetch } from "../utils/useFetch"; import { toEditClientPolicy } from "./routes/EditClientPolicy"; import type { EditClientPolicyConditionParams } from "./routes/EditCondition"; export type ItemType = { value: string }; type ConfigProperty = ConfigPropertyRepresentation & { conditions: any; config: any; }; export default function NewClientPolicyCondition() { const { t } = useTranslation(); const { addAlert, addError } = useAlerts(); const navigate = useNavigate(); const { realm } = useRealm(); const [openConditionType, setOpenConditionType] = useState(false); const [policies, setPolicies] = useState([]); const [condition, setCondition] = useState< ClientPolicyConditionRepresentation[] >([]); const [conditionData, setConditionData] = useState(); const [conditionType, setConditionType] = useState(""); const [conditionProperties, setConditionProperties] = useState< ConfigPropertyRepresentation[] >([]); const { policyName, conditionName } = useParams(); const serverInfo = useServerInfo(); const form = useForm(); const conditionTypes = serverInfo.componentTypes?.[ "org.keycloak.services.clientpolicy.condition.ClientPolicyConditionProvider" ]; const setupForm = (condition: ClientPolicyConditionRepresentation) => { form.reset({ config: condition.configuration || {} }); }; useFetch( () => adminClient.clientPolicies.listPolicies(), (policies) => { setPolicies(policies.policies ?? []); if (conditionName) { const currentPolicy = policies.policies?.find( (item) => item.name === policyName, ); const typeAndConfigData = currentPolicy?.conditions?.find( (item) => item.condition === conditionName, ); const currentCondition = conditionTypes?.find( (condition) => condition.id === conditionName, ); setConditionData(typeAndConfigData!); setConditionProperties(currentCondition?.properties!); setupForm(typeAndConfigData!); } }, [], ); const save = async (configPolicy: ConfigProperty) => { const configValues = configPolicy.config; const writeConfig = () => { return conditionProperties.reduce((r: any, p) => { r[p.name!] = configValues[p.name!]; return r; }, {}); }; const updatedPolicies = policies.map((policy) => { if (policy.name !== policyName) { return policy; } let conditions = policy.conditions ?? []; if (conditionName) { const createdCondition = { condition: conditionData?.condition, configuration: writeConfig(), }; const index = conditions.findIndex( (condition) => conditionName === condition.condition, ); if (index === -1) { return; } const newConditions = [ ...conditions.slice(0, index), createdCondition, ...conditions.slice(index + 1), ]; return { ...policy, conditions: newConditions, }; } conditions = conditions.concat({ condition: condition[0].condition, configuration: writeConfig(), }); return { ...policy, conditions, }; }) as ClientPolicyRepresentation[]; try { await adminClient.clientPolicies.updatePolicy({ policies: updatedPolicies, }); setPolicies(updatedPolicies); navigate(toEditClientPolicy({ realm, policyName: policyName! })); addAlert( conditionName ? t("updateClientConditionSuccess") : t("createClientConditionSuccess"), AlertVariant.success, ); } catch (error) { addError("createClientConditionError", error); } }; return ( } > ( )} /> ); }