Remove attribute mapping
This commit is contained in:
parent
7883b2d1c0
commit
4dd11a881e
|
@ -14,8 +14,7 @@ class Client:
|
|||
host=None, port=389,
|
||||
encryption=None, ciphers=None, validate=False, ca_certs=None,
|
||||
username=None, password=None,
|
||||
base_dn=None,
|
||||
attribute_id='uid', attribute_mail='mail'):
|
||||
base_dn=None):
|
||||
log.info("Initializing LDAP Client.")
|
||||
|
||||
self.host = host
|
||||
|
@ -31,9 +30,6 @@ class Client:
|
|||
|
||||
self.base_dn = base_dn
|
||||
|
||||
self.attribute_id = attribute_id
|
||||
self.attribute_mail = attribute_mail
|
||||
|
||||
self.connection_lock = Lock()
|
||||
self.connection = make_connection(host=self.host, port=self.port,
|
||||
encryption=self.encryption,
|
||||
|
|
|
@ -8,7 +8,7 @@ log = get_logger(__name__)
|
|||
def get_entry_by_uid(client, 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_))
|
||||
|
||||
response_id = client.connection.search(
|
||||
|
|
|
@ -1,26 +1,22 @@
|
|||
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):
|
||||
shown_keys = ['uid', 'mail', 'createTimestamp', 'modifyTimestamp']
|
||||
|
||||
entry = get_entry_by_uid(client, uid)
|
||||
|
||||
if not entry:
|
||||
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
|
||||
for k, v in entry['attributes'].items()
|
||||
if k in mapping.keys()}
|
||||
user = {k: flatten(attr)
|
||||
for k, attr in entry['attributes'].items()
|
||||
if k in shown_keys}
|
||||
|
||||
return flatten_attributes(user)
|
||||
return user
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
def flatten_attributes(d):
|
||||
return {k: (v[0] if isinstance(v, list) else v)
|
||||
for k, v in d.items()}
|
|
@ -11,8 +11,7 @@ def ldap_client():
|
|||
validate=False,
|
||||
username='uid=phi,ou=Services,dc=unit,dc=macaomilano,dc=org',
|
||||
password='phi',
|
||||
base_dn='dc=unit,dc=macaomilano,dc=org',
|
||||
attribute_id='uid', attribute_mail='mail')
|
||||
base_dn='dc=unit,dc=macaomilano,dc=org')
|
||||
client.open()
|
||||
yield client
|
||||
client.close()
|
||||
|
|
Loading…
Reference in New Issue
Block a user