I want to import the users of a ActiveDirectory database into Django. To this end I'm trying to use the django_auth_ldap module.
Here is what I tried already :
in my settings.py :
AUTH_LDAP_SERVER_URI = "ldap://example.fr"
AUTH_LDAP_BIND_DN = 'cn=a_user,dc=example,dc=fr'
AUTH_LDAP_BIND_PASSWORD=''
AUTH_LDAP_USER_SEARCH = LDAPSearch('ou=users,dc=example,dc=fr', ldap.SCOPE_SUBTREE, '(uid=%(user)s)')
AUTH_LDAP_GROUP_SEARCH = LDAPSearch('ou=groups,dc=example,dc=fr', ldap.SCOPE_SUBTREE, '(objectClass=groupOfNames)')
AUTH_LDAP_GROUP_TYPE = ActiveDirectoryGroupType()
#Populate the Django user from the LDAP directory
AUTH_LDAP_USER_ATTR_MAP = {
'first_name': 'sAMAccountName',
'last_name': 'displayName',
'email': 'mail'
}
AUTHENTICATION_BACKENDS = (
'django_auth_ldap.backend.LDAPBackend',
'django.contrib.auth.backends.ModelBackend',
)
Then I call python manage.py syncdb
with no result. No warning, no error, nothing updataed in the auth_user table. Is there something obvious I forgot to do ?
I needed to do something similar, and found the LDAPBackend.populate_user(user_name) API useful.
Given each call is going to issue LDAP queries and a bunch of DB select/ updated/ insert queries, this is more suited to getting or creating occasional users (for masquerading as them/ check how the app looks for them) rather than bulk creating them.
I'd say that you really don't want to use the django_auth_ldap here, since that just creates users on demand as they log in (as others have noted). Instead, you can just use the raw python_ldap module to do a raw LDAP query:
And then iterate over the results to stuff them into your model.
Looking at the documentation for
django_auth_ldap
it appears that the module doesn't actually walk through LDAP users and load them into the database. Instead, it authenticates a user against LDAP, and then adds or updates them inauth_users
with the information it gets from LDAP when the user logs in.If you want to pre-populate the database with all of the users in Active Directory then it looks like you'll need to write a script that queries AD directly and insert the users.
Something like this should get you started:
I have left the database update to you, since I don't have any information about your setup.
If you need more information about LDAP queries, check out the LDAP questions here on Stackoverflow -- and I also found this article to be a help.