Skip to content
Snippets Groups Projects
Commit efd7efde authored by Jakob Berger's avatar Jakob Berger :key2: Committed by jakob
Browse files

fixed a lot of stuff in ldap (again)

parent c9264cf7
No related branches found
No related tags found
No related merge requests found
import ldap3
from typing import List, Dict
# TODO: add a paginated search feature utilizing the ldap3 paginated search
class LdapHandler:
def __init__(self, ldaphost: str, default_context: str = '', ssl: bool = False):
server = ldap3.Server(ldaphost, use_ssl=ssl, get_info=ldap3.ALL)
......@@ -8,42 +10,37 @@ class LdapHandler:
self.default_context = default_context
def get_ldap_entities(self, search_filters: list, silent: bool = True, context: str = None,
attributes: set = ['sn', 'gn', 'objectclass']) -> list:
attributes: set = ['sn', 'gn', 'objectclass']) -> List[ldap3.Entry]:
if context is None:
search_context = self.default_context
else:
search_context = context
first = True
search_string = "(|"
for search_filter in search_filters:
# if first:
# first = False
# else:
# search_string += '||'
search_string += "(&"
for key, value in search_filter.items():
search_string += '(' + key + '=' + value + ')'
search_string += ")"
search_string += ')'
self.connection.search(search_context, search_string, attributes=attributes)
return self.connection.entries
def get_ldap_users(self, usernames: list,
attributes: dict = {'username': 'uid', 'name': 'cn', 'mail': 'mail', 'location': 'l',
'extern_uid': 'dn'},
default_vars: dict = {'provider': 'ldapmain', 'project_limit': '0', 'confirm': 'false'}) -> list:
""" queries the ldap and prefills the user object for you.
The attributes dictionary maps the names of the user object attributes to the names of the ldap fields"""
query = self.get_ldap_entities([{'uid': username} for username in usernames],
attributes=set(attributes.values()))
def get_ldap_users(self, usernames: List[str],
attributes: dict = {'username': 'uid', 'name': 'cn', 'mail': 'mail'},
default_vars: dict = {'provider': 'ldapmain', 'project_limit': '0', 'confirm': 'false'},
filters: dict = {}) -> List[Dict]:
""" Queries the ldap and prefills the user object for you.
The attributes dictionary maps the names of the user object attributes to the names of the ldap fields
The default_vars dictionary specifies static default values of the user object. Costumize to your needs.
filters is a dicitonary of filters applied to all user searches, for example an objectclass"""
query = [{'uid': username} for username in usernames]
for user_dict in query:
user_dict.update(filters)
results = self.get_ldap_entities(query, attributes=set(attributes.values()))
users = []
for entry in query:
for entry in results:
user = default_vars.copy()
for attr, ld in attributes.items():
user[attr] = entry[ld].encode('utf8')
for user_field, ldap_name in attributes.items():
user[user_field] = entry.entry_attributes_as_dict[ldap_name][0]
users.append(user)
return users
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