@keycloakify/keycloak-admin-ui
Version:
Repackaged Keycloak Admin UI
68 lines • 6.58 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { HelpItem, KeycloakSelect, PasswordControl, SelectControl, SelectVariant, TextControl, } from "../../ui-shared";
import { AlertVariant, Button, FormGroup, SelectOption, Switch, } from "@patternfly/react-core";
import { get, isEqual } from "lodash-es";
import { useState } from "react";
import { Controller, FormProvider, useWatch, } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { useAdminClient } from "../../admin-client";
import { useAlerts } from "../../ui-shared";
import { FormAccess } from "../../components/form/FormAccess";
import { WizardSectionHeader } from "../../components/wizard-section-header/WizardSectionHeader";
import { useRealm } from "../../context/realm-context/RealmContext";
const testLdapProperties = [
"connectionUrl",
"bindDn",
"bindCredential",
"useTruststoreSpi",
"connectionTimeout",
"startTls",
"authType",
];
export const convertFormToSettings = (form) => {
const settings = {};
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, }) => {
const { adminClient } = useAdminClient();
const { t } = useTranslation();
const { realm } = useRealm();
const { addAlert, addError } = useAlerts();
const edit = !!id;
const testLdap = async (testType) => {
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 (_jsxs(FormProvider, { ...form, children: [showSectionHeading && (_jsx(WizardSectionHeader, { title: t("connectionAndAuthenticationSettings"), description: t("ldapConnectionAndAuthorizationSettingsDescription"), showDescription: showSectionDescription })), _jsxs(FormAccess, { role: "manage-realm", isHorizontal: true, children: [_jsx(TextControl, { name: "config.connectionUrl.0", label: t("connectionURL"), labelIcon: t("consoleDisplayConnectionUrlHelp"), type: "url", rules: {
required: t("validateConnectionUrl"),
} }), _jsx(FormGroup, { label: t("enableStartTls"), labelIcon: _jsx(HelpItem, { helpText: t("enableStartTlsHelp"), fieldLabelId: "enableStartTls" }), fieldId: "kc-enable-start-tls", hasNoPaddingTop: true, children: _jsx(Controller, { name: "config.startTls", defaultValue: ["false"], control: form.control, render: ({ field }) => (_jsx(Switch, { id: "kc-enable-start-tls", "data-testid": "enable-start-tls", isDisabled: false, onChange: (_event, value) => field.onChange([`${value}`]), isChecked: field.value[0] === "true", label: t("on"), labelOff: t("off"), "aria-label": t("enableStartTls") })) }) }), _jsx(SelectControl, { id: "useTruststoreSpi", name: "config.useTruststoreSpi[0]", label: t("useTruststoreSpi"), labelIcon: t("useTruststoreSpiHelp"), controller: {
defaultValue: "always",
}, options: [
{ key: "always", value: t("always") },
{ key: "never", value: t("never") },
] }), _jsx(FormGroup, { label: t("connectionPooling"), labelIcon: _jsx(HelpItem, { helpText: t("connectionPoolingHelp"), fieldLabelId: "connectionPooling" }), fieldId: "kc-connection-pooling", hasNoPaddingTop: true, children: _jsx(Controller, { name: "config.connectionPooling", defaultValue: ["false"], control: form.control, render: ({ field }) => (_jsx(Switch, { id: "kc-connection-pooling", "data-testid": "connection-pooling", isDisabled: false, onChange: (_event, value) => field.onChange([`${value}`]), isChecked: field.value[0] === "true", label: t("on"), labelOff: t("off"), "aria-label": t("connectionPooling") })) }) }), _jsx(TextControl, { name: "config.connectionTimeout.0", label: t("connectionTimeout"), labelIcon: t("connectionTimeoutHelp"), type: "number", min: 0 }), _jsx(FormGroup, { fieldId: "kc-test-connection-button", children: _jsx(Button, { variant: "secondary", id: "kc-test-connection-button", "data-testid": "test-connection-button", onClick: () => testLdap("testConnection"), children: t("testConnection") }) }), _jsx(FormGroup, { label: t("bindType"), labelIcon: _jsx(HelpItem, { helpText: t("bindTypeHelp"), fieldLabelId: "bindType" }), fieldId: "kc-bind-type", isRequired: true, children: _jsx(Controller, { name: "config.authType[0]", defaultValue: "simple", control: form.control, render: ({ field }) => (_jsxs(KeycloakSelect, { toggleId: "kc-bind-type", onToggle: () => setIsBindTypeDropdownOpen(!isBindTypeDropdownOpen), isOpen: isBindTypeDropdownOpen, onSelect: (value) => {
field.onChange(value);
setIsBindTypeDropdownOpen(false);
}, selections: field.value, variant: SelectVariant.single, "data-testid": "ldap-bind-type", "aria-label": t("selectBindType"), children: [_jsx(SelectOption, { value: "simple", children: "simple" }), _jsx(SelectOption, { value: "none", children: "none" })] })) }) }), isEqual(ldapBindType, ["simple"]) && (_jsxs(_Fragment, { children: [_jsx(TextControl, { name: "config.bindDn.0", label: t("bindDn"), labelIcon: t("bindDnHelp"), rules: {
required: t("validateBindDn"),
} }), _jsx(PasswordControl, { name: "config.bindCredential.0", label: t("bindCredentials"), labelIcon: t("bindCredentialsHelp"), hasReveal: !edit, rules: {
required: t("validateBindCredentials"),
} })] })), _jsx(FormGroup, { fieldId: "kc-test-auth-button", children: _jsx(Button, { variant: "secondary", id: "kc-test-auth-button", "data-testid": "test-auth-button", onClick: () => testLdap("testAuthentication"), children: t("testAuthentication") }) })] })] }));
};
//# sourceMappingURL=LdapSettingsConnection.js.map