Using the django-auth-ldap LDAPSearch to search two OUs
Asked Answered
G

1

11

I have an containerized application that is using django-auth-ldap to search an Active Directory for users. I would like to combine the output from two separate OUs. Is there a different method or overload that could take two DN's or a way to the join the output of two separate searches?

AUTH_LDAP_USER_SEARCH = LDAPSearch(os.environ.get('AUTH_LDAP_USER_SEARCH_BASEDN', ''),
                                ldap.SCOPE_SUBTREE,
                                "(sAMAccountName=%(user)s)")
Gavage answered 21/8, 2018 at 17:31 Comment(0)
S
8

Taken from the 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)"),
)
Scandium answered 23/8, 2018 at 20:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.