Switched to authens

This commit is contained in:
Dorian Lesbre 2021-02-05 22:02:23 +01:00 committed by Tom Hubrecht
parent 9d78764e06
commit bba2d096e6
5 changed files with 30 additions and 11 deletions

View file

@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/dev/ref/settings/
""" """
import os import os
from django.urls import reverse_lazy
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
@ -56,7 +57,7 @@ INSTALLED_APPS = [
"django.contrib.sessions", "django.contrib.sessions",
"django.contrib.messages", "django.contrib.messages",
"django.contrib.staticfiles", "django.contrib.staticfiles",
"django_cas_ng", "authens",
"fiches", "fiches",
] ]
@ -91,7 +92,7 @@ TEMPLATES = [
AUTHENTICATION_BACKENDS = ( AUTHENTICATION_BACKENDS = (
"django.contrib.auth.backends.ModelBackend", "django.contrib.auth.backends.ModelBackend",
"django_cas_ng.backends.CASBackend", "fiches.backends.BackendFiches",
) )
WSGI_APPLICATION = "annuaire.wsgi.application" WSGI_APPLICATION = "annuaire.wsgi.application"
@ -157,8 +158,8 @@ MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = "/media/" MEDIA_URL = "/media/"
CAS_SERVER_URL = "https://cas.eleves.ens.fr/" LOGIN_URL = reverse_lazy("authens:login")
LOGOUT_REDIRECT_URL = reverse_lazy("home")
CAS_VERSION = "2" AUTHENS_USE_OLDCAS = False
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"

View file

@ -18,13 +18,11 @@ from django.contrib import admin
from django.urls import path, include from django.urls import path, include
from django.conf.urls.static import static from django.conf.urls.static import static
from fiches.views import BirthdayView, HomeView from fiches.views import BirthdayView, HomeView
import django_cas_ng.views as cas_views
urlpatterns = [ urlpatterns = [
path("", include("fiches.urls")), path("", include("fiches.urls")),
path("admin/", admin.site.urls), path("admin/", admin.site.urls),
path("accounts/login/", cas_views.LoginView.as_view(), name="cas_ng_login"), path("authens/", include("authens.urls")),
path("logout", cas_views.LogoutView.as_view(), name="cas_ng_logout"),
path("i18n/", include("django.conf.urls.i18n")), path("i18n/", include("django.conf.urls.i18n")),
] ]

19
fiches/backends.py Normal file
View file

@ -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

View file

@ -50,9 +50,9 @@
<div id="account-area"> <div id="account-area">
{% if user.is_authenticated %} {% if user.is_authenticated %}
{% blocktrans %}Connecté en tant que <span class="clipper">{{ user }}</span>{% endblocktrans %}<br /> {% blocktrans %}Connecté en tant que <span class="clipper">{{ user }}</span>{% endblocktrans %}<br />
<a href='{% url "cas_ng_logout" %}'>{% trans "Se déconnecter" %}</a> <a href='{% url "authens:logout" %}'>{% trans "Se déconnecter" %}</a>
{% else %} {% else %}
<a href='{% url "cas_ng_login" %}'>{% trans "Se connecter" %}</a> <a href='{% url "authens:login" %}'>{% trans "Se connecter" %}</a>
{% endif %} {% endif %}
</div> </div>
</div> </div>

View file

@ -1,4 +1,5 @@
django==2.2.* django==2.2.*
Pillow Pillow
django_cas_ng django_cas_ng
python-ldap python-ldap
authens