From 8c1a1fade8faa80fbee0e16b226cbf34cf773a43 Mon Sep 17 00:00:00 2001 From: Erik Jan de Wit Date: Thu, 6 May 2021 15:03:25 +0200 Subject: [PATCH] added test ldap button --- .../UserFederationLdapSettings.tsx | 2 +- .../ldap/LdapSettingsConnection.tsx | 51 +++++++++++++++++-- src/user-federation/messages.json | 2 + 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/user-federation/UserFederationLdapSettings.tsx b/src/user-federation/UserFederationLdapSettings.tsx index 56dd2540cf..be8e3d25a7 100644 --- a/src/user-federation/UserFederationLdapSettings.tsx +++ b/src/user-federation/UserFederationLdapSettings.tsx @@ -167,7 +167,7 @@ const LdapSettingsHeader = ({ export const UserFederationLdapSettings = () => { const { t } = useTranslation("user-federation"); - const form = useForm(); + const form = useForm({ mode: "onChange" }); const history = useHistory(); const adminClient = useAdminClient(); const { realm } = useRealm(); diff --git a/src/user-federation/ldap/LdapSettingsConnection.tsx b/src/user-federation/ldap/LdapSettingsConnection.tsx index f3ef2eb912..4851741070 100644 --- a/src/user-federation/ldap/LdapSettingsConnection.tsx +++ b/src/user-federation/ldap/LdapSettingsConnection.tsx @@ -1,4 +1,5 @@ import { + AlertVariant, Button, FormGroup, Select, @@ -10,11 +11,16 @@ import { 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; @@ -22,13 +28,45 @@ export type LdapSettingsConnectionProps = { 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 helpText = useTranslation("user-federation-help").t; + 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, @@ -219,7 +257,7 @@ export const LdapSettingsConnection = ({ > (