import type RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation"; import { UnmanagedAttributePolicy, UserProfileConfig, } from "@keycloak/keycloak-admin-client/lib/defs/userProfileMetadata"; import { ActionGroup, Button, ClipboardCopy, FormGroup, PageSection, Stack, StackItem, } from "@patternfly/react-core"; import { useEffect, useState } from "react"; import { Controller, FormProvider, useForm } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { FormErrorText, HelpItem, SelectControl, TextControl, } from "@keycloak/keycloak-ui-shared"; import { useAdminClient } from "../admin-client"; import { DefaultSwitchControl } from "../components/SwitchControl"; import { FormattedLink } from "../components/external-link/FormattedLink"; import { FormAccess } from "../components/form/FormAccess"; import { KeyValueInput } from "../components/key-value-form/KeyValueInput"; import { KeycloakSpinner } from "../components/keycloak-spinner/KeycloakSpinner"; import { useRealm } from "../context/realm-context/RealmContext"; import { addTrailingSlash, convertAttributeNameToForm, convertToFormValues, } from "../util"; import { useFetch } from "../utils/useFetch"; import { UIRealmRepresentation } from "./RealmSettingsTabs"; type RealmSettingsGeneralTabProps = { realm: UIRealmRepresentation; save: (realm: UIRealmRepresentation) => void; }; export const RealmSettingsGeneralTab = ({ realm, save, }: RealmSettingsGeneralTabProps) => { const { adminClient } = useAdminClient(); const { realm: realmName } = useRealm(); const [userProfileConfig, setUserProfileConfig] = useState(); useFetch( () => adminClient.users.getProfile({ realm: realmName }), (config) => setUserProfileConfig(config), [], ); if (!userProfileConfig) { return ; } return ( ); }; type RealmSettingsGeneralTabFormProps = { realm: UIRealmRepresentation; save: (realm: UIRealmRepresentation) => void; userProfileConfig: UserProfileConfig; }; type FormFields = Omit & { unmanagedAttributePolicy: UnmanagedAttributePolicy; }; const REQUIRE_SSL_TYPES = ["all", "external", "none"]; const UNMANAGED_ATTRIBUTE_POLICIES = [ UnmanagedAttributePolicy.Disabled, UnmanagedAttributePolicy.Enabled, UnmanagedAttributePolicy.AdminView, UnmanagedAttributePolicy.AdminEdit, ]; function RealmSettingsGeneralTabForm({ realm, save, userProfileConfig, }: RealmSettingsGeneralTabFormProps) { const { adminClient } = useAdminClient(); const { t } = useTranslation(); const { realm: realmName } = useRealm(); const form = useForm(); const { control, handleSubmit, setValue, formState: { isDirty, errors }, } = form; const setupForm = () => { convertToFormValues(realm, setValue); if (realm.attributes?.["acr.loa.map"]) { const result = Object.entries( JSON.parse(realm.attributes["acr.loa.map"]), ).flatMap(([key, value]) => ({ key, value })); result.concat({ key: "", value: "" }); setValue( convertAttributeNameToForm("attributes.acr.loa.map") as any, result, ); } }; useEffect(setupForm, []); const onSubmit = handleSubmit(({ unmanagedAttributePolicy, ...data }) => { const upConfig = { ...userProfileConfig }; if (unmanagedAttributePolicy === UnmanagedAttributePolicy.Disabled) { delete upConfig.unmanagedAttributePolicy; } else { upConfig.unmanagedAttributePolicy = unmanagedAttributePolicy; } save({ ...data, upConfig }); }); return ( ( {field.value} )} /> {errors.realm && ( )} ({ key: sslType, value: t(`sslType.${sslType}`), }))} /> } > ({ key: policy, value: t(`unmanagedAttributePolicy.${policy}`), }))} /> } fieldId="kc-endpoints" > ); }