Remove attribute mapping

This commit is contained in:
User Identifier 2020-09-30 16:38:31 +02:00
parent 7883b2d1c0
commit 4dd11a881e
5 changed files with 14 additions and 26 deletions

View File

@ -14,8 +14,7 @@ class Client:
host=None, port=389, host=None, port=389,
encryption=None, ciphers=None, validate=False, ca_certs=None, encryption=None, ciphers=None, validate=False, ca_certs=None,
username=None, password=None, username=None, password=None,
base_dn=None, base_dn=None):
attribute_id='uid', attribute_mail='mail'):
log.info("Initializing LDAP Client.") log.info("Initializing LDAP Client.")
self.host = host self.host = host
@ -31,9 +30,6 @@ class Client:
self.base_dn = base_dn self.base_dn = base_dn
self.attribute_id = attribute_id
self.attribute_mail = attribute_mail
self.connection_lock = Lock() self.connection_lock = Lock()
self.connection = make_connection(host=self.host, port=self.port, self.connection = make_connection(host=self.host, port=self.port,
encryption=self.encryption, encryption=self.encryption,

View File

@ -8,7 +8,7 @@ log = get_logger(__name__)
def get_entry_by_uid(client, uid): def get_entry_by_uid(client, uid):
log.info("Searching entry with identifier: {}".format(uid)) log.info("Searching entry with identifier: {}".format(uid))
filter_ = "({}={})".format(client.attribute_id, uid) filter_ = "({}={})".format('uid', uid)
log.debug("Search filter: {}".format(filter_)) log.debug("Search filter: {}".format(filter_))
response_id = client.connection.search( response_id = client.connection.search(

View File

@ -1,26 +1,22 @@
from phi.ldap.entry import get_entry_by_uid from phi.ldap.entry import get_entry_by_uid
from phi.ldap.utils import flatten_attributes
def user_attributes_mapping(client):
return {
client.attribute_id: 'uid',
client.attribute_mail: 'mail',
'createTimestamp': 'created_at',
'modifyTimestamp': 'modified_at'
}
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
mapping = user_attributes_mapping(client) def flatten(attr):
if isinstance(attr, list) and len(attr)==1:
return attr[0]
else:
return attr
user = {mapping[k]: v user = {k: flatten(attr)
for k, v in entry['attributes'].items() for k, attr in entry['attributes'].items()
if k in mapping.keys()} if k in shown_keys}
return flatten_attributes(user) return user

View File

@ -1,3 +0,0 @@
def flatten_attributes(d):
return {k: (v[0] if isinstance(v, list) else v)
for k, v in d.items()}

View File

@ -11,8 +11,7 @@ def ldap_client():
validate=False, validate=False,
username='uid=phi,ou=Services,dc=unit,dc=macaomilano,dc=org', username='uid=phi,ou=Services,dc=unit,dc=macaomilano,dc=org',
password='phi', password='phi',
base_dn='dc=unit,dc=macaomilano,dc=org', base_dn='dc=unit,dc=macaomilano,dc=org')
attribute_id='uid', attribute_mail='mail')
client.open() client.open()
yield client yield client
client.close() client.close()