def get_dn(dn, name): # pylint: disable=C0103 """Returns a distinguished name with group name and ou information.""" dn = DistinguishedName(dn) uid_entry = DNComponent('cn', name) dn.insert(0, uid_entry) return dn
def remove(name, member, *, ou=OU, domain=DOMAIN): """Adds a member to the group.""" dn = DistinguishedName.for_group(name, domain, ou=ou) yield LDIFEntry('dn', dn) yield LDIFEntry('changetype', 'modify') yield LDIFEntry('delete', 'memberUid') yield LDIFEntry('memberUid', member)
def create(name, gid, members, *, ou=OU, domain=DOMAIN): """Creates a new group LDIF.""" dn = DistinguishedName.for_group(name, domain, ou=ou) yield LDIFEntry('dn', dn) yield LDIFEntry('cn', name) yield LDIFEntry('gidNumber', gid) for clas in CLASSES: yield LDIFEntry('objectClass', clas) for member in members: yield LDIFEntry('memberUid', member)
def modify(name=None, new_name=None, uid=None, gid=None, first_name=None, last_name=None, passwd=None, pwhash=None, home=None, shell=None, *, ou=OU, domain=DOMAIN): """Creates an LDIF to modify a user.""" dn = DistinguishedName.for_user(name, domain, ou=ou) yield LDIFEntry('dn', dn) yield LDIFEntry('changetype', 'modify') if new_name is not None: yield LDIFEntry('replace', 'uid') yield LDIFEntry('uid', new_name) cn = get_cn(first_name, last_name) if cn is not None: yield LDIFEntry('replace', 'cn') yield LDIFEntry('cn', cn) if last_name is not None: yield LDIFEntry('replace', 'sn') yield LDIFEntry('sn', last_name) if first_name is not None: yield LDIFEntry('replace', 'givenName') yield LDIFEntry('givenName', first_name) if pwhash is None and passwd is None: pwhash = None else: pwhash = get_pwhash(passwd, pwhash) if pwhash is not None: yield LDIFEntry('replace', 'userPassword') yield LDIFEntry('userPassword', pwhash) if shell is not None: yield LDIFEntry('replace', 'loginShell') yield LDIFEntry('loginShell', shell) if uid is not None: yield LDIFEntry('replace', 'uidNumber') yield LDIFEntry('uidNumber', uid) if gid is not None: yield LDIFEntry('replace', 'gidNumber') yield LDIFEntry('gidNumber', gid) if home is not None: yield LDIFEntry('replace', 'homeDirectory') yield LDIFEntry('homeDirectory', home)
def modify(name, new_name=None, gid=None, *, ou=OU, domain=DOMAIN): """Modifies an existing group.""" dn = DistinguishedName.for_group(name, domain, ou=ou) yield LDIFEntry('dn', dn) yield LDIFEntry('changetype', 'modify') if new_name is not None: yield LDIFEntry('replace', 'cn') yield LDIFEntry('cn', new_name) if gid is not None: yield LDIFEntry('replace', 'gidNumber') yield LDIFEntry('gidNumber', gid)
def create(name, first_name, last_name, passwd=None, pwhash=None, uid=None, gid=None, home=HOME, shell=SHELL, *, ou=OU, domain=DOMAIN): """Creates an LDIF represeting a new user.""" dn = DistinguishedName.for_user(name, domain, ou=ou) yield LDIFEntry('dn', dn) for clas in CLASSES: yield LDIFEntry('objectClass', clas) yield LDIFEntry('uid', name) full_name = ' '.join((first_name, last_name)) yield LDIFEntry('cn', full_name) yield LDIFEntry('sn', last_name) yield LDIFEntry('givenName', first_name) pwhash = get_pwhash(passwd, pwhash) yield LDIFEntry('userPassword', pwhash) yield LDIFEntry('loginShell', shell) uid = get_uid() if uid is None else uid yield LDIFEntry('uidNumber', uid) gid = get_gid() if gid is None else gid yield LDIFEntry('gidNumber', gid) home = home.format(name) yield LDIFEntry('homeDirectory', home)
def delete(name, *, ou=OU, domain=DOMAIN): """Creates an LDIF to delete a user.""" return DistinguishedName.for_user(name, domain, ou=ou)