Skip to content

Commit 636495e

Browse files
authored
Merge pull request #1297 from NeodymiumFerBore/feat/multiple-ldap-require-dn
Feature: Support for multiple AUTH_LDAP_REQUIRE_GROUP from environment variables
2 parents 09ba1d3 + 671f5e9 commit 636495e

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

configuration/ldap/extra.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
####
22
## This file contains extra configuration options that can't be configured
33
## directly through environment variables.
4-
## All vairables set here overwrite any existing found in ldap_config.py
4+
## All variables set here overwrite any existing found in ldap_config.py
55
####
66

77
# # This Python script inherits all the imports from ldap_config.py
8-
# from django_auth_ldap.config import LDAPGroupQuery # Imported since not in ldap_config.py
98

109
# # Sets a base requirement of membetship to netbox-user-ro, netbox-user-rw, or netbox-user-admin.
1110
# AUTH_LDAP_REQUIRE_GROUP = (

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)