import { AlertVariant, Button, FormGroup, Select, SelectOption, SelectVariant, Switch, TextInput, ValidatedOptions, } from "@patternfly/react-core"; import { useTranslation } from "react-i18next"; import React, { useState } from "react"; import _ from "lodash"; import TestLdapConnectionRepresentation from "keycloak-admin/lib/defs/testLdapConnection"; import { HelpItem } from "../../components/help-enabler/HelpItem"; import { Controller, UseFormMethods, useWatch } from "react-hook-form"; import { FormAccess } from "../../components/form-access/FormAccess"; import { WizardSectionHeader } from "../../components/wizard-section-header/WizardSectionHeader"; import { PasswordInput } from "../../components/password-input/PasswordInput"; import { useAdminClient } from "../../context/auth/AdminClient"; import { useRealm } from "../../context/realm-context/RealmContext"; import { useAlerts } from "../../components/alert/Alerts"; export type LdapSettingsConnectionProps = { form: UseFormMethods; showSectionHeading?: boolean; showSectionDescription?: boolean; }; const testLdapProperties: Array = [ "connectionUrl", "bindDn", "bindCredential", "useTruststoreSpi", "connectionTimeout", "startTls", "authType", ]; export const LdapSettingsConnection = ({ form, showSectionHeading = false, showSectionDescription = false, }: LdapSettingsConnectionProps) => { const { t } = useTranslation("user-federation"); const { t: helpText } = useTranslation("user-federation-help"); const adminClient = useAdminClient(); const { realm } = useRealm(); const { addAlert } = useAlerts(); const testLdap = async () => { try { const settings: TestLdapConnectionRepresentation = {}; testLdapProperties.forEach((key) => { const value = _.get(form.getValues(), `config.${key}`); settings[key] = _.isArray(value) ? value[0] : ""; }); await adminClient.realms.testLDAPConnection( { realm }, { ...settings, action: "testConnection" } ); addAlert(t("testSuccess"), AlertVariant.success); } catch (error) { addAlert(t("testError"), AlertVariant.danger); console.error(error.response?.data?.errorMessage); } }; const [ isTruststoreSpiDropdownOpen, setIsTruststoreSpiDropdownOpen, ] = useState(false); const [isBindTypeDropdownOpen, setIsBindTypeDropdownOpen] = useState(false); const ldapBindType = useWatch({ control: form.control, name: "config.authType", }); return ( <> {showSectionHeading && ( )} } fieldId="kc-console-connection-url" isRequired > {form.errors.config && form.errors.config.connectionUrl && form.errors.config.connectionUrl[0] && (
{form.errors.config.connectionUrl[0].message}
)}
} fieldId="kc-enable-start-tls" hasNoPaddingTop > ( onChange([`${value}`])} isChecked={value[0] === "true"} label={t("common:on")} labelOff={t("common:off")} /> )} > } fieldId="kc-use-truststore-spi" > ( )} > } fieldId="kc-connection-pooling" hasNoPaddingTop > ( onChange([`${value}`])} isChecked={value[0] === "true"} label={t("common:on")} labelOff={t("common:off")} /> )} > } fieldId="kc-console-connection-timeout" > } fieldId="kc-bind-type" isRequired > ( )} > {_.isEqual(ldapBindType, ["simple"]) && ( <> } fieldId="kc-console-bind-dn" helperTextInvalid={t("validateBindDn")} validated={ form.errors.config?.bindDn ? ValidatedOptions.error : ValidatedOptions.default } isRequired > } fieldId="kc-console-bind-credentials" helperTextInvalid={t("validateBindCredentials")} validated={ form.errors.config?.bindCredential ? ValidatedOptions.error : ValidatedOptions.default } isRequired > )}
); };