/* eslint-disable */ // @ts-nocheck import type TestLdapConnectionRepresentation from "@keycloak/keycloak-admin-client/lib/defs/testLdapConnection"; import { HelpItem, KeycloakSelect, PasswordControl, SelectControl, SelectVariant, TextControl, } from "../../../shared/keycloak-ui-shared"; import { AlertVariant, Button, FormGroup, SelectOption, Switch, } from "../../../shared/@patternfly/react-core"; import { get, isEqual } from "lodash-es"; import { useState } from "react"; import { Controller, FormProvider, UseFormReturn, useWatch, } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { useAdminClient } from "../../admin-client"; import { useAlerts } from "../../../shared/keycloak-ui-shared"; import { FormAccess } from "../../components/form/FormAccess"; import { WizardSectionHeader } from "../../components/wizard-section-header/WizardSectionHeader"; import { useRealm } from "../../context/realm-context/RealmContext"; export type LdapSettingsConnectionProps = { form: UseFormReturn; id?: string; showSectionHeading?: boolean; showSectionDescription?: boolean; }; const testLdapProperties: Array = [ "connectionUrl", "bindDn", "bindCredential", "useTruststoreSpi", "connectionTimeout", "startTls", "authType", ]; type TestTypes = "testConnection" | "testAuthentication"; export const convertFormToSettings = (form: UseFormReturn) => { const settings: TestLdapConnectionRepresentation = {}; testLdapProperties.forEach((key) => { const value = get(form.getValues(), `config.${key}`); settings[key] = Array.isArray(value) ? value[0] : ""; }); return settings; }; export const LdapSettingsConnection = ({ form, id, showSectionHeading = false, showSectionDescription = false, }: LdapSettingsConnectionProps) => { const { adminClient } = useAdminClient(); const { t } = useTranslation(); const { realm } = useRealm(); const { addAlert, addError } = useAlerts(); const edit = !!id; const testLdap = async (testType: TestTypes) => { try { const settings = convertFormToSettings(form); await adminClient.realms.testLDAPConnection( { realm }, { ...settings, action: testType, componentId: id }, ); addAlert(t("testSuccess"), AlertVariant.success); } catch (error) { addError("testError", error); } }; const [isBindTypeDropdownOpen, setIsBindTypeDropdownOpen] = useState(false); const ldapBindType = useWatch({ control: form.control, name: "config.authType", defaultValue: ["simple"], }); return ( {showSectionHeading && ( )} } fieldId="kc-enable-start-tls" hasNoPaddingTop > ( field.onChange([`${value}`])} isChecked={field.value[0] === "true"} label={t("on")} labelOff={t("off")} aria-label={t("enableStartTls")} /> )} /> } fieldId="kc-connection-pooling" hasNoPaddingTop > ( field.onChange([`${value}`])} isChecked={field.value[0] === "true"} label={t("on")} labelOff={t("off")} aria-label={t("connectionPooling")} /> )} /> } fieldId="kc-bind-type" isRequired > ( setIsBindTypeDropdownOpen(!isBindTypeDropdownOpen) } isOpen={isBindTypeDropdownOpen} onSelect={(value) => { field.onChange(value as string); setIsBindTypeDropdownOpen(false); }} selections={field.value} variant={SelectVariant.single} data-testid="ldap-bind-type" aria-label={t("selectBindType")} > simple none )} /> {isEqual(ldapBindType, ["simple"]) && ( <> )} ); };