Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix Password Reset/Set up
michalefb9b27eb413
diff --git a/saleor/dashboard/staff/views.py b/saleor/dashboard/staff/views.pyindex 694131d4fc..7b8c45111f 100644--- a/saleor/dashboard/staff/views.py+++ b/saleor/dashboard/staff/views.py@@ -42,6 +42,10 @@ def staff_create(request):form = StaffForm(request.POST or None, instance=staff)if form.is_valid():form.save()+ password = User.objects.make_random_password()+ staff.set_password(password)+ staff.save()+msg = pgettext_lazy('Dashboard message', 'Added staff member %s') % staffmessages.success(request, msg)diff --git a/saleor/registration/forms.py b/saleor/registration/forms.pyindex df8e01509f..9386c511b3 100644--- a/saleor/registration/forms.py+++ b/saleor/registration/forms.py@@ -1,7 +1,11 @@from django import formsfrom django.contrib.auth import forms as django_forms+from django.core.urlresolvers import reverse+from django.http.request import HttpRequestfrom django.utils.translation import pgettext+from templated_email import send_templated_mail+from saleor.userprofile.models import User@@ -45,3 +49,25 @@ class SetPasswordForm(django_forms.SetPasswordForm):def __init__(self, *args, **kwargs):super(SetPasswordForm, self).__init__(*args, **kwargs)del self.fields['new_password2']+++class PasswordSetUpForm(django_forms.PasswordResetForm):+ """+ PasswordSetUpForm that overrides sending emails to use templated email.+ Allows setting password for new users that have no usable password.+ """++ def get_users(self, email):+ active_users = User.objects.filter(email__iexact=email, is_active=True)+ return active_users++ def send_mail(self, subject_template_name, email_template_name,+ context, from_email, to_email, html_email_template_name=None):+ reset_url = HttpRequest.build_absolute_uri(+ reverse(+ 'account_reset_password_confirm',+ kwargs={'uidb64': context['uid'], 'token': context['token']}))+ context['reset_url'] = reset_url+ send_templated_mail(+ 'account/password_set', from_email=from_email,+ recipient_list=[to_email], context=context)diff --git a/saleor/registration/views.py b/saleor/registration/views.pyindex d86f1063b7..bbc840a7b3 100644--- a/saleor/registration/views.py+++ b/saleor/registration/views.py@@ -8,7 +8,7 @@ from django.utils.translation import ugettext_lazy as _from django.template.response import TemplateResponsefrom saleor.cart.utils import find_and_assign_anonymous_cart-from .forms import LoginForm, SignupForm, SetPasswordForm+from .forms import LoginForm, PasswordSetUpForm, SignupForm, SetPasswordForm@find_and_assign_anonymous_cart()@@ -50,12 +50,20 @@ def password_reset(request):return django_views.PasswordResetView.as_view(**kwargs)(request, **kwargs)+class PasswordResetConfirm(django_views.PasswordResetConfirmView):+ template_name = 'account/password_reset_from_key.html'+ success_url = reverse_lazy('account_reset_password_complete')+ set_password_form = PasswordSetUpForm+ token = None+ uidb64 = None++def password_reset_confirm(request, uidb64=None, token=None):kwargs = {'template_name': 'account/password_reset_from_key.html','success_url': reverse_lazy('account_reset_password_complete'),- 'set_password_form': 'SetPasswordForm',+ 'set_password_form': 'PasswordSetUpForm','token': token,'uidb64': uidb64}- return django_views.PasswordResetConfirmView.as_view(**kwargs)(+ return PasswordResetConfirm.as_view(**kwargs)(request, **kwargs)diff --git a/templates/account/password_reset_from_key.html b/templates/account/password_reset_from_key.htmlindex 730a676e45..d81c1d48b4 100644--- a/templates/account/password_reset_from_key.html+++ b/templates/account/password_reset_from_key.html@@ -18,21 +18,29 @@</p></div>{% else %}- <form method="post" novalidate>- {% csrf_token %}- <h2>{% trans "Password Reset" context "Password reset from key form title" %}</h2>- <p>{% trans "Now you can set new password for you account." context "Password reset from key form title" %}</p>- <fieldset>- {% bootstrap_form form %}- </fieldset>- <div class="row login__btn-group">- <div class="col-sm-12">- <button class="btn primary narrow">- {% trans "Continue" context "Password reset from key form primary action" %}- </button>- <img src="{% static "images/pirate-reset.png" %}" />+ {% if form %}+ <form method="post" novalidate>+ {% csrf_token %}+ <h2>{% trans "Password Reset" context "Password reset from key form title" %}</h2>+ <p>{% trans "Now you can set new password for you account." context "Password reset from key form title" %}</p>+ <fieldset>+ {% bootstrap_form form %}+ </fieldset>+ <div class="row login__btn-group">+ <div class="col-sm-12">+ <button class="btn primary narrow">+ {% trans "Continue" context "Password reset from key form primary action" %}+ </button>+ <img src="{% static "images/pirate-reset.png" %}" />+ </div></div>- </div>- </form>+ </form>+ {% else %}+ <h2>{% trans "Password Reset Link Expired" context "Password reset link expired from key form title" %}</h2>+ <p>{% trans "Sorry but this link is no longer valid. If you forget your password, please use link below." context "Password reset from key form title" %}</p>+ <a href="{% url 'account_reset_password' %}">+ {% trans "Reset my password" context "Reset my password link" %}+ </a>+ {% endif %}{% endif %}{% endblock %}diff --git a/tests/dashboard/test_permissions.py b/tests/dashboard/test_permissions.pyindex 7f4499b2d1..02ae0d57f2 100644--- a/tests/dashboard/test_permissions.py+++ b/tests/dashboard/test_permissions.py@@ -1,7 +1,10 @@from __future__ import unicode_literals+from django.core import mailfrom django.core.urlresolvers import reversefrom django.contrib.auth.models import Group+from ..utils import get_redirect_location+from saleor.userprofile.models import Userfrom saleor.dashboard.group.forms import GroupPermissionsFormfrom saleor.dashboard.staff.forms import StaffForm@@ -718,6 +721,26 @@ def test_staff_form_create_valid(assert staff_user.groups.count() == 1+def test_staff_create_email_with_set_link_password(+ admin_client, staff_client, staff_user, staff_group):+ url = reverse('dashboard:staff-create')+ data = {'email': '[email protected]', 'groups': staff_group.pk,+ 'is_staff': True}+ response = admin_client.post(url, data)+ assert User.objects.count() == 3+ assert len(mail.outbox) == 1++ uidb64 = response.context[0]['uid']+ token = response.context[0]['token']+ url = reverse('account_reset_password_confirm', kwargs={'uidb64': uidb64,+ 'token': token})+ response = staff_client.get(url)+ assert response.status_code == 302+ redirect_location = get_redirect_location(response)+ assert (redirect_location ==+ '/account/password/reset/%s/set-password/' % uidb64)++def test_staff_form_create_not_valid(admin_client, staff_user):url = reverse('dashboard:staff-details', args=[staff_user.pk])data = {'groups': 1}