Problem
As far as I can tell, django-auth-ldap
is doing everything I need given my configuration. Except that it's unable to establish a mapping from ldap groups to django groups.
I have a group in ldap called dev
, i.e., cn=dev
. I also have a group in django called dev
. When I login with my user in Django (uid=fkilibarda
), django-auth-ldap
is able to get my firstname, lastname, and email, but it fails to add me to the dev
group.
Debugging
django_auth_ldap.backend
line: 203
def get_group_permissions(self, user, obj=None):
if not hasattr(user, 'ldap_user') and self.settings.AUTHORIZE_ALL_USERS:
_LDAPUser(self, user=user) # This sets user.ldap_user
if hasattr(user, 'ldap_user') and (user.ldap_user.dn is not None):
return user.ldap_user.get_group_permissions()
else:
return set()
I've found that hasattr(user, 'ldap_user')
is always false. Thus, user.ldap_user.get_group_permissions()
never runs, which is why the group mapping is never established.
I just don't understand the significance of the above. Why doesn't user
have the ldap_user
attribute?
Configuration
MIDDLEWARE = [
'debug_toolbar.middleware.DebugToolbarMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
AUTHENTICATION_BACKENDS = {
'django_auth_ldap.backend.LDAPBackend',
'django.contrib.auth.backends.ModelBackend',
}
# ===================
# LDAP configurations
# ===================
AUTH_LDAP_SERVER_URI = "example_server"
AUTH_LDAP_GROUP_SEARCH = LDAPSearch(
'ou=group,dc=example,dc=example,dc=com',
ldap.SCOPE_SUBTREE,
'(objectClass=posixGroup)',
)
AUTH_LDAP_GROUP_TYPE = GroupOfNamesType()
AUTH_LDAP_USER_FLAGS_BY_GROUP = {
'is_active': 'cn=dev,ou=group,dc=example,dc=example,dc=com',
'is_staff': 'cn=dev,ou=group,dc=example,dc=example,dc=com',
'is_superuser': 'cn=dev,ou=group,dc=example,dc=example,dc=com',
}
AUTH_LDAP_USER_SEARCH = LDAPSearch(
'ou=people,dc=example,dc=example,dc=com',
ldap.SCOPE_SUBTREE,
'(uid=%(user)s)'
)
AUTH_LDAP_USER_ATTR_MAP = {
"first_name": "givenName",
"last_name": "sn",
"email": "mail"
}
AUTH_LDAP_FIND_GROUP_PERMS = True
AUTH_LDAP_ALWAYS_UPDATE_USER = True
Version
django-auth-ldap==1.2.14
python3