From ad462f0a11972c7b70ac319972876aa839f1b6d3 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Tue, 5 Nov 2024 20:52:02 -0800 Subject: [PATCH] refac: ldap settings --- .../admin/Settings/Connections.svelte | 232 +---------------- .../components/admin/Settings/Users.svelte | 238 +++++++++++++++++- 2 files changed, 239 insertions(+), 231 deletions(-) diff --git a/src/lib/components/admin/Settings/Connections.svelte b/src/lib/components/admin/Settings/Connections.svelte index d3ec405ee..993d70be7 100644 --- a/src/lib/components/admin/Settings/Connections.svelte +++ b/src/lib/components/admin/Settings/Connections.svelte @@ -20,12 +20,7 @@ updateOpenAIKeys, updateOpenAIUrls } from '$lib/apis/openai'; - import { - getLdapConfig, - updateLdapConfig, - getLdapServer, - updateLdapServer - } from '$lib/apis/auths'; + import { toast } from 'svelte-sonner'; import Switch from '$lib/components/common/Switch.svelte'; import Spinner from '$lib/components/common/Spinner.svelte'; @@ -51,22 +46,6 @@ let ENABLE_OPENAI_API = null; let ENABLE_OLLAMA_API = null; - // LDAP - let ENABLE_LDAP = false; - let LDAP_SERVER = { - label: '', - host: '', - port: '', - attribute_for_username: 'uid', - app_dn: '', - app_dn_password: '', - search_base: '', - search_filters: '', - use_tls: false, - certificate_path: '', - ciphers: '' - }; - const verifyOpenAIHandler = async (idx) => { OPENAI_API_BASE_URLS = OPENAI_API_BASE_URLS.map((url) => url.replace(/\/$/, '')); @@ -158,17 +137,6 @@ } }; - const updateLdapServerHandler = async () => { - if (!ENABLE_LDAP) return; - const res = await updateLdapServer(localStorage.token, LDAP_SERVER).catch((error) => { - toast.error(error); - return null; - }); - if (res) { - toast.success($i18n.t('LDAP server updated')); - } - }; - onMount(async () => { if ($user.role === 'admin') { await Promise.all([ @@ -180,19 +148,14 @@ })(), (async () => { OPENAI_API_KEYS = await getOpenAIKeys(localStorage.token); - })(), - (async () => { - LDAP_SERVER = await getLdapServer(localStorage.token); })() ]); const ollamaConfig = await getOllamaConfig(localStorage.token); const openaiConfig = await getOpenAIConfig(localStorage.token); - const ldapConfig = await getLdapConfig(localStorage.token); ENABLE_OPENAI_API = openaiConfig.ENABLE_OPENAI_API; ENABLE_OLLAMA_API = ollamaConfig.ENABLE_OLLAMA_API; - ENABLE_LDAP = ldapConfig.ENABLE_LDAP; if (ENABLE_OPENAI_API) { OPENAI_API_BASE_URLS.forEach(async (url, idx) => { @@ -211,13 +174,12 @@ on:submit|preventDefault={() => { updateOpenAIHandler(); updateOllamaUrlsHandler(); - updateLdapServerHandler(); dispatch('save'); }} >
- {#if ENABLE_OPENAI_API !== null && ENABLE_OLLAMA_API !== null && ENABLE_LDAP !== null} + {#if ENABLE_OPENAI_API !== null && ENABLE_OLLAMA_API !== null}
@@ -467,196 +429,6 @@
{/if}
- -
- -
-
-
-
{$i18n.t('LDAP')}
- -
- { - updateLdapConfig(localStorage.token, ENABLE_LDAP); - }} - /> -
-
- - {#if ENABLE_LDAP} -
-
-
-
- {$i18n.t('Label')} -
- -
-
-
-
-
-
- {$i18n.t('Host')} -
- -
-
-
- {$i18n.t('Port')} -
- - - -
-
-
-
-
- {$i18n.t('Application DN')} -
- - - -
-
-
- {$i18n.t('Application DN Password')} -
- -
-
-
-
-
- {$i18n.t('Attribute for Username')} -
- - - -
-
-
-
-
- {$i18n.t('Search Base')} -
- - - -
-
-
-
-
- {$i18n.t('Search Filters')} -
- -
-
- -
-
-
{$i18n.t('TLS')}
- -
- -
-
- {#if LDAP_SERVER.use_tls} -
-
-
- {$i18n.t('Certificate Path')} -
- -
-
-
-
-
- {$i18n.t('Ciphers')} -
- - - -
-
-
- {/if} -
-
- {/if} -
-
{:else}
diff --git a/src/lib/components/admin/Settings/Users.svelte b/src/lib/components/admin/Settings/Users.svelte index 04f780759..e4fae8436 100644 --- a/src/lib/components/admin/Settings/Users.svelte +++ b/src/lib/components/admin/Settings/Users.svelte @@ -3,10 +3,19 @@ import { getSignUpEnabledStatus, toggleSignUpEnabledStatus } from '$lib/apis/auths'; import { getUserPermissions, updateUserPermissions } from '$lib/apis/users'; + import { + getLdapConfig, + updateLdapConfig, + getLdapServer, + updateLdapServer + } from '$lib/apis/auths'; + import { onMount, getContext } from 'svelte'; import { models, config } from '$lib/stores'; import Switch from '$lib/components/common/Switch.svelte'; import { setDefaultModels } from '$lib/apis/configs'; + import Tooltip from '$lib/components/common/Tooltip.svelte'; + import SensitiveInput from '$lib/components/common/SensitiveInput.svelte'; const i18n = getContext('i18n'); @@ -28,6 +37,33 @@ let chatEdit = true; let chatTemporary = true; + // LDAP + let ENABLE_LDAP = false; + let LDAP_SERVER = { + label: '', + host: '', + port: '', + attribute_for_username: 'uid', + app_dn: '', + app_dn_password: '', + search_base: '', + search_filters: '', + use_tls: false, + certificate_path: '', + ciphers: '' + }; + + const updateLdapServerHandler = async () => { + if (!ENABLE_LDAP) return; + const res = await updateLdapServer(localStorage.token, LDAP_SERVER).catch((error) => { + toast.error(error); + return null; + }); + if (res) { + toast.success($i18n.t('LDAP server updated')); + } + }; + onMount(async () => { permissions = await getUserPermissions(localStorage.token); @@ -41,6 +77,13 @@ whitelistModels = res.models.length > 0 ? res.models : ['']; } + (async () => { + LDAP_SERVER = await getLdapServer(localStorage.token); + })(); + + const ldapConfig = await getLdapConfig(localStorage.token); + ENABLE_LDAP = ldapConfig.ENABLE_LDAP; + defaultModelId = $config.default_models ? $config?.default_models.split(',')[0] : ''; }); @@ -59,12 +102,15 @@ } }); await updateModelFilterConfig(localStorage.token, whitelistEnabled, whitelistModels); + + await updateLdapServerHandler(); + saveHandler(); await config.set(await getBackendConfig()); }} > -
+
{$i18n.t('User Permissions')}
@@ -87,6 +133,196 @@
+
+ +
+
+
+
{$i18n.t('LDAP')}
+ +
+ { + updateLdapConfig(localStorage.token, ENABLE_LDAP); + }} + /> +
+
+ + {#if ENABLE_LDAP} +
+
+
+
+ {$i18n.t('Label')} +
+ +
+
+
+
+
+
+ {$i18n.t('Host')} +
+ +
+
+
+ {$i18n.t('Port')} +
+ + + +
+
+
+
+
+ {$i18n.t('Application DN')} +
+ + + +
+
+
+ {$i18n.t('Application DN Password')} +
+ +
+
+
+
+
+ {$i18n.t('Attribute for Username')} +
+ + + +
+
+
+
+
+ {$i18n.t('Search Base')} +
+ + + +
+
+
+
+
+ {$i18n.t('Search Filters')} +
+ +
+
+ +
+
+
{$i18n.t('TLS')}
+ +
+ +
+
+ {#if LDAP_SERVER.use_tls} +
+
+
+ {$i18n.t('Certificate Path')} +
+ +
+
+
+
+
+ {$i18n.t('Ciphers')} +
+ + + +
+
+
+ {/if} +
+
+ {/if} +
+
+