added test ldap button

This commit is contained in:
Erik Jan de Wit 2021-05-06 15:03:25 +02:00 committed by mfrances
parent ac664a2f54
commit 8c1a1fade8
3 changed files with 49 additions and 6 deletions

View file

@ -167,7 +167,7 @@ const LdapSettingsHeader = ({
export const UserFederationLdapSettings = () => {
const { t } = useTranslation("user-federation");
const form = useForm<ComponentRepresentation>();
const form = useForm<ComponentRepresentation>({ mode: "onChange" });
const history = useHistory();
const adminClient = useAdminClient();
const { realm } = useRealm();

View file

@ -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<keyof TestLdapConnectionRepresentation> = [
"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 = ({
>
<Controller
name="config.authType[0]"
defaultValue=""
defaultValue="none"
control={form.control}
render={({ onChange, value }) => (
<Select
@ -311,9 +349,12 @@ export const LdapSettingsConnection = ({
)}
</FormGroup>
<FormGroup fieldId="kc-test-button">
{" "}
{/* TODO: whatever this button is supposed to do */}
<Button variant="secondary" id="kc-test-button">
<Button
isDisabled={!form.formState.isValid}
variant="secondary"
id="kc-test-button"
onClick={() => testLdap()}
>
{t("common:test")}
</Button>
</FormGroup>

View file

@ -84,6 +84,8 @@
"saveError": "User federation provider could not be saved: {error}",
"createSuccess": "User federation provider successfully created",
"createError": "User federation provider could not be created: {error}",
"testSuccess": "Successfully connected to LDAP",
"testError": "Error when trying to connect to LDAP. See server.log for details.",
"learnMore": "Learn more",
"addNewProvider": "Add new provider",