ldap导入mysql,导入LDAP用户进入Django的数据库

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 ?

解决方案

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 in auth_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:

import ldap

l = ldap.initialize('ldap://your_ldap_server') # or ldaps://

l.simple_bind_s("cn=a_user,dc=example,dc=fr")

users = l.search_ext_s("memberOf=YourUserGroup",\

ldap.SCOPE_SUBTREE, \

"(sAMAccountName=a_user)", \

attrlist=["sAMAccountName", "displayName","mail"])

# users is now an array of members who match your search criteria.

# *Each* user will look something like this:

# [["Firstname"],["LastName"],["some@email.address"]]

# Note that each field is in an array, even if there is only one value.

# If you only want the first value from each, you can transform the results:

# users = [[field[0] for field in user] for user in users]

# That will transform each row into something like this:

# ["Firstname", "Lastname", "some@email.address"]

# TODO -- add to the database.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值