2021-02-06 12:34:02 +01:00
|
|
|
import ldap
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
verbose_depts = {
|
|
|
|
"bio": "Biologie",
|
|
|
|
"chimie": "Chimie",
|
|
|
|
"dec": "Études Cognitives",
|
|
|
|
"geol": "Géosciences",
|
|
|
|
"guests": "Invité·e",
|
|
|
|
"info": "Informatique",
|
|
|
|
"litt": "Lettres",
|
|
|
|
"maths": "Mathématiques",
|
|
|
|
"pei": "PEI",
|
|
|
|
"phy": "Physique",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def parse_dept(home_dir):
|
|
|
|
"""Extrait le département d'entrée d'un·e élève à partir de son dossier.
|
|
|
|
Le dossier a le format `/users/<promo>/<dept>/<login>`.
|
|
|
|
"""
|
|
|
|
users, promo, dept, login = home_dir.split("/")[1:]
|
|
|
|
|
|
|
|
if users != "users":
|
|
|
|
raise ValueError("Invalid home directory")
|
|
|
|
|
|
|
|
# Ça casse en 2100, mais le système de naming de sas aussi...
|
|
|
|
promo = 2000 + int(promo)
|
|
|
|
|
|
|
|
return promo, verbose_depts.get(dept, None)
|
|
|
|
|
|
|
|
|
|
|
|
def extract_ldap_info(entry, field):
|
|
|
|
return entry[1].get(field, [b""])[0].decode("utf-8")
|
|
|
|
|
|
|
|
|
|
|
|
def get_ldap_infos(clipper_login):
|
|
|
|
ldap_server = settings.LDAP["SPI"]
|
|
|
|
ldap_obj = ldap.initialize("{PROTOCOL}://{URL}:{PORT}".format(**ldap_server))
|
2021-02-06 17:04:24 +01:00
|
|
|
try:
|
|
|
|
res = ldap_obj.search_s(
|
|
|
|
"dc=spi,dc=ens,dc=fr",
|
|
|
|
ldap.SCOPE_SUBTREE,
|
|
|
|
"(uid={})".format(clipper_login),
|
|
|
|
["uid", "cn", "homeDirectory"],
|
|
|
|
)
|
|
|
|
except ldap.SERVER_DOWN:
|
|
|
|
return None
|
2021-02-06 12:34:02 +01:00
|
|
|
|
|
|
|
if not res:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if len(res) != 1:
|
|
|
|
raise RuntimeError("LDAP returned too many results: {}".format(res))
|
|
|
|
(res,) = res
|
|
|
|
promo, dept = parse_dept(extract_ldap_info(res, "homeDirectory"))
|
|
|
|
|
|
|
|
return {"name": extract_ldap_info(res, "cn"), "promo": promo, "dept": dept}
|