2024-07-10 13:51:24 +02:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
2020-12-21 00:07:07 +01:00
|
|
|
from django import forms
|
|
|
|
from django.contrib.auth import authenticate
|
|
|
|
from django.contrib.auth import forms as auth_forms
|
2021-04-26 17:54:07 +02:00
|
|
|
from django.core.validators import validate_email
|
2020-12-21 00:07:07 +01:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
2024-07-10 13:51:24 +02:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from elections.typing import User
|
|
|
|
else:
|
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
User = get_user_model()
|
2021-01-26 14:26:35 +01:00
|
|
|
|
2020-12-21 00:07:07 +01:00
|
|
|
|
|
|
|
class ElectionAuthForm(forms.Form):
|
2020-12-21 13:38:18 +01:00
|
|
|
"""Adapts Django's AuthenticationForm to allow for an election specific login."""
|
2020-12-21 00:07:07 +01:00
|
|
|
|
|
|
|
login = auth_forms.UsernameField(label=_("Identifiant"), max_length=255)
|
2020-12-21 14:35:12 +01:00
|
|
|
password = forms.CharField(
|
|
|
|
label=_("Mot de passe"),
|
|
|
|
strip=False,
|
|
|
|
widget=forms.PasswordInput(attrs={"autocomplete": "current-password"}),
|
|
|
|
)
|
2020-12-21 00:07:07 +01:00
|
|
|
election_id = forms.IntegerField(widget=forms.HiddenInput())
|
|
|
|
|
|
|
|
def __init__(self, request=None, *args, **kwargs):
|
|
|
|
self.request = request
|
|
|
|
self.user_cache = None
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def clean(self):
|
2020-12-21 13:38:18 +01:00
|
|
|
login = self.cleaned_data.get("login")
|
2020-12-21 00:07:07 +01:00
|
|
|
password = self.cleaned_data.get("password")
|
|
|
|
election_id = self.cleaned_data.get("election_id")
|
|
|
|
|
|
|
|
if login is not None and password:
|
|
|
|
self.user_cache = authenticate(
|
|
|
|
self.request,
|
|
|
|
login=login,
|
|
|
|
password=password,
|
|
|
|
election_id=election_id,
|
|
|
|
)
|
|
|
|
if self.user_cache is None:
|
|
|
|
raise self.get_invalid_login_error()
|
|
|
|
|
|
|
|
return self.cleaned_data
|
|
|
|
|
|
|
|
def get_user(self):
|
|
|
|
# Necessary API for LoginView
|
|
|
|
return self.user_cache
|
|
|
|
|
|
|
|
def get_invalid_login_error(self):
|
|
|
|
return forms.ValidationError(
|
|
|
|
_(
|
|
|
|
"Aucun·e électeur·ice avec cet identifiant et mot de passe n'existe "
|
|
|
|
"pour cette élection. Vérifiez que les informations rentrées sont "
|
|
|
|
"correctes, les champs sont sensibles à la casse."
|
|
|
|
),
|
|
|
|
code="invalid_login",
|
|
|
|
)
|
2021-01-26 14:26:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
class PwdResetForm(auth_forms.PasswordResetForm):
|
|
|
|
"""Restricts the search for password users, i.e. whose username starts with pwd__."""
|
|
|
|
|
|
|
|
def get_users(self, email):
|
|
|
|
users = super().get_users(email)
|
|
|
|
return (u for u in users if u.username.split("__")[0] == "pwd")
|
2021-04-26 17:54:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
class PwdUserForm(forms.ModelForm):
|
|
|
|
"""
|
|
|
|
Allows for the creation of a Password Account given the email, base username and full name.
|
|
|
|
"""
|
|
|
|
|
|
|
|
email = forms.EmailField(
|
|
|
|
label=_("Email"), required=True, validators=[validate_email]
|
|
|
|
)
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
# On rajoute le préfixe signifiant qu'on crée un compte avec mot de passe
|
|
|
|
cleaned_data = super().clean()
|
|
|
|
cleaned_data["username"] = "pwd__" + cleaned_data["username"]
|
|
|
|
return cleaned_data
|
|
|
|
|
|
|
|
class Meta:
|
2021-05-29 11:58:42 +02:00
|
|
|
model = User
|
2021-04-26 17:54:07 +02:00
|
|
|
fields = ["username", "full_name", "email"]
|
2021-07-13 02:41:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
class UserAdminForm(forms.Form):
|
|
|
|
"""
|
|
|
|
Allows to select an user and give them some admin permissions
|
|
|
|
"""
|
|
|
|
|
|
|
|
username = forms.CharField(label=_("Nom d'utilisateur"), max_length=150)
|
|
|
|
|
|
|
|
full_admin = forms.BooleanField(
|
|
|
|
label=_("Passer administrateur de Kadenios"), required=False
|
|
|
|
)
|
|
|
|
faq_admin = forms.BooleanField(
|
|
|
|
label=_("Autoriser à créer des FAQs"), required=False
|
|
|
|
)
|
|
|
|
election_admin = forms.BooleanField(
|
|
|
|
label=_("Autoriser à créer des élections"), required=False
|
|
|
|
)
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
cleaned_data = super().clean()
|
|
|
|
username = cleaned_data["username"]
|
|
|
|
|
|
|
|
if not username[:5] in ["cas__", "pwd__"]:
|
|
|
|
self.add_error(
|
|
|
|
"username",
|
|
|
|
_(
|
|
|
|
"Format de login invalide, seuls les comptes CAS ou avec "
|
|
|
|
"mot de passe sont modifiables"
|
|
|
|
),
|
|
|
|
)
|
|
|
|
elif not User.objects.filter(username=username).exists():
|
|
|
|
self.add_error("username", _("Pas d'utilisateur·rice avec ce login"))
|
|
|
|
|
|
|
|
return cleaned_data
|