Clean user and group dictionaries

This commit is contained in:
uid 2020-10-10 12:54:28 +02:00
parent b46b4fa01e
commit affcc47fe9
3 changed files with 41 additions and 35 deletions

View File

@ -48,7 +48,7 @@ def get_all_users(client):
return users
def add_user(client, uid, cn, sn, mail):
def add_user(client, uid, cn, sn, mail, password):
dn = 'uid={},ou=Hackers,{}'.format(uid, client.base_dn)
attributes={
@ -59,7 +59,8 @@ def add_user(client, uid, cn, sn, mail):
],
'cn': cn,
'sn': sn,
'mail': mail
'mail': mail,
'userPassword': password # TODO: use hashed password
}
add_entry(client, dn, attributes)

View File

@ -1,35 +1,41 @@
import re
from phi.logging import get_logger
log = get_logger(__name__)
def flatten_attribute(attr):
if isinstance(attr, list) and len(attr)==1:
return attr[0]
else:
return attr
def flatten_entry(entry):
return {k: flatten_attribute(attr)
for k, attr in entry['attributes'].items()}
def make_user_dict(client, entry):
user = flatten_entry(entry)
dn = 'uid={},ou=Hackers,{}'.format(user['uid'], client.base_dn)
user['dn'] = dn
attributes = entry['attributes']
user = {}
user['uid'] = attributes['uid'][0]
user['dn'] = 'uid={},ou=Hackers,{}'.format(user['uid'], client.base_dn)
user['cn'] = attributes['cn'][0]
user['sn'] = attributes['sn'][0]
user['mail'] = attributes['mail'][0]
user['password'] = attributes['userPassword'][0]
return user
def make_group_dict(client, entry):
group = flatten_entry(entry)
dn = 'cn={},ou=Groups,{}'.format(group['cn'], client.base_dn)
group['dn'] = dn
def get_uid_from_dn(client, dn):
uid = re.search('uid=(.+?),ou=Hackers,{}'.format(client.base_dn),
dn).group(1)
return uid
# unflatten members, they have to be a list even is there is a single one
group['member'] = entry['attributes']['member']
def make_group_dict(client, entry):
attributes = entry['attributes']
cn = attributes['cn'][0]
dn = 'cn={},ou=Groups,{}'.format(cn, client.base_dn)
members = [get_uid_from_dn(client, u_dn)
for u_dn in attributes['member']]
group = {}
group['dn'] = dn
group['cn'] = cn
group['members'] = members
return group

View File

@ -15,9 +15,9 @@ def test_get_user_by_id(ldap_client):
assert user['mail'] == 'rmascetti@autistici.org'
def test_get_users_by_ou(ldap_client):
def test_get_all_users(ldap_client):
users = get_all_users(ldap_client)
# print(users)
assert 'conte_mascetti' in [u['uid'] for u in users]
@ -26,14 +26,16 @@ def test_add_delete_user(ldap_client):
cn = 'Rosa'
sn = 'Rossi'
mail = 'foo@autistici.org'
password = 'changeme'
add_user(ldap_client, uid, cn, sn, mail)
add_user(ldap_client, uid, cn, sn, mail, password)
user = get_user_by_uid(ldap_client, uid)
assert user['uid'] == uid
assert user['mail'] == mail
delete_user(ldap_client, user)
# print(user)
user = get_user_by_uid(ldap_client, uid)
assert user is None
@ -43,7 +45,7 @@ def test_failing_add_user(ldap_client):
uid = 'conte_mascetti'
try:
add_user(ldap_client, uid, '.', '.', '.')
add_user(ldap_client, uid, 'name', 'surname', 'mail', 'pass')
except: # User alrady existing
pass
else:
@ -72,13 +74,13 @@ def test_add_to_group(ldap_client):
group_cn = 'WikiUsers'
member_uid = 'rosa_rossi'
add_user(client, member_uid, '.', '.', '.')
add_user(client, member_uid, 'name', 'surname', 'mail', 'pass')
user = get_user_by_uid(client, member_uid)
print(user)
# print(user)
group = get_group_by_cn(client, group_cn)
group_members = group['member']
group_members = group['members']
assert len(group_members) == 1
# print(group_members)
@ -86,13 +88,10 @@ def test_add_to_group(ldap_client):
add_group_member(client, group, user)
group = get_group_by_cn(client, group_cn)
group_members = group['member']
group_members = group['members']
assert len(group_members) == 2
assert user['dn'] in group_members
assert user['uid'] in group_members
# print(group_members)
delete_user(client, user)
print(group)
# assert False