使用 django-auth-ldap LDAPSearch 搜索两个 OU

Using the django-auth-ldap LDAPSearch to search two OUs

我有一个容器化应用程序,它使用 django-auth-ldap 在 Active Directory 中搜索用户。我想合并来自两个独立 OU 的输出。是否有不同的方法或重载可以采用两个 DN 或一种方法来连接两个单独搜索的输出?

AUTH_LDAP_USER_SEARCH = LDAPSearch(os.environ.get('AUTH_LDAP_USER_SEARCH_BASEDN', ''),
                                ldap.SCOPE_SUBTREE,
                                "(sAMAccountName=%(user)s)")

取自 updated documentation:

New in version 1.1.

If you need to search in more than one place for a user, you can use LDAPSearchUnion. This takes multiple LDAPSearch objects and returns the union of the results. The precedence of the underlying searches is unspecified.

import ldap
from django_auth_ldap.config import LDAPSearch, LDAPSearchUnion

AUTH_LDAP_USER_SEARCH = LDAPSearchUnion(
    LDAPSearch("ou=users,dc=example,dc=com", ldap.SCOPE_SUBTREE, "(uid=%(user)s)"),
    LDAPSearch("ou=otherusers,dc=example,dc=com", ldap.SCOPE_SUBTREE, "(uid=%(user)s)"),
)