Authentification CAS par défaut, permet de choisir entre CAS et mdp lorsque la variable election exite dans le template

This commit is contained in:
Tom Hubrecht 2020-12-21 13:38:18 +01:00
parent fc695b9cc5
commit 416efd5267
7 changed files with 104 additions and 5 deletions

View file

@ -76,7 +76,7 @@ class CreatorOnlyEditMixin(CreatorOnlyMixin, SingleObjectMixin):
return filters return filters
class AdministratorOnlyMixin: class AdministratorOnlyMixin(LoginRequiredMixin):
"""Restreint l'accès aux admins""" """Restreint l'accès aux admins"""

View file

@ -34,6 +34,12 @@ class Election(models.Model):
archived = models.BooleanField(_("archivée"), default=False) archived = models.BooleanField(_("archivée"), default=False)
@property
def preferred_method(self):
if self.restricted:
return "PWD"
return "CAS"
class Meta: class Meta:
ordering = ["-start_date", "-end_date"] ordering = ["-start_date", "-end_date"]

View file

@ -5,7 +5,7 @@ from django.utils.translation import gettext_lazy as _
class ElectionAuthForm(forms.Form): class ElectionAuthForm(forms.Form):
"""Adapts Django's AuthenticationForm to allow for OldCAS login.""" """Adapts Django's AuthenticationForm to allow for an election specific login."""
login = auth_forms.UsernameField(label=_("Identifiant"), max_length=255) login = auth_forms.UsernameField(label=_("Identifiant"), max_length=255)
password = forms.CharField(label=_("Mot de passe"), strip=False) password = forms.CharField(label=_("Mot de passe"), strip=False)
@ -17,7 +17,7 @@ class ElectionAuthForm(forms.Form):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
def clean(self): def clean(self):
login = self.cleaned_data.get("cas_login") login = self.cleaned_data.get("login")
password = self.cleaned_data.get("password") password = self.cleaned_data.get("password")
election_id = self.cleaned_data.get("election_id") election_id = self.cleaned_data.get("election_id")

View file

@ -45,6 +45,8 @@ class LoginSelectView(NextPageMixin, TemplateView):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context["next"] = self.get_next_url() context["next"] = self.get_next_url()
context["method"] = self.request.GET.get("method", "CAS")
context["election_id"] = self.request.GET.get("election_id", None)
return context return context
@ -82,7 +84,7 @@ class PasswordLoginView(auth_views.LoginView):
authentication_form = ElectionAuthForm authentication_form = ElectionAuthForm
def get_initial(self): def get_initial(self):
return {"election_id": self.request.GET.get("election", None)} return {"election_id": self.request.GET.get("election_id", None)}
class LogoutView(auth_views.LogoutView): class LogoutView(auth_views.LogoutView):

View file

@ -1,3 +1,47 @@
{% extends "base.html" %} {% extends "base.html" %}
{% load i18n %} {% load i18n %}
{% block content %}
<h1 class="title">{% trans "Choisissez la méthode de connexion" %}</h1>
<hr>
{# Indications de connexion #}
<div class="message is-warning">
<div class="message-body">
{% if method == "PWD" %}
{% trans "Pour voter lors de cette élection, vous devez vous connecter à l'aide des identifiants reçus par mail. Choisissez la connexion par mot de passe." %}
{% else %}
{% trans "Pour voter lors de cette élection, vous devez vous connecter à l'aide du CAS élève." %}
{% endif %}
</div>
</div>
<div class="tile is-ancestor">
<div class="tile is-parent">
<a class="tile is-child notification is-primary" href="{% url "auth.cas" %}?next={{ next }}">
<div class="subtitle has-text-centered mb-2">
<span class="icon has-text-white">
<i class="fas fa-school"></i>
</span>
<span class="ml-3">{% trans "Connexion via CAS" %}</span>
</div>
</a>
</div>
<div class="tile is-parent">
<a class="tile is-child notification" href="{% url "auth.pwd" %}?next={{ next }}&election_id={{ election_id }}">
<div class="subtitle has-text-centered mb-2">
<span class="icon">
<i class="fas fa-key"></i>
</span>
<span class="ml-3">{% trans "Connexion par mot de passe" %}</span>
</div>
</a>
</div>
</div>
{% endblock %}

View file

@ -1,2 +1,40 @@
{% extends "base.html" %} {% extends "base.html" %}
{% load i18n %} {% load i18n %}
{% block content %}
<h1 class="title">{% trans "Connexion par mot de passe" %}</h1>
<hr>
<div class="columns is-centered">
<div class="column is-two-thirds">
<form action="" method="post">
{% csrf_token %}
{{ form.errors }}
{% include "forms/form.html" with errors=True %}
<div class="field is-grouped is-centered">
<div class="control is-expanded">
<button class="button is-fullwidth is-outlined is-primary is-light">
<span class="icon is-small">
<i class="fas fa-check"></i>
</span>
<span>{% trans "Enregistrer" %}</span>
</button>
</div>
<div class="control">
<a class="button is-primary" href="{% url 'auth.select' %}?next={{ next }}&election_id={{ election_id }}&method={{ method }}">
<span class="icon is-small">
<i class="fas fa-undo-alt"></i>
</span>
<span>{% trans "Retour" %}</span>
</a>
</div>
</div>
</form>
</div>
</div>
{% endblock %}

View file

@ -42,6 +42,7 @@
<h1 class="has-text-primary-light is-size-1 is-family-secondary">Kadenios</h1> <h1 class="has-text-primary-light is-size-1 is-family-secondary">Kadenios</h1>
</div> </div>
</div> </div>
<div class="level-right pr-5"> <div class="level-right pr-5">
{% if user.is_authenticated %} {% if user.is_authenticated %}
<div class="level-item mr-5"> <div class="level-item mr-5">
@ -49,16 +50,24 @@
{% blocktrans with name=user.base_username connection=user.connection_method %}Connecté·e en tant que {{ name }} par {{ connection }}{% endblocktrans %} {% blocktrans with name=user.base_username connection=user.connection_method %}Connecté·e en tant que {{ name }} par {{ connection }}{% endblocktrans %}
</div> </div>
</div> </div>
<div class="level-item ml-3"> <div class="level-item ml-3">
<a class="icon is-size-1 has-text-white" href="{% url 'auth.logout' %}?next={{ view.get_next_url }}"> <a class="icon is-size-1 has-text-white" href="{% url 'auth.logout' %}?next={{ view.get_next_url }}">
<i class="fas fa-sign-out-alt"></i> <i class="fas fa-sign-out-alt"></i>
</a> </a>
</div> </div>
{% else %} {% else %}
<div class="level-item"> <div class="level-item">
<a class="icon is-size-1 has-text-white" href="{% url 'auth.select' %}?next={{ request.path }}"> {% if election %}
<a class="icon is-size-1 has-text-white" href="{% url 'auth.select' %}?next={{ request.path }}&election_id={{ election.id }}&method={{ election.preferred_method }}">
<i class="fas fa-sign-in-alt"></i> <i class="fas fa-sign-in-alt"></i>
</a> </a>
{% else %}
<a class="icon is-size-1 has-text-white" href="{% url 'auth.cas' %}?next={{ request.path }}">
<i class="fas fa-sign-in-alt"></i>
</a>
{% endif %}
</div> </div>
{% endif %} {% endif %}
</div> </div>