play-daemon-threaded/pipeline/ldap.py
Stefan Nenzén 30361abdd9 VisibilityHandler (#3)
Create a VisibilityHandler

Co-authored-by: nenzen <stefan@nenzen.com>
Reviewed-on: #3
Reviewed-by: erth9960 <thuning@dsv.su.se>
Co-authored-by: Stefan Nenzén <nenzen@dsv.su.se>
Co-committed-by: Stefan Nenzén <nenzen@dsv.su.se>
2024-03-27 14:54:33 +01:00

26 lines
749 B
Python

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