Merge branch 'dodo/switch_authens' into 'master'

Switched to authens

See merge request klub-dev-ens/annuaire!19
This commit is contained in:
Tom Hubrecht 2021-02-05 22:02:23 +01:00
commit fb90a7058f
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
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"

View file

@ -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")),
]

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">
{% if user.is_authenticated %}
{% 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 %}
<a href='{% url "cas_ng_login" %}'>{% trans "Se connecter" %}</a>
<a href='{% url "authens:login" %}'>{% trans "Se connecter" %}</a>
{% endif %}
</div>
</div>

View file

@ -2,3 +2,4 @@ django==2.2.*
Pillow
django_cas_ng
python-ldap
authens