Search by organizational unit
This commit is contained in:
parent
3e299f96b1
commit
dbf6ca966f
|
@ -14,7 +14,7 @@ def get_entry_by_uid(client, uid):
|
||||||
response_id = client.connection.search(
|
response_id = client.connection.search(
|
||||||
client.base_dn, filter_,
|
client.base_dn, filter_,
|
||||||
search_scope='SUBTREE',
|
search_scope='SUBTREE',
|
||||||
attributes=[ALL_ATTRIBUTES, ALL_OPERATIONAL_ATTRIBUTES]
|
attributes=[ALL_ATTRIBUTES]
|
||||||
)
|
)
|
||||||
|
|
||||||
response, result, request = client.connection.get_response(
|
response, result, request = client.connection.get_response(
|
||||||
|
@ -34,3 +34,22 @@ def get_entry_by_uid(client, uid):
|
||||||
.format(len(response)))
|
.format(len(response)))
|
||||||
|
|
||||||
return response[0]
|
return response[0]
|
||||||
|
|
||||||
|
|
||||||
|
def get_entries_by_ou(client, ou):
|
||||||
|
log.info("Searching entries with organizational unit: {}".format(ou))
|
||||||
|
|
||||||
|
dn = 'ou={},{}'.format(ou, client.base_dn)
|
||||||
|
log.debug("Search dn: {}".format(dn))
|
||||||
|
|
||||||
|
response_id = client.connection.search(
|
||||||
|
dn, '(objectclass=person)',
|
||||||
|
search_scope='SUBTREE',
|
||||||
|
attributes=[ALL_ATTRIBUTES]
|
||||||
|
)
|
||||||
|
|
||||||
|
response, result, request = client.connection.get_response(
|
||||||
|
response_id, get_request=True
|
||||||
|
)
|
||||||
|
|
||||||
|
return response
|
||||||
|
|
|
@ -1,22 +1,32 @@
|
||||||
from phi.ldap.entry import get_entry_by_uid
|
from phi.ldap.entry import get_entry_by_uid, get_entries_by_ou
|
||||||
|
|
||||||
|
shown_keys = ['uid', 'mail', 'createTimestamp', 'modifyTimestamp']
|
||||||
|
|
||||||
|
|
||||||
|
def flatten_attribute(attr):
|
||||||
|
if isinstance(attr, list) and len(attr)==1:
|
||||||
|
return attr[0]
|
||||||
|
else:
|
||||||
|
return attr
|
||||||
|
|
||||||
|
|
||||||
|
def flatten_user(entry):
|
||||||
|
return {k: flatten_attribute(attr)
|
||||||
|
for k, attr in entry['attributes'].items()
|
||||||
|
if k in shown_keys}
|
||||||
|
|
||||||
|
|
||||||
def get_user_by_uid(client, uid):
|
def get_user_by_uid(client, uid):
|
||||||
shown_keys = ['uid', 'mail', 'createTimestamp', 'modifyTimestamp']
|
|
||||||
|
|
||||||
entry = get_entry_by_uid(client, uid)
|
entry = get_entry_by_uid(client, uid)
|
||||||
|
|
||||||
if not entry:
|
if not entry:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def flatten(attr):
|
return flatten_user(entry)
|
||||||
if isinstance(attr, list) and len(attr)==1:
|
|
||||||
return attr[0]
|
|
||||||
else:
|
|
||||||
return attr
|
|
||||||
|
|
||||||
user = {k: flatten(attr)
|
|
||||||
for k, attr in entry['attributes'].items()
|
|
||||||
if k in shown_keys}
|
|
||||||
|
|
||||||
return user
|
def get_users_by_ou(client, ou):
|
||||||
|
entries = get_entries_by_ou(client, ou)
|
||||||
|
users = [flatten_user(entry) for entry in entries]
|
||||||
|
|
||||||
|
return users
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
from phi.ldap.user import get_user_by_uid
|
from phi.ldap.user import get_user_by_uid, get_users_by_ou
|
||||||
|
|
||||||
|
|
||||||
def test_connection(ldap_client):
|
def test_connection(ldap_client):
|
||||||
|
@ -10,3 +10,9 @@ def test_get_user_by_id(ldap_client):
|
||||||
entry = get_user_by_uid(ldap_client, 'conte_mascetti')
|
entry = get_user_by_uid(ldap_client, 'conte_mascetti')
|
||||||
assert entry['uid'] == 'conte_mascetti'
|
assert entry['uid'] == 'conte_mascetti'
|
||||||
assert entry['mail'] == 'rmascetti@autistici.org'
|
assert entry['mail'] == 'rmascetti@autistici.org'
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_users_by_ou(ldap_client):
|
||||||
|
users = get_users_by_ou(ldap_client, 'Hackers')
|
||||||
|
|
||||||
|
assert 'conte_mascetti' in [u['uid'] for u in users]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user