Commit 72b3e228 authored by Antoine Bernard's avatar Antoine Bernard

[pw_reset] Ajout changement de password et confirmation.

parent dbff8cdd
{% extends "template.html" %}
{% load i18n %}
{% block head %}
{% endblock head %}
{% block title %}{{ block.super }} : {% trans "Réinitialisation du mot de passe" %}{% endblock %}
{% block h1 %}{% trans "Mot de passe réinitialisé" %}{% endblock %}
{% load staticfiles %}
{% block content %}
<p>{% trans "Votre mot de passe a été réinitialisé. Vous pouvez maintenant vous connecter." %}</p>
{% endblock %}
{% extends "template.html" %}
{% load i18n %}
{% block content %}
<h3>{% block confirm_title %}{{ confirm_title }}{% endblock %}</h3>
<p>{% block confirm_message %}
{{ confirm_message }}
{% endblock %}</p>
<form class="form-full-width" method="post">{% csrf_token %}
<div class="error-container">
{{ form.non_field_errors }}
</div>
<div class="row">
<div class="six columns">
{{ form.newpasswd1.label_tag }}
{{ form.newpasswd1 }}
{{ form.newpasswd1.errors }}
</div>
<div class="six columns">
{{ form.newpasswd2.label_tag }}
{{ form.newpasswd2 }}
{{ form.newpasswd2.errors }}
</div>
<footer>
<input type="submit" value="{% trans "Enregistrer" %}">
</footer>
</form>
{% endblock %}
......@@ -30,4 +30,10 @@ urlpatterns = [
url(r'^done/$',
views.password_reset_done,
name="password_reset_done"),
url(r'^reset/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$',
views.password_reset_confirm,
name="password_reset_confirm"),
url(r'^reset/done/$',
views.password_reset_complete,
name="password_reset_complete"),
]
......@@ -9,6 +9,7 @@ from intranet import settings
#: Import de fonctions utiles
from django.shortcuts import render, redirect
from django.core.urlresolvers import reverse_lazy
from compte.views import get_admin_luser
#: Import des views Django
from django.views.generic.detail import DetailView
......@@ -22,11 +23,12 @@ from django.contrib import messages
#: Import des formulaires
from password_reset.forms import EmailForm, UsernameForm
from compte.forms import BasePassForm
#: Tokenization
from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode, int_to_base36
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode, int_to_base36
from django.utils.crypto import salted_hmac
#Copié depuis Django1.11
......@@ -131,3 +133,68 @@ class PasswordResetDoneView(DetailView):
)
password_reset_done = PasswordResetDoneView.as_view()
class PasswordResetConfirmView(FormView):
"""
Vue permettant le changement de mot de passe.
On vérifie la validité du token.
"""
token_generator = my_token_generator
form_class = BasePassForm
success_url = reverse_lazy("password_reset:password_reset_complete")
template_name = 'password_reset/password_reset_confirm.html'
def get(self, request, *args, **kwargs):
if not request.user.is_anonymous():
messages.error(request, _(u"Vous devez vous déconnecter pour accédez à cette page"))
return redirect(reverse_lazy('index'))
return render(
request,
"password_reset/password_reset_confirm.html",
{'form' : self.form_class,},
)
def post(self, request, uidb64=None, token=None, *arg, **kwargs):
"""
View that checks the hash in a password reset link and presents a
form for entering a new password.
"""
form = self.form_class(request.POST)
assert uidb64 is not None and token is not None
try:
uid = urlsafe_base64_decode(uidb64)
# On va avoir besoin d'écrire pour changer le mot de passe.
user = get_admin_luser(request, uid, 'adh', mode='w')
except (TypeError, ValueError, OverflowError):
user = None
# Si `user` existe et le `token` n'a pas expiré.
if user is not None and self.token_generator.check_token(user, token):
if form.is_valid() and form.apply(user):
user.history_gen()
user.save()
messages.success(request, _(u'Le mot de passe a été réinitialisé'))
return self.form_valid(form)
else:
messages.error(request, _(u'La réinitialisation de mot de passe a échoué'))
return self.form_invalid(form)
else:
messages.error(request, _(u"Le token de réinitialisation n'est plus valide."))
return redirect(reverse("password_reset"))
password_reset_confirm = PasswordResetConfirmView.as_view()
class PasswordResetCompleteView(DetailView):
"""
Vue finale.
"""
def get(self, request, *args, **kwargs):
if not request.user.is_anonymous():
messages.error(request, _(u"Vous devez vous déconnecter pour accédez à cette page"))
return redirect(reverse_lazy('index'))
return render(
request,
"password_reset/password_reset_complete.html",
)
password_reset_complete = PasswordResetCompleteView.as_view()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment