Skip to content
Snippets Groups Projects
Commit ea1682d0 authored by artem's avatar artem Committed by jakob
Browse files

searching for users in ldap by dict-specified arguments

parent e49ff17e
No related branches found
No related tags found
No related merge requests found
...@@ -7,28 +7,16 @@ class LdapHandler: ...@@ -7,28 +7,16 @@ class LdapHandler:
self.connection = ldap3.Connection(server, auto_bind=True) self.connection = ldap3.Connection(server, auto_bind=True)
self.default_context = default_context self.default_context = default_context
def get_ldap_user(self, search_filter: dict, silent: bool = False, context: str = '__USEDEFAULT__',
def get_ldap_users(self, user_list: list, silent: bool = False, context: str = '__USEDEFAULT__'): attributes: list = ['sn', 'gn', 'objectclass']):
results = [] if context == '__USEDEFAULT__':
if context == '__USEDEFAULT':
search_context = self.default_context search_context = self.default_context
else: else:
search_context = context search_context = context
i = 1
for user in user_list: search_string = "(&"
user = user.strip() for key, value in search_filter.items():
if not silent: search_string += '(' + key + '=' + value + ')'
print("%s (%d/%d)" % (user, i, len(results))) search_string += ')'
search_no = self.ldap.search(search_context, ldap3.SCOPE_SUBTREE, "uid=%s" % user, None)
search_res = self.ldap.result(search_no, 0) self.connection.search(search_context, search_string, attributes=attributes)
if search_res[0] == 100:
dn = search_res[1][0][0]
name = search_res[1][0][1]['cn'][0]
mail = search_res[1][0][1]['mail'][0]
location = search_res[1][0][1]['l'][0]
results.append({'dn': dn, 'username': user, 'name': name, 'mail': mail, 'location': location})
return results
def connect(ldaphost: str, default_context: str = '', ssl: bool = False) -> LdapHandler:
return LdapHandler(ldaphost, default_context, ssl)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment