Hello
First of all, this is a great project!
I can now successfully create and log in django users by assigning them to AD groups. Running Django 1.3, python2.7 and python-ldap2.3, and a Windows 2008 R2 server with AD.
I've spent all day making this work, and just want to share my setup, since I've encountered a few problems along the way.
This is my settings.py. Note that I had to change the user search ldap.SCOPE_SUBTREE to use "sAMAccountName" instead of "uid", as well as using the group search (objectClass=group), and ActiveDirectoryGroupType() GROUP_TYPE.
#===============================================================================
# LDAP CONFIGURATION
#===============================================================================
# Basexampleine configuration.
AUTH_LDAP_SERVER_URI = 'ldap://host.example.local'
AUTH_LDAP_BIND_DN = 'cn=bind_user,cn=Users,dc=example,dc=local'
AUTH_LDAP_BIND_PASSWORD = 'password_here'
AUTH_LDAP_USER_SEARCH = LDAPSearch('dc=example,dc=local', ldap.SCOPE_SUBTREE, '(sAMAccountName=%(user)s)',)
AUTH_LDAP_GROUP_SEARCH = LDAPSearch('ou=Application_OU,ou=Grupper,dc=example,dc=local', ldap.SCOPE_SUBTREE, '(objectClass=group)')
AUTH_LDAP_GROUP_TYPE = ActiveDirectoryGroupType()
# Only users in this group can log in.
AUTH_LDAP_REQUIRE_GROUP = 'cn=enabled,ou=Application_OU,ou=Grupper,dc=example,dc=local'
# Populate the Django user from the LDAP directory.
AUTH_LDAP_USER_ATTR_MAP = {
'first_name': 'givenName',
'last_name': 'sn',
'email': 'mail'
}
#AUTH_LDAP_PROFILE_ATTR_MAP = {
# "employee_number": "employeeNumber"
#}
AUTH_LDAP_USER_FLAGS_BY_GROUP = {
'is_active': 'cn=active,ou=Application_OU,ou=Grupper,dc=example,dc=local',
'is_staff': 'cn=staff,ou=Application_OU,ou=Grupper,dc=example,dc=local',
'is_superuser': 'cn=superuser,ou=Application_OU,ou=Grupper,dc=example,dc=local'
}
# This is the default, but I like to be explicit.
AUTH_LDAP_ALWAYS_UPDATE_USER = True
# Use LDAP group membership to calculate group permissions.
AUTH_LDAP_FIND_GROUP_PERMS = True
# Cache group memberships for 5 minutes to reduce LDAP traffic
AUTH_LDAP_CACHE_GROUPS = True
AUTH_LDAP_GROUP_CACHE_TIMEOUT = 300
AUTH_LDAP_GLOBAL_OPTIONS = {
ldap.OPT_X_TLS_REQUIRE_CERT: False,
ldap.OPT_REFERRALS: False,
}
# Keep ModexampleBackend around for per-user permissions and maybe a local
# superuser.
AUTHENTICATION_BACKENDS = (
'django_auth_ldap.backend.LDAPBackend',
'django.contrib.auth.backends.ModexampleBackend',
)
However, I've one last issue I can't seem to figure out.
When using the AUTH_LDAP_USER_FLAGS_BY_GROUP to set permission flags on the django user account, I need to add the AD user to all this groups before I log in and the user will be created as a django user.
Shouldn't the user get only the flags set(by adding the user to the desired groups on AD)? If I try to create a django user by only adding the user to ie. "enabled" and "active" AD groups, It seems natural to me that the django user which will be created should be able to log in(because he's in the "enabled" group, and have is_active flag set True (since he's in the "active" AD group). But this fails with an error like this:
2011-09-29 22:52:23 WARNING django_auth_ldap Caught LDAPError while authenticating username: NO_SUCH_ATTRIBUTE({'info': '00002080: AtrErr: DSID-0308014F, #1:\n\t0: 00002080: DSID-0308014F, problem 1001 (NO_ATTRIBUTE_OR_VAL), data 0, Att 1f (member)\n', 'desc': 'No such attribute'},)
Is this intended behaviour?