import React, { useState } from "react"; import { ActionGroup, AlertVariant, Button, FormGroup, PageSection, Select, SelectOption, SelectVariant, } from "@patternfly/react-core"; import { useTranslation } from "react-i18next"; import { FormAccess } from "../components/form-access/FormAccess"; import { ViewHeader } from "../components/view-header/ViewHeader"; import { useAlerts } from "../components/alert/Alerts"; import { useServerInfo } from "../context/server-info/ServerInfoProvider"; import { Controller, FormProvider, useForm } from "react-hook-form"; import { HelpItem } from "../components/help-enabler/HelpItem"; import { Link, useHistory, useParams } from "react-router-dom"; import { useAdminClient, useFetch } from "../context/auth/AdminClient"; import type ComponentTypeRepresentation from "@keycloak/keycloak-admin-client/lib/defs/componentTypeRepresentation"; import type { ConfigPropertyRepresentation } from "@keycloak/keycloak-admin-client/lib/defs/authenticatorConfigInfoRepresentation"; import type ClientProfileRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientProfileRepresentation"; import type { ClientProfileParams } from "./routes/ClientProfile"; import { COMPONENTS, isValidComponentType, } from "../client-scopes/add/components/components"; import type ClientPolicyExecutorRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientPolicyExecutorRepresentation"; type ExecutorForm = Required; const defaultValues: ExecutorForm = { configuration: {}, executor: "", }; export default function ExecutorForm() { const { t } = useTranslation("realm-settings"); const history = useHistory(); const { realm, profileName } = useParams(); const { addAlert, addError } = useAlerts(); const [selectExecutorTypeOpen, setSelectExecutorTypeOpen] = useState(false); const serverInfo = useServerInfo(); const adminClient = useAdminClient(); const executorTypes = serverInfo.componentTypes?.[ "org.keycloak.services.clientpolicy.executor.ClientPolicyExecutorProvider" ]; const [executors, setExecutors] = useState([]); const [executorProperties, setExecutorProperties] = useState< ConfigPropertyRepresentation[] >([]); const [globalProfiles, setGlobalProfiles] = useState< ClientProfileRepresentation[] >([]); const [profiles, setProfiles] = useState([]); const form = useForm({ defaultValues }); const { control } = form; useFetch( () => adminClient.clientPolicies.listProfiles({ includeGlobalProfiles: true }), (profiles) => { setGlobalProfiles(profiles.globalProfiles ?? []); setProfiles(profiles.profiles ?? []); }, [] ); const fldNameFormatter = (name: string) => name.toLowerCase().trim().split(/\s+/).join("-"); 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, configuration: formValues.configuration, }); return { ...profile, executors, }; }); try { await adminClient.clientPolicies.createProfiles({ profiles: updatedProfiles, globalProfiles: globalProfiles, }); addAlert(t("realm-settings:addExecutorSuccess"), AlertVariant.success); history.push(`/${realm}/realm-settings/clientPolicies/${profileName}`); } catch (error) { addError("realm-settings:addExecutorError", error); } }; return ( <> 0 && executors[0].helpText! !== "" ? ( ) : undefined } > ( )} /> {executorProperties.map((option) => { const componentType = option.type!; if (isValidComponentType(componentType)) { const Component = COMPONENTS[componentType]; return ( ); } else { console.warn( `There is no editor registered for ${componentType}` ); } })} ); }