1
0
mirror of https://github.com/kakwa/ldapcherry synced 2024-11-22 09:24:21 +01:00

cleaner way to merge user's attributes from different backends

This commit is contained in:
kakwa 2015-10-20 20:05:22 +02:00
parent 1fd76a9485
commit 9ecd97a8d0

View File

@ -44,7 +44,7 @@ class LdapCherry(object):
) )
else: else:
cherrypy.log.error( cherrypy.log.error(
msg="uncatched exception: [%(e)s]" % {'e': str(e)}, msg="uncaught exception: [%(e)s]" % {'e': str(e)},
severity=logging.ERROR severity=logging.ERROR
) )
# log the traceback as 'debug' # log the traceback as 'debug'
@ -424,6 +424,17 @@ class LdapCherry(object):
) )
exit(1) exit(1)
def _merge_user_attrs(self, attrs_backend, attrs_out, backend_name):
""" merge attributes from one backend search to the attributes dict
output
"""
for attr in attrs_backend:
if attr in self.attributes.backend_attributes[backend_name]:
attrid = self.attributes.backend_attributes[backend_name][attr]
if attrid not in attrs_out:
attrs_out[attrid] = attrs_backend[attr]
def _search(self, searchstring): def _search(self, searchstring):
""" search users """ search users
@str searchstring: search string @str searchstring: search string
@ -437,11 +448,7 @@ class LdapCherry(object):
for u in tmp: for u in tmp:
if u not in ret: if u not in ret:
ret[u] = {} ret[u] = {}
for attr in tmp[u]: self._merge_user_attrs(tmp[u], ret[u], b)
if attr in self.attributes.backend_attributes[b]:
attrid = self.attributes.backend_attributes[b][attr]
if attr not in ret[u]:
ret[u][attrid] = tmp[u][attr]
return ret return ret
def _get_user(self, username): def _get_user(self, username):
@ -458,11 +465,7 @@ class LdapCherry(object):
except UserDoesntExist as e: except UserDoesntExist as e:
self._handle_exception(e) self._handle_exception(e)
tmp = {} tmp = {}
for attr in tmp: self._merge_user_attrs(tmp, ret, b)
if attr in self.attributes.backend_attributes[b]:
attrid = self.attributes.backend_attributes[b][attr]
if attr not in ret:
ret[attrid] = tmp[attr]
cherrypy.log.error( cherrypy.log.error(
msg="user '" + username + "' attributes " + str(ret), msg="user '" + username + "' attributes " + str(ret),