Rajoute une page pour créer plus facilement des comptes avec mdp
This commit is contained in:
parent
c73f8a5943
commit
c97f57912b
11 changed files with 108 additions and 19 deletions
|
@ -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"]
|
||||
|
|
|
@ -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
15
shared/auth/utils.py
Normal 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
|
|
@ -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)
|
||||
|
|
31
shared/templates/auth/create-user.html
Normal file
31
shared/templates/auth/create-user.html
Normal file
|
@ -0,0 +1,31 @@
|
|||
{% extends "base.html" %}
|
||||
{% load i18n %}
|
||||
|
||||
|
||||
{% block content %}
|
||||
|
||||
<h1 class="title">{% trans "Créer un compte avec mot de passe" %}</h1>
|
||||
<hr>
|
||||
|
||||
<div class="columns is-centered">
|
||||
<div class="column is-two-thirds">
|
||||
<form action="" method="post">
|
||||
{% csrf_token %}
|
||||
|
||||
{% 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">
|
||||
<i class="fas fa-check"></i>
|
||||
</span>
|
||||
<span>{% trans "Enregistrer" %}</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
|
@ -122,7 +122,7 @@
|
|||
<div class="level is-mobile">
|
||||
<div class="level-item">
|
||||
<div class="tag">
|
||||
{% blocktrans with name=user.get_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>
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue