@@ -26,6 +26,11 @@ import type { ConnectionSecurity, LdapConfig } from './types';
2626import { jsonParse , LoggerProxy as Logger } from 'n8n-workflow' ;
2727import { License } from '@/License' ;
2828import { InternalHooks } from '@/InternalHooks' ;
29+ import {
30+ isEmailCurrentAuthenticationMethod ,
31+ isLdapCurrentAuthenticationMethod ,
32+ setCurrentAuthenticationMethod ,
33+ } from '@/sso/ssoHelpers' ;
2934
3035/**
3136 * Check whether the LDAP feature is disabled in the instance
@@ -50,8 +55,24 @@ export const setLdapLoginLabel = (value: string): void => {
5055/**
5156 * Set the LDAP login enabled to the configuration object
5257 */
53- export const setLdapLoginEnabled = ( value : boolean ) : void => {
54- config . set ( LDAP_LOGIN_ENABLED , value ) ;
58+ export const setLdapLoginEnabled = async ( value : boolean ) : Promise < void > => {
59+ if ( config . get ( LDAP_LOGIN_ENABLED ) === value ) {
60+ return ;
61+ }
62+ // only one auth method can be active at a time, with email being the default
63+ if ( value && isEmailCurrentAuthenticationMethod ( ) ) {
64+ // enable ldap login and disable email login, but only if email is the current auth method
65+ config . set ( LDAP_LOGIN_ENABLED , true ) ;
66+ await setCurrentAuthenticationMethod ( 'ldap' ) ;
67+ } else if ( ! value && isLdapCurrentAuthenticationMethod ( ) ) {
68+ // disable ldap login, but only if ldap is the current auth method
69+ config . set ( LDAP_LOGIN_ENABLED , false ) ;
70+ await setCurrentAuthenticationMethod ( 'email' ) ;
71+ } else {
72+ Logger . warn (
73+ 'Cannot switch LDAP login enabled state when an authentication method other than email is active' ,
74+ ) ;
75+ }
5576} ;
5677
5778/**
@@ -126,8 +147,8 @@ export const getLdapConfig = async (): Promise<LdapConfig> => {
126147/**
127148 * Take the LDAP configuration and set login enabled and login label to the config object
128149 */
129- export const setGlobalLdapConfigVariables = ( ldapConfig : LdapConfig ) : void => {
130- setLdapLoginEnabled ( ldapConfig . loginEnabled ) ;
150+ export const setGlobalLdapConfigVariables = async ( ldapConfig : LdapConfig ) : Promise < void > => {
151+ await setLdapLoginEnabled ( ldapConfig . loginEnabled ) ;
131152 setLdapLoginLabel ( ldapConfig . loginLabel ) ;
132153} ;
133154
@@ -175,7 +196,7 @@ export const updateLdapConfig = async (ldapConfig: LdapConfig): Promise<void> =>
175196 { key : LDAP_FEATURE_NAME } ,
176197 { value : JSON . stringify ( ldapConfig ) , loadOnStartup : true } ,
177198 ) ;
178- setGlobalLdapConfigVariables ( ldapConfig ) ;
199+ await setGlobalLdapConfigVariables ( ldapConfig ) ;
179200} ;
180201
181202/**
@@ -197,7 +218,7 @@ export const handleLdapInit = async (): Promise<void> => {
197218
198219 const ldapConfig = await getLdapConfig ( ) ;
199220
200- setGlobalLdapConfigVariables ( ldapConfig ) ;
221+ await setGlobalLdapConfigVariables ( ldapConfig ) ;
201222
202223 // init LDAP manager with the current
203224 // configuration
0 commit comments