play-daemon/daemon/ldap.py
Erik Thuning 645d2cd955 Changed Ldap.get_name() to return displayName instead of cn.
This due to cn possibly havingm ultiple values.
2022-09-07 11:09:57 +02:00

31 lines
938 B
Python

from ldap3 import Connection, ObjectDef, Reader
from ldap3.core.exceptions import LDAPSocketSendError
class Ldap:
def __init__(self, conf):
self.base = conf['base_dn']
self.url = conf['url']
self.__setup_connection()
def __setup_connection(self):
self.conn = Connection(self.url, auto_bind=True)
self.pdef = ObjectDef('inetOrgPerson', self.conn)
def search(self, term):
r = Reader(self.conn, self.pdef, self.base, term)
try:
r.search()
except LDAPSocketSendError as e:
self.__setup_connection()
return self.search(term)
return r
def get_name(self, uid):
r = self.search('uid:'+uid)
if len(r) == 0:
return uid
if len(r) > 1:
m = f'Multiple hits when searching for "{uid}" in LDAP ({self.url})'
raise Exception(m)
return str(r[0].displayName)