Skip to content

Commit d9ff19f

Browse files
Support for multiple AUTH_LDAP_REQUIRE_GROUP from environment variable
1 parent cb1bc4b commit d9ff19f

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

configuration/ldap/ldap_config.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from os import environ
33

44
import ldap
5-
from django_auth_ldap.config import LDAPSearch
5+
from django_auth_ldap.config import LDAPGroupQuery, LDAPSearch
66

77

88
# Read secret from file
@@ -86,12 +86,22 @@ def _import_group_type(group_type_name):
8686
# Define a group required to login.
8787
AUTH_LDAP_REQUIRE_GROUP = environ.get('AUTH_LDAP_REQUIRE_GROUP_DN')
8888

89+
# If non-empty string, AUTH_LDAP_REQUIRE_GROUP will be treated as a list delimited by this separator
90+
AUTH_LDAP_REQUIRE_GROUP_SEPARATOR = environ.get('AUTH_LDAP_REQUIRE_GROUP_DN_SEPARATOR', '')
91+
8992
# Define special user types using groups. Exercise great caution when assigning superuser status.
9093
AUTH_LDAP_USER_FLAGS_BY_GROUP = {}
9194

9295
if AUTH_LDAP_REQUIRE_GROUP is not None:
96+
# Build an LDAPGroupQuery when AUTH_LDAP_REQUIRE_GROUP should be treated as a list
97+
if AUTH_LDAP_REQUIRE_GROUP_SEPARATOR:
98+
_groups = list(filter(None, AUTH_LDAP_REQUIRE_GROUP.split(AUTH_LDAP_REQUIRE_GROUP_SEPARATOR)))
99+
AUTH_LDAP_REQUIRE_GROUP = LDAPGroupQuery(_groups[0])
100+
for i in range(1, len(_groups)):
101+
AUTH_LDAP_REQUIRE_GROUP |= LDAPGroupQuery(_groups[i])
102+
93103
AUTH_LDAP_USER_FLAGS_BY_GROUP = {
94-
"is_active": environ.get('AUTH_LDAP_REQUIRE_GROUP_DN', ''),
104+
"is_active": AUTH_LDAP_REQUIRE_GROUP,
95105
"is_staff": environ.get('AUTH_LDAP_IS_ADMIN_DN', ''),
96106
"is_superuser": environ.get('AUTH_LDAP_IS_SUPERUSER_DN', '')
97107
}

0 commit comments

Comments
 (0)