From bba2d096e671a308073bb08ea0dddda0a6311cb7 Mon Sep 17 00:00:00 2001 From: Dorian Lesbre Date: Fri, 5 Feb 2021 22:02:23 +0100 Subject: [PATCH] Switched to authens --- annuaire/settings.py | 11 ++++++----- annuaire/urls.py | 4 +--- fiches/backends.py | 19 +++++++++++++++++++ fiches/templates/fiches/base.html | 4 ++-- requirements.txt | 3 ++- 5 files changed, 30 insertions(+), 11 deletions(-) create mode 100644 fiches/backends.py diff --git a/annuaire/settings.py b/annuaire/settings.py index 62cbb30..08fcbb1 100644 --- a/annuaire/settings.py +++ b/annuaire/settings.py @@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/dev/ref/settings/ """ import os +from django.urls import reverse_lazy # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -56,7 +57,7 @@ INSTALLED_APPS = [ "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", - "django_cas_ng", + "authens", "fiches", ] @@ -91,7 +92,7 @@ TEMPLATES = [ AUTHENTICATION_BACKENDS = ( "django.contrib.auth.backends.ModelBackend", - "django_cas_ng.backends.CASBackend", + "fiches.backends.BackendFiches", ) WSGI_APPLICATION = "annuaire.wsgi.application" @@ -157,8 +158,8 @@ MEDIA_ROOT = os.path.join(BASE_DIR, "media") MEDIA_URL = "/media/" -CAS_SERVER_URL = "https://cas.eleves.ens.fr/" - -CAS_VERSION = "2" +LOGIN_URL = reverse_lazy("authens:login") +LOGOUT_REDIRECT_URL = reverse_lazy("home") +AUTHENS_USE_OLDCAS = False EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" diff --git a/annuaire/urls.py b/annuaire/urls.py index 8d2de91..dadfa71 100644 --- a/annuaire/urls.py +++ b/annuaire/urls.py @@ -18,13 +18,11 @@ from django.contrib import admin from django.urls import path, include from django.conf.urls.static import static from fiches.views import BirthdayView, HomeView -import django_cas_ng.views as cas_views urlpatterns = [ path("", include("fiches.urls")), path("admin/", admin.site.urls), - path("accounts/login/", cas_views.LoginView.as_view(), name="cas_ng_login"), - path("logout", cas_views.LogoutView.as_view(), name="cas_ng_logout"), + path("authens/", include("authens.urls")), path("i18n/", include("django.conf.urls.i18n")), ] diff --git a/fiches/backends.py b/fiches/backends.py new file mode 100644 index 0000000..dc00480 --- /dev/null +++ b/fiches/backends.py @@ -0,0 +1,19 @@ +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 diff --git a/fiches/templates/fiches/base.html b/fiches/templates/fiches/base.html index 8d1eecb..72bea43 100644 --- a/fiches/templates/fiches/base.html +++ b/fiches/templates/fiches/base.html @@ -50,9 +50,9 @@
{% if user.is_authenticated %} {% blocktrans %}Connecté en tant que {{ user }}{% endblocktrans %}
- {% trans "Se déconnecter" %} + {% trans "Se déconnecter" %} {% else %} - {% trans "Se connecter" %} + {% trans "Se connecter" %} {% endif %}
diff --git a/requirements.txt b/requirements.txt index 0192af9..c6e40a1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ django==2.2.* Pillow django_cas_ng -python-ldap \ No newline at end of file +python-ldap +authens