djangodjango-auth-ldap

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


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)")

Solution

  • 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)"),
    )