Rajoute une page pour créer plus facilement des comptes avec mdp

This commit is contained in:
Tom Hubrecht 2021-04-26 17:54:07 +02:00
parent c73f8a5943
commit c97f57912b
11 changed files with 108 additions and 19 deletions

View file

@ -2,6 +2,7 @@ from django import forms
from django.contrib.auth import authenticate
from django.contrib.auth import forms as auth_forms
from django.contrib.auth import get_user_model
from django.core.validators import validate_email
from django.utils.translation import gettext_lazy as _
UserModel = get_user_model()
@ -61,3 +62,23 @@ class PwdResetForm(auth_forms.PasswordResetForm):
def get_users(self, email):
users = super().get_users(email)
return (u for u in users if u.username.split("__")[0] == "pwd")
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:
model = UserModel
fields = ["username", "full_name", "email"]

View file

@ -8,4 +8,5 @@ urlpatterns = [
views.ElectionLoginView.as_view(),
name="auth.election",
),
path("pwd-create", views.CreatePwdAccount.as_view(), name="auth.create-account"),
]

15
shared/auth/utils.py Normal file
View file

@ -0,0 +1,15 @@
import random
# #############################################################################
# Fonctions universelles
# #############################################################################
def generate_password(size=15):
random.seed()
alphabet = "abcdefghjkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789"
password = ""
for i in range(size):
password += random.choice(alphabet)
return password

View file

@ -1,6 +1,15 @@
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth import get_user_model
from django.contrib.auth import views as auth_views
from django.contrib.auth.hashers import make_password
from django.urls import reverse_lazy
from django.utils.decorators import method_decorator
from django.views.generic.edit import CreateView
from .forms import ElectionAuthForm
from .forms import ElectionAuthForm, PwdUserForm
from .utils import generate_password
User = get_user_model()
# #############################################################################
# Election Specific Login
@ -17,3 +26,23 @@ class ElectionLoginView(auth_views.LoginView):
def get_context_data(self, **kwargs):
kwargs.update({"election_id": self.kwargs.get("election_id")})
return super().get_context_data(**kwargs)
# #############################################################################
# Creation of Password Accounts
# #############################################################################
@method_decorator(staff_member_required, name="dispatch")
class CreatePwdAccount(CreateView):
model = User
form_class = PwdUserForm
template_name = "auth/create-user.html"
success_url = reverse_lazy("auth.create-account")
def form_valid(self, form):
# On enregistre un mot de passe aléatoire
form.instance.password = make_password(generate_password(32))
# On envoie un mail pour réinitialiser le mot de passe
return super().form_valid(form)