Python CSC Electronic Office
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.9 KiB

from typing import Union, List
import ldap3
from zope import component
from zope.interface import implementer
from .UWLDAPRecord import UWLDAPRecord
from ceo_common.interfaces import IUWLDAPService, IConfig
class UWLDAPService:
def __init__(self):
cfg = component.getUtility(IConfig)
self.uwldap_server_url = cfg.get('uwldap_server_url')
self.uwldap_base = cfg.get('uwldap_base')
def _get_conn(self) -> ldap3.Connection:
return ldap3.Connection(
self.uwldap_server_url, auto_bind=True, read_only=True,
def get_user(self, username: str) -> Union[UWLDAPRecord, None]:
conn = self._get_conn()
self.uwldap_base, f'(uid={username})',
attributes=UWLDAPRecord.ldap_attributes, size_limit=1)
if not conn.entries:
return None
return UWLDAPRecord.deserialize_from_ldap(conn.entries[0])
def get_programs_for_users(self, usernames: List[str]) -> List[Union[str, None]]:
filter_str = '(|' + ''.join([f'(uid={uid})' for uid in usernames]) + ')'
programs = [None] * len(usernames)
user_indices = {uid: i for i, uid in enumerate(usernames)}
conn = self._get_conn()
self.uwldap_base, filter_str, attributes=['ou', 'uid'],
for entry in conn.entries:
# some records in UWLDAP have two UIDs because UIDs
# were historically limited to 8 characters
uid = None
for uw_uid in entry.uid.values:
if uw_uid in user_indices:
uid = uw_uid
# sanity check
assert uid is not None
idx = user_indices[uid]
program = entry.ou.value
if program:
programs[idx] = program
return programs