/* eslint-disable */ // @ts-nocheck import type ComponentRepresentation from "@keycloak/keycloak-admin-client/lib/defs/componentRepresentation"; import type ComponentTypeRepresentation from "@keycloak/keycloak-admin-client/lib/defs/componentTypeRepresentation"; import { DirectionType } from "@keycloak/keycloak-admin-client/lib/resources/userStorageProvider"; import { HelpItem, KeycloakSelect, KeycloakSpinner, SelectVariant, TextControl, useAlerts, useFetch, } from "../../../../shared/keycloak-ui-shared"; import { ActionGroup, AlertVariant, Button, ButtonVariant, DropdownItem, FormGroup, PageSection, SelectOption, } from "../../../../shared/@patternfly/react-core"; import { useState } from "react"; import { Controller, FormProvider, useForm, useWatch } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { useNavigate } from "react-router-dom"; import { useAdminClient } from "../../../admin-client"; import { useConfirmDialog } from "../../../components/confirm-dialog/ConfirmDialog"; import { convertToName, DynamicComponents, } from "../../../components/dynamic/DynamicComponents"; import { FormAccess } from "../../../components/form/FormAccess"; import { ViewHeader } from "../../../components/view-header/ViewHeader"; import { useRealm } from "../../../context/realm-context/RealmContext"; import { beerify, convertFormValuesToObject, convertToFormValues, } from "../../../util"; import { useParams } from "../../../utils/useParams"; import { toUserFederationLdap } from "../../routes/UserFederationLdap"; import { UserFederationLdapMapperParams } from "../../routes/UserFederationLdapMapper"; export default function LdapMapperDetails() { const { adminClient } = useAdminClient(); const form = useForm(); const [mapping, setMapping] = useState(); const [components, setComponents] = useState(); const { id, mapperId } = useParams(); const navigate = useNavigate(); const { realm } = useRealm(); const { t } = useTranslation(); const { addAlert, addError } = useAlerts(); const [isMapperDropdownOpen, setIsMapperDropdownOpen] = useState(false); const [mapperTypeFilter, setMapperTypeFilter] = useState(""); const [key, setKey] = useState(0); const refresh = () => setKey(key + 1); useFetch( async () => { const components = await adminClient.components.listSubComponents({ id, type: "org.keycloak.storage.ldap.mappers.LDAPStorageMapper", }); if (mapperId && mapperId !== "new") { const fetchedMapper = await adminClient.components.findOne({ id: mapperId, }); return { components, fetchedMapper }; } return { components }; }, ({ components, fetchedMapper }) => { setMapping(fetchedMapper); setComponents(components); if (mapperId !== "new" && !fetchedMapper) throw new Error(t("notFound")); if (fetchedMapper) setupForm(fetchedMapper); }, [], ); const setupForm = (mapper: ComponentRepresentation) => { convertToFormValues(mapper, form.setValue); }; const save = async (mapper: ComponentRepresentation) => { const component: ComponentRepresentation = convertFormValuesToObject(mapper); const map = { ...component, config: Object.entries(component.config || {}).reduce( (result, [key, value]) => { result[key] = Array.isArray(value) ? value : [value]; return result; }, {} as Record, ), }; try { if (mapperId === "new") { await adminClient.components.create(map); navigate( toUserFederationLdap({ realm, id: mapper.parentId!, tab: "mappers" }), ); } else { await adminClient.components.update({ id: mapperId }, map); } setupForm(map as ComponentRepresentation); addAlert( t( mapperId === "new" ? "mappingCreatedSuccess" : "mappingUpdatedSuccess", ), AlertVariant.success, ); } catch (error) { addError( mapperId === "new" ? "mappingCreatedError" : "mappingUpdatedError", error, ); } }; const sync = async (direction: DirectionType) => { try { const result = await adminClient.userStorageProvider.mappersSync({ parentId: mapping?.parentId || "", id: mapperId, direction, }); addAlert( t("syncLDAPGroupsSuccessful", { result: result.status, }), ); } catch (error) { addError("syncLDAPGroupsError", error); } refresh(); }; const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({ titleKey: "deleteMappingTitle", messageKey: "deleteMappingConfirm", continueButtonLabel: "delete", continueButtonVariant: ButtonVariant.danger, onConfirm: async () => { try { await adminClient.components.del({ id: mapping!.id!, }); addAlert(t("mappingDeletedSuccess"), AlertVariant.success); navigate(toUserFederationLdap({ id, realm, tab: "mappers" })); } catch (error) { addError("mappingDeletedError", error); } }, }); const mapperType = useWatch({ control: form.control, name: "providerId", }); const config = useWatch({ control: form.control, name: "config", }); const mapper = components?.find((c) => c.id === mapperType); const realmRolesValue = config?.[beerify("use.realm.roles.mapping")]; const isRealmMapping = realmRolesValue === undefined || realmRolesValue === "true"; const visibleProperties = mapperType === "role-ldap-mapper" && isRealmMapping ? (mapper?.properties ?? []).filter((p) => p.name !== "client.id") : (mapper?.properties ?? []); const selectItems = () => (components || []) .filter((c) => c.id.includes(mapperTypeFilter)) .map((c) => ( {c.id} )); if (!components) { return ; } const isNew = mapperId === "new"; return ( <> {t("delete")} , ...(mapper?.metadata.fedToKeycloakSyncSupported ? [ sync("fedToKeycloak")} > {t(mapper.metadata.fedToKeycloakSyncMessage)} , ] : []), ...(mapper?.metadata.keycloakToFedSyncSupported ? [ { await sync("keycloakToFed"); }} > {t(mapper.metadata.keycloakToFedSyncMessage)} , ] : []), ] } /> save(form.getValues()))} > {!isNew && } {!isNew ? ( ) : ( } fieldId="kc-providerId" isRequired > ( { setMapperTypeFilter(search); return selectItems(); }} onSelect={(value) => { setupForm({ providerId: value as string, ...Object.fromEntries( components .find((c) => c.id === value) ?.properties.filter((m) => m.type === "List") .map((m) => [ convertToName(m.name!), m.options?.[0], ]) || [], ), }); }} selections={field.value} variant={SelectVariant.typeahead} aria-label={t("selectMapperType")} > {selectItems()} )} > )} {!!mapperType && ( )} ); }