def create_user_in_ldap(username, password, uidnumber):
# Open a connection
l = ldap.initialize(settings.AUTH_LDAP_SERVER_URI)
# Bind/authenticate with a user with apropriate rights to add objects
l.simple_bind_s(settings.AUTH_LDAP_BIND_DN, settings.AUTH_LDAP_BIND_PASSWORD)
# The dn of our new entry/object
dn="cn="+ username +",dc=ldap,dc=portal,dc=com"
#dn="cn=python_test,ou=People,dc=coegss,dc=hlrs,dc=de"
ctx = sha.new(password)
hash = "{SHA}" + b64encode(ctx.digest())
# A dict to help build the "body" of the object
attrs = {}
attrs['uid'] = [str(username)]
attrs['uidNumber'] = [str(uidnumber+500)]
attrs['gidNumber'] = ['100']
attrs['objectclass'] = ['inetOrgPerson','organizationalPerson','person','posixAccount','top']
attrs['cn'] = str(username)
attrs['sn'] = str(username)
attrs['userPassword'] = hash
#attrs['description'] = 'test_python_user'
attrs['homeDirectory'] = '/home/users/' + str(username)
# Convert our dict to nice syntax for the add-function using modlist-module
ldif = modlist.addModlist(attrs)
# Do the actual synchronous add-operation to the ldapserver
l.add_s(dn,ldif)
# Disconnect and free resources when done
l.unbind_s()
评论列表
文章目录