Projet

Général

Profil

0001-manager-send-new-email-in-the-email-change-verificat.patch

Benjamin Dauvergne, 19 janvier 2018 10:18

Télécharger (6,09 ko)

Voir les différences:

Subject: [PATCH] manager: send new email in the email change verification mail
 (fixes #20564)

Use of a ModelForm keeping the original email field for the
UserChangeEmailForm makes keeping the original email value after clean()
is called impossible, as clean() is also responsible of transfering
value from the form into the model instance.

We keep using a ModelForm but we use a new field not present in the
model to get the new email and we override the save() method so that the
behaviour of sending the validation mail is kept inside the form and not
in the view. Only the call to the manager's hook
manager-change-email-request is kept in the view.
 src/authentic2/manager/forms.py      | 23 ++++++++++++++++++++---
 src/authentic2/manager/user_views.py | 20 +++++++++-----------
 tests/test_user_manager.py           | 12 ++++++++++--
 3 files changed, 39 insertions(+), 16 deletions(-)
src/authentic2/manager/forms.py
21 21
from authentic2.models import PasswordReset
22 22
from authentic2.utils import import_module_or_class
23 23
from authentic2.a2_rbac.utils import get_default_ou
24
from authentic2.utils import send_password_reset_mail
24
from authentic2.utils import send_password_reset_mail, send_email_change_email
25 25
from authentic2 import app_settings as a2_app_settings
26 26

  
27 27
from . import fields, app_settings, utils
......
651 651
    return RoleEditForm
652 652

  
653 653

  
654
class UserChangeEmailForm(CssClass, forms.ModelForm):
654
# we need a model form so that we can use a BaseEditView, a simple Form
655
# would not work
656
class UserChangeEmailForm(CssClass, FormWithRequest, forms.ModelForm):
657
    new_email = forms.EmailField(label=_('Email'))
658

  
659
    def __init__(self, *args, **kwargs):
660
        initial = kwargs.setdefault('initial', {})
661
        instance = kwargs.get('instance')
662
        if instance:
663
            initial['new_email'] = instance.email
664
        super(UserChangeEmailForm, self).__init__(*args, **kwargs)
665

  
655 666
    def save(self, *args, **kwargs):
667
        new_email = self.cleaned_data['new_email']
668
        send_email_change_email(
669
            self.instance,
670
            new_email,
671
            request=self.request,
672
            template_names=['authentic2/manager/user_change_email_notification'])
656 673
        return self.instance
657 674

  
658 675
    class Meta:
659
        fields = ('email',)
676
        fields = ()
src/authentic2/manager/user_views.py
353 353
    title = _('Change user email')
354 354

  
355 355
    def get_success_message(self, cleaned_data):
356
        return ugettext('A mail was sent to %s to verify it.') % cleaned_data['email']
357

  
358
    def get_form_kwargs(self):
359
        kwargs = super(UserChangeEmailView, self).get_form_kwargs()
360
        kwargs.setdefault('initial', {})['email'] = self.object.email
361
        return kwargs
356
        return ugettext('A mail was sent to %s to verify it.') % cleaned_data['new_email']
362 357

  
363 358
    def form_valid(self, form):
364 359
        response = super(UserChangeEmailView, self).form_valid(form)
365
        email = form.cleaned_data['email']
366
        hooks.call_hooks('event', name='manager-change-email-request', user=self.request.user,
367
                         instance=form.instance, form=form, email=email)
368
        send_email_change_email(self.object, email, request=self.request,
369
                               template_names=['authentic2/manager/user_change_email_notification'])
360
        new_email = form.cleaned_data['new_email']
361
        hooks.call_hooks(
362
                'event',
363
                name='manager-change-email-request',
364
                user=self.request.user,
365
                instance=form.instance,
366
                form=form,
367
                email=new_email)
370 368
        return response
371 369

  
372 370
user_change_email = UserChangeEmailView.as_view()
tests/test_user_manager.py
14 14
    ou.validate_emails = True
15 15
    ou.save()
16 16

  
17
    NEW_EMAIL = 'john.doe@example.com'
18

  
19
    assert NEW_EMAIL != simple_user.email
20

  
17 21
    response = login(app, superuser_or_admin,
18 22
                     reverse('a2-manager-user-by-uuid-detail',
19 23
                             kwargs={'slug': unicode(simple_user.uuid)}))
......
21 25
    # cannot click it's a submit button :/
22 26
    response = app.get(reverse('a2-manager-user-by-uuid-change-email',
23 27
                               kwargs={'slug': unicode(simple_user.uuid)}))
24
    response.form.set('email', 'john.doe@example.com')
28
    assert response.form['new_email'].value == simple_user.email
29
    response.form.set('new_email', NEW_EMAIL)
25 30
    assert len(mailoutbox) == 0
26 31
    response = response.form.submit().follow()
27 32
    assert 'A mail was sent to john.doe@example.com to verify it.' in response.content
28 33
    assert 'Change user email' in response.content
29 34
    # cannot click it's a submit button :/
30 35
    assert len(mailoutbox) == 1
36
    assert simple_user.email in mailoutbox[0].body
37
    assert NEW_EMAIL in mailoutbox[0].body
38

  
31 39
    # logout
32 40
    app.session.flush()
33 41

  
......
37 45
        'your request for changing your email for john.doe@example.com is successful'
38 46
        in response.content)
39 47
    simple_user.refresh_from_db()
40
    assert simple_user.email == 'john.doe@example.com'
48
    assert simple_user.email == NEW_EMAIL
41 49

  
42 50

  
43 51
def test_search_by_attribute(app, simple_user, admin):
44
-