From fb0d3b944ae0c6c2f77d9d62e81f3e14476c9cce Mon Sep 17 00:00:00 2001 From: linoman <2051016+linoman@users.noreply.github.com> Date: Tue, 17 Sep 2024 17:52:53 +0200 Subject: [PATCH] SSO LDAP: Fix search base dns parsing (#93179) * Fix parse of * Add i18n files * Add default value * Replace input field with multiselect * rename delete form function * Use Controller for MultiSelect --- public/app/features/admin/ldap/LdapDrawer.tsx | 28 +++++++------- .../features/admin/ldap/LdapSettingsPage.tsx | 38 +++++++++++-------- public/locales/en-US/grafana.json | 5 +-- public/locales/pseudo-LOCALE/grafana.json | 5 +-- 4 files changed, 40 insertions(+), 36 deletions(-) diff --git a/public/app/features/admin/ldap/LdapDrawer.tsx b/public/app/features/admin/ldap/LdapDrawer.tsx index e267abf1c15..42b3abdc660 100644 --- a/public/app/features/admin/ldap/LdapDrawer.tsx +++ b/public/app/features/admin/ldap/LdapDrawer.tsx @@ -1,6 +1,6 @@ import { css } from '@emotion/css'; import { Dispatch, SetStateAction, useEffect, useId, useState } from 'react'; -import { useFormContext } from 'react-hook-form'; +import { Controller, useFormContext } from 'react-hook-form'; import { GrafanaTheme2, SelectableValue } from '@grafana/data'; import { @@ -49,7 +49,7 @@ export const LdapDrawerComponent = ({ const [encryptionProvider, setEncryptionProvider] = useState(EncryptionProvider.Base64); const styles = useStyles2(getStyles); - const { getValues, register, setValue, watch } = useFormContext(); + const { control, getValues, register, setValue, watch } = useFormContext(); const nameId = useId(); const surnameId = useId(); @@ -335,19 +335,17 @@ export const LdapDrawerComponent = ({ 'Root CA certificate content' )} > - { - setValue( - `${serverConfig}.root_ca_cert_value`, - v.filter(({ v }) => typeof v === 'string')?.map(({ v }) => v) - ); - }} - value={watch(`${serverConfig}.root_ca_cert_value`)?.map((v) => ({ - label: renderMultiSelectLabel(v), - value: v, - }))} + ( + onChange(v.map(({ value }) => String(value)))} + value={value?.map((v) => ({ label: renderMultiSelectLabel(v), value: v }))} + /> + )} /> { }); const methods = useForm({ defaultValues: emptySettings }); - const { getValues, handleSubmit, register, reset, watch } = methods; + const { control, getValues, handleSubmit, register, reset, watch } = methods; const styles = useStyles2(getStyles); @@ -207,7 +210,7 @@ export const LdapSettingsPage = () => { const saveForm = () => { putPayload(getValues()); }; - const discardForm = async () => { + const deleteLDAPConfig = async () => { try { setIsLoading(true); await getBackendSrv().delete('/api/v1/sso-settings/ldap'); @@ -273,7 +276,7 @@ export const LdapSettingsPage = () => { id="host" placeholder={t('ldap-settings-page.host.placeholder', 'example: 127.0.0.1')} type="text" - {...register('settings.config.servers.0.host', { required: true })} + {...register(`${serverConfig}.host`, { required: true })} /> { id="bind-dn" placeholder={t('ldap-settings-page.bind-dn.placeholder', 'example: cn=admin,dc=grafana,dc=org')} type="text" - {...register('settings.config.servers.0.bind_dn')} + {...register(`${serverConfig}.bind_dn`)} /> { id="search_filter" placeholder={t('ldap-settings-page.search_filter.placeholder', 'example: cn=%s')} type="text" - {...register('settings.config.servers.0.search_filter', { required: true })} + {...register(`${serverConfig}.search_filter`, { required: true })} /> - + ( + onChange(v.map(({ value }) => String(value)))} + /> + )} + > @@ -356,7 +364,7 @@ export const LdapSettingsPage = () => { - + } placement="bottom-start" diff --git a/public/locales/en-US/grafana.json b/public/locales/en-US/grafana.json index ecec41eb388..29529fcd98a 100644 --- a/public/locales/en-US/grafana.json +++ b/public/locales/en-US/grafana.json @@ -1178,9 +1178,8 @@ "placeholder": "example: cn=%s" }, "search-base-dns": { - "description": "An array of base dns to search through; separate by commas or spaces.", - "label": "Search base DNS *", - "placeholder": "example: \"dc=grafana.dc=org\"" + "description": "An array of base dns to search through.", + "label": "Search base DNS *" }, "subtitle": "The LDAP integration in Grafana allows your Grafana users to log in with their LDAP credentials. Find out more in our <2><0>documentation.", "title": "Basic Settings" diff --git a/public/locales/pseudo-LOCALE/grafana.json b/public/locales/pseudo-LOCALE/grafana.json index 5c79fdf73a2..dd3a9a9f27c 100644 --- a/public/locales/pseudo-LOCALE/grafana.json +++ b/public/locales/pseudo-LOCALE/grafana.json @@ -1178,9 +1178,8 @@ "placeholder": "ęχämpľę: čʼn=%ş" }, "search-base-dns": { - "description": "Åʼn äřřäy őƒ þäşę đʼnş ŧő şęäřčĥ ŧĥřőūģĥ; şępäřäŧę þy čőmmäş őř şpäčęş.", - "label": "Ŝęäřčĥ þäşę ĐŃŜ *", - "placeholder": "ęχämpľę: \"đč=ģřäƒäʼnä.đč=őřģ\"" + "description": "Åʼn äřřäy őƒ þäşę đʼnş ŧő şęäřčĥ ŧĥřőūģĥ.", + "label": "Ŝęäřčĥ þäşę ĐŃŜ *" }, "subtitle": "Ŧĥę ĿĐÅP įʼnŧęģřäŧįőʼn įʼn Ğřäƒäʼnä äľľőŵş yőūř Ğřäƒäʼnä ūşęřş ŧő ľőģ įʼn ŵįŧĥ ŧĥęįř ĿĐÅP čřęđęʼnŧįäľş. Fįʼnđ őūŧ mőřę įʼn őūř <2><0>đőčūmęʼnŧäŧįőʼn.", "title": "ßäşįč Ŝęŧŧįʼnģş"