On utilise les fonctions déjà présentes
This commit is contained in:
parent
ef0d42a9c7
commit
939c18f45f
1 changed files with 10 additions and 46 deletions
|
@ -1,51 +1,11 @@
|
||||||
import ldap
|
from .management.commands._ldap import ClipperLDAP
|
||||||
|
|
||||||
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):
|
def get_ldap_infos(clipper_login):
|
||||||
ldap_server = settings.LDAP["SPI"]
|
ldap = ClipperLDAP()
|
||||||
ldap_obj = ldap.initialize("{PROTOCOL}://{URL}:{PORT}".format(**ldap_server))
|
|
||||||
try:
|
try:
|
||||||
res = ldap_obj.search_s(
|
res = ldap.search("(uid={})".format(clipper_login))
|
||||||
"dc=spi,dc=ens,dc=fr",
|
except Exception:
|
||||||
ldap.SCOPE_SUBTREE,
|
|
||||||
"(uid={})".format(clipper_login),
|
|
||||||
["uid", "cn", "homeDirectory"],
|
|
||||||
)
|
|
||||||
except ldap.SERVER_DOWN:
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if not res:
|
if not res:
|
||||||
|
@ -54,6 +14,10 @@ def get_ldap_infos(clipper_login):
|
||||||
if len(res) != 1:
|
if len(res) != 1:
|
||||||
raise RuntimeError("LDAP returned too many results: {}".format(res))
|
raise RuntimeError("LDAP returned too many results: {}".format(res))
|
||||||
(res,) = res
|
(res,) = res
|
||||||
promo, dept = parse_dept(extract_ldap_info(res, "homeDirectory"))
|
promo, dept = ldap.parse_dept(ldap.extract_ldap_info(res, "homeDirectory"))
|
||||||
|
|
||||||
return {"name": extract_ldap_info(res, "cn"), "promo": promo, "dept": dept}
|
return {
|
||||||
|
"name": ldap.extract_ldap_info(res, "cn"),
|
||||||
|
"promo": promo,
|
||||||
|
"dept": dept,
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue