Project

General

Profile

0002-utils-factorize-sending-of-email-change-verification.patch

Benjamin Dauvergne, 04 December 2017 03:13 PM

Download (5.74 KB)

View differences:

Subject: [PATCH 2/3] utils: factorize sending of email change verification
 email (#19716)

 src/authentic2/manager/forms.py |  2 +-
 src/authentic2/utils.py         | 50 +++++++++++++++++++++++++++++++++++++++++
 src/authentic2/views.py         | 39 ++------------------------------
 3 files changed, 53 insertions(+), 38 deletions(-)
src/authentic2/manager/forms.py
622 622

  
623 623
    class Meta:
624 624
        model = get_ou_model()
625
        fields = ('name', 'default', 'username_is_unique', 'email_is_unique', 'validate_emails')
625
        fields = ('name', 'default', 'username_is_unique', 'email_is_unique')
626 626

  
627 627

  
628 628
def get_role_form_class():
src/authentic2/utils.py
979 979
def get_manager_login_url():
980 980
    from authentic2.manager import app_settings
981 981
    return app_settings.LOGIN_URL or settings.LOGIN_URL
982

  
983

  
984
def send_email_change_mail(user, email, request=None, context=None, template_names=None):
985
    '''Send an email to verify that user can take email as its new email'''
986
    assert user
987
    assert email
988

  
989
    logger = logging.getLogger(__name__)
990

  
991
    if template_names is None:
992
        template_names = ['authentic2/change_email_notification']
993
        legacy_subject_templates = ['profiles/email_change_subject.txt']
994
        legacy_body_templates = ['profiles/email_change_body.txt']
995
    else:
996
        legacy_subject_templates = None
997
        legacy_body_templates = None
998

  
999
    # build verify email URL containing a signed token
1000
    token = signing.dumps({
1001
        'email': email,
1002
        'user_pk': user.pk,
1003
    })
1004
    link = '{0}?token={1}'.format(reverse('email-change-verify'), token)
1005
    link = request.build_absolute_uri(link)
1006

  
1007
    # check if email should be unique and is not
1008
    email_is_not_unique = False
1009
    qs = get_user_model().objects.all()
1010
    if app_settings.A2_EMAIL_IS_UNIQUE:
1011
        email_is_not_unique = qs.filter(email=email).exclude(pk=user.pk).exists()
1012
    elif user.ou and user.ou.email_is_unique:
1013
        email_is_not_unique = qs.filter(email=email, ou=user.ou).exclude(pk=user.pk).exists()
1014
    ctx = context or {}
1015
    ctx.update({
1016
        'email': email,
1017
        'old_email': user.email,
1018
        'user': user,
1019
        'link': link,
1020
        'domain': request.get_host(),
1021
        'token_lifetime': human_duration(app_settings.A2_EMAIL_CHANGE_TOKEN_LIFETIME),
1022
        'password_reset_url': request.build_absolute_uri(reverse('password_reset')),
1023
        'email_is_not_unique': email_is_not_unique,
1024
    })
1025
    logger.info(u'sent email verify email to %s for %s', email, user)
1026
    send_templated_mail(
1027
        email,
1028
        template_names,
1029
        context=ctx,
1030
        legacy_subject_templates=legacy_subject_templates,
1031
        legacy_body_templates=legacy_body_templates)
src/authentic2/views.py
152 152

  
153 153
    def get_form_kwargs(self):
154 154
        kwargs = super(EmailChangeView, self).get_form_kwargs()
155
        kwargs.update({
156
            'user': self.request.user,
157
        })
155
        kwargs['user'] = self.request.user
158 156
        return kwargs
159 157

  
160 158
    def post(self, request, *args, **kwargs):
......
162 160
            return utils.redirect(request, 'account_management')
163 161
        return super(EmailChangeView, self).post(request, *args, **kwargs)
164 162

  
165
    @classmethod
166
    def send_email_change_email(cls, request, user, email):
167
        token = signing.dumps({
168
            'email': email,
169
            'user_pk': user.pk,
170
        })
171
        link = '{0}?token={1}'.format(
172
            reverse('email-change-verify'),
173
            token)
174
        link = request.build_absolute_uri(link)
175
        ctx = {
176
            'email': email,
177
            'old_email': user.email,
178
            'user': user,
179
            'link': link,
180
            'domain': request.get_host(),
181
            'token_lifetime': utils.human_duration(app_settings.A2_EMAIL_CHANGE_TOKEN_LIFETIME),
182
            'password_reset_url': request.build_absolute_uri(reverse('password_reset')),
183
        }
184
        qs = compat.get_user_model().objects.all()
185
        if app_settings.A2_EMAIL_IS_UNIQUE:
186
            ctx['email_is_not_unique'] = qs.filter(email=email).exclude(pk=user.pk).exists()
187
        elif user.ou and user.ou.email_is_unique:
188
            ctx['email_is_not_unique'] = qs.filter(email=email,
189
                                                   ou=user.ou).exclude(pk=user.pk).exists()
190

  
191
        utils.send_templated_mail(
192
            email,
193
            ['authentic2/change_email_notification'],
194
            context=ctx,
195
            legacy_subject_templates=['profiles/email_change_subject.txt'],
196
            legacy_body_templates=['profiles/email_change_body.txt'])
197

  
198 163
    def form_valid(self, form):
199 164
        email = form.cleaned_data['email']
200
        self.send_email_change_email(self.request, self.request.user, email)
165
        utils.send_email_change_mail(self.request.user, email, request=self.request)
201 166
        hooks.call_hooks('event', name='change-email', user=self.request.user, email=email)
202 167
        messages.info(
203 168
            self.request,
204
-