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

Lots of LDAP changes/fixes

parent dbcc087b
No related branches found
No related tags found
No related merge requests found
import ldap3 import ldap3
class LdapHandler:
class LdapHandler:
def __init__(self, ldaphost: str, default_context: str = '', ssl: bool = False): def __init__(self, ldaphost: str, default_context: str = '', ssl: bool = False):
server = ldap3.Server(ldaphost, use_ssl = ssl, get_info=ldap3.ALL) server = ldap3.Server(ldaphost, use_ssl=ssl, get_info=ldap3.ALL)
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 = True, context: str = '__USEDEFAULT__', def get_ldap_entities(self, search_filters: list, silent: bool = True, context: str = None,
attributes: list = ['sn', 'gn', 'objectclass']): attributes: set = ['sn', 'gn', 'objectclass']) -> list:
if context == '__USEDEFAULT__': if context is None:
search_context = self.default_context search_context = self.default_context
else: else:
search_context = context search_context = context
first = True
search_string = "(&" search_string = "(|"
for key, value in search_filter.items(): for search_filter in search_filters:
search_string += '(' + key + '=' + value + ')' # if first:
# first = False
# else:
# search_string += '||'
search_string += "(&"
for key, value in search_filter.items():
search_string += '(' + key + '=' + value + ')'
search_string += ")"
search_string += ')' search_string += ')'
self.connection.search(search_context, search_string, attributes=attributes) 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()))
users = []
for entry in query:
user = default_vars.copy()
for attr, ld in attributes.items():
user[attr] = entry[ld].encode('utf8')
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