1717import ldap
1818from django_auth_ldap .config import GroupOfNamesType
1919from django_auth_ldap .config import LDAPSearch
20+ from dejacode_toolkit .ldap import build_user_search
2021
2122# The home directory of the dejacode user that owns the installation.
2223PROJECT_DIR = environ .Path (__file__ ) - 1
@@ -769,7 +770,7 @@ def get_fake_redis_connection(config, use_strict_redis):
769770
770771# Optional: Define multiple LDAP user searches using a JSON list.
771772# When provided, this setting overrides AUTH_LDAP_USER_DN and AUTH_LDAP_USER_FILTERSTR.
772- #
773+ #
773774# Example:
774775# AUTH_LDAP_USER_SEARCHES = """
775776# [
@@ -793,46 +794,11 @@ def get_fake_redis_connection(config, use_strict_redis):
793794# All searches are combined using LDAPSearchUnion.
794795AUTH_LDAP_USER_SEARCHES = env .str ("AUTH_LDAP_USER_SEARCHES" , default = "" )
795796
796- if AUTH_LDAP_USER_SEARCHES :
797- import json
798- from django_auth_ldap .config import LDAPSearchUnion
799-
800- try :
801- ldap_search_definitions = json .loads (AUTH_LDAP_USER_SEARCHES )
802- except json .JSONDecodeError as e :
803- raise ValueError ("Invalid JSON in AUTH_LDAP_USER_SEARCHES" ) from e
804-
805- if not isinstance (ldap_search_definitions , list ):
806- raise ValueError ("AUTH_LDAP_USER_SEARCHES must be a JSON list" )
807-
808- ldap_searches = []
809- for search_definition in ldap_search_definitions :
810- if not isinstance (search_definition , dict ):
811- raise ValueError ("Each entry must be an object" )
812-
813- base_dn = search_definition .get ("base" )
814- filterstr = search_definition .get ("filter" )
815-
816- if not base_dn or not filterstr :
817- raise ValueError ("Each LDAP search entry must define 'base' and 'filter'" )
818-
819- ldap_searches .append (
820- LDAPSearch (base_dn , ldap .SCOPE_SUBTREE , filterstr )
821- )
822-
823- if not ldap_searches :
824- raise ValueError ("AUTH_LDAP_USER_SEARCHES cannot be empty" )
825-
826- # Always use LDAPSearchUnion, even for a single search entry.
827- AUTH_LDAP_USER_SEARCH = LDAPSearchUnion (* ldap_searches )
828-
829- else :
830- # Fallback to single LDAP search configuration.
831- AUTH_LDAP_USER_SEARCH = LDAPSearch (
832- AUTH_LDAP_USER_DN ,
833- ldap .SCOPE_SUBTREE ,
834- AUTH_LDAP_USER_FILTERSTR ,
835- )
797+ AUTH_LDAP_USER_SEARCH = build_user_search (
798+ AUTH_LDAP_USER_SEARCHES ,
799+ AUTH_LDAP_USER_DN ,
800+ AUTH_LDAP_USER_FILTERSTR ,
801+ )
836802
837803# When AUTH_LDAP_AUTOCREATE_USER is True (default), a new DejaCode user will be
838804# created in the database with the minimum permission (a read-only user).
0 commit comments