annuaire-eleves/fiches/backends.py

20 lines
739 B
Python
Raw Normal View History

2021-02-05 22:02:23 +01:00
from authens.backends import ENSCASBackend, ENSCASError
from authens.utils import parse_entrance_year
from fiches.models import Profile
class BackendFiches(ENSCASBackend):
"""Wrapper around AuthENS's CAS authentication backend.
Ensures the required field promotion year is non-empty"""
def create_user(self, username, attributes):
"""create a CAS user
overwrite to add required promotion"""
user = super().create_user(username, attributes)
entrance_year = parse_entrance_year(attributes.get("homeDirectory"))
if entrance_year is None:
raise ENSCASError("Entrance year not available")
Profile.objects.create(user=user, promotion=entrance_year)
return user