def updateuser(self, uid, modattrs):
l = ldap.initialize(self.config["server"])
l.simple_bind(self.config["admin"], self.config["password"])
dn = "uid=%s,%s" % (uid, self.config["memberdn"])
ldap_filter = "uid="+uid
result_id = l.search(self.config["memberdn"], ldap.SCOPE_SUBTREE, ldap_filter, None)
if result_id:
type, data = l.result(result_id, 0)
if data:
dn, attrs = data[0]
oldattrs = attrs
newattrs = attrs.copy()
newattrs.update(modattrs)
# now change it
newattrs.update(oldattrs)
ldif = modlist.modifyModlist(oldattrs, newattrs)
print ldif
l.modify_s(dn, ldif)
l.unbind_s()
return True
else:
return False
python类modlist()的实例源码
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()
def __ldap_update_mail(self, member, newaddress):
l = self.__ldap_bind()
dn = self.__ldap_member_to_key(member)
oldaddress = self.getMemberCPAddress(member)
modlist = ldap.modlist.modifyModlist({'mail': oldaddress},
{'mail': newaddress})
l.modify_s(dn, modlist)
# Load new values
self.__updatetime = 0
self.__ldap_load_members(l)
def adduser(self, name, surname, username, usersecret, expireDate, uidNo, badgenum):
if (self.userexistsbyuid(username) ):
print("User %s already exist!", username)
return
dn = "uid="+username+",ou=People,"+self.dc
attrs = {}
attrs['uid'] = username
attrs['userPassword'] = usersecret
attrs['givenName'] = name
attrs['sn'] = surname
attrs['cn'] = name+' '+surname
attrs['objectClass'] = ['person',
'organizationalPerson',
'inetOrgPerson',
'posixAccount',
'top',
'shadowAccount']
attrs['shadowMax'] = '99999'
attrs['shadowWarning'] = '7'
attrs['shadowExpire'] = expireDate
attrs['loginShell'] = '/bin/bash'
attrs['uidNumber'] = uidNo
attrs['gidNumber'] = '100'
attrs['homeDirectory'] = '/home/'+username
attrs['gecos'] = name+' '+surname+',,,,'+badgenum
attrs['employeeNumber'] = badgenum
attrs['mail'] = username+'@lcm.mi.infn.it'
# 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 ldapuri
self.conn.add_s(dn,ldif)
def adduser(self, attrs):
l = ldap.initialize(self.config["server"])
l.simple_bind(self.config["admin"], self.config["password"])
dn = "uid=%s,%s" % (attrs["uid"], self.config["memberdn"])
attrs["objectClass"] = ['top', 'account', 'simpleSecurityObject', 'xxPilot']
attrs["userPassword"] = self.makeSecret(attrs["userPassword"])
ldif = modlist.addModlist(attrs)
l.add_s(dn, ldif)
l.unbind_s()
def addgroup(self, attrs):
l = ldap.initialize(self.config["server"])
l.simple_bind(self.config["admin"], self.config["password"])
dn = "cn=%s,%s" % (attrs["cn"], self.config["groupdn"])
attrs["objectClass"] = ["groupofnames"]
ldif = modlist.addModlist(attrs)
print dn, ldif
l.add_s(dn, ldif)
l.unbind_s()
def addModlist(entry,ignore_attr_types=None):
"""Build modify list for call of method LDAPObject.add()"""
ignore_attr_types = list_dict(map(lower,(ignore_attr_types or [])))
modlist = []
for attrtype in entry.keys():
if lower(attrtype) in ignore_attr_types:
# This attribute type is ignored
continue
# Eliminate empty attr value strings in list
attrvaluelist = filter(lambda x:x!=None,entry[attrtype])
if attrvaluelist:
modlist.append((attrtype,entry[attrtype]))
return modlist # addModlist()
def create_modlist(newattrs):
modlist = []
for attrtype in newattrs.keys():
utf8_vals = []
for val in newattrs[attrtype]:
utf8_vals.append(utils.utf8(val))
newattrs[attrtype] = utf8_vals
modlist.append((attrtype, newattrs[attrtype]))
return modlist