Projet

Général

Profil

0001-allow-custom-authentication-method-in-registration-v.patch

Benjamin Dauvergne, 09 août 2018 17:30

Télécharger (4,74 ko)

Voir les différences:

Subject: [PATCH] allow custom authentication method in registration view
 (fixes #25623)

 src/authentic2/registration_backend/views.py | 11 ++++--
 tests/test_registration.py                   | 35 ++++++++++++++++++++
 2 files changed, 43 insertions(+), 3 deletions(-)
src/authentic2/registration_backend/views.py
135 135

  
136 136
    def dispatch(self, request, *args, **kwargs):
137 137
        self.token = request.token
138
        self.authentication_method = self.token.get('authentication_method', 'email')
138 139
        self.email = request.token['email']
139 140
        if 'ou' in self.token:
140 141
            self.ou = OrganizationalUnit.objects.get(pk=self.token['ou'])
......
265 266
    def get(self, request, *args, **kwargs):
266 267
        if len(self.users) == 1 and self.email_is_unique:
267 268
            # Found one user, EMAIL is unique, log her in
268
            simulate_authentication(request, self.users[0], method='email', service_slug=self.service)
269
            simulate_authentication(request, self.users[0],
270
                                    method=self.authentication_method,
271
                                    service_slug=self.service)
269 272
            return redirect(request, self.get_success_url())
270 273
        confirm_data = self.token.get('confirm_data', False)
271 274

  
......
300 303
            uid = request.POST['uid']
301 304
            for user in self.users:
302 305
                if str(user.id) == uid:
303
                    simulate_authentication(request, user, method='email',
306
                    simulate_authentication(request, user,
307
                                            method=self.authentication_method,
304 308
                                            service_slug=self.service)
305 309
                    return redirect(request, self.get_success_url())
306 310
        return super(RegistrationCompletionView, self).post(request, *args, **kwargs)
......
332 336

  
333 337
    def registration_success(self, request, user, form):
334 338
        hooks.call_hooks('event', name='registration', user=user, form=form, view=self,
339
                         authentication_method=self.authentication_method,
335 340
                         token=request.token, service=self.service)
336
        simulate_authentication(request, user, method='email',
341
        simulate_authentication(request, user, method=self.authentication_method,
337 342
                                service_slug=self.service)
338 343
        messages.info(self.request, _('You have just created an account.'))
339 344
        self.send_registration_success_email(user)
tests/test_registration.py
602 602
    response.form.set('password2', 'AAAazerty12AZ')
603 603
    response = response.form.submit()
604 604
    assert "The two password fields didn't match." in response.content
605

  
606

  
607
def test_authentication_method(app, db, rf, hooks):
608
    activation_url = utils.build_activation_url(
609
        rf.post('/accounts/register/'),
610
        email='john.doe@example.com',
611
        next_url='/',
612
        first_name='John',
613
        last_name='Doe',
614
        no_password=True,
615
        confirm_data=False)
616
    app.get(activation_url)
617

  
618
    assert len(hooks.calls['event']) == 2
619
    assert hooks.calls['event'][-2]['kwargs']['name'] == 'registration'
620
    assert hooks.calls['event'][-2]['kwargs']['authentication_method'] == 'email'
621
    assert hooks.calls['event'][-1]['kwargs']['name'] == 'login'
622
    assert hooks.calls['event'][-1]['kwargs']['how'] == 'email'
623

  
624
    activation_url = utils.build_activation_url(
625
        rf.post('/accounts/register/'),
626
        email='jane.doe@example.com',
627
        next_url='/',
628
        first_name='Jane',
629
        last_name='Doe',
630
        no_password=True,
631
        authentication_method='another',
632
        confirm_data=False)
633
    app.get(activation_url)
634

  
635
    assert len(hooks.calls['event']) == 4
636
    assert hooks.calls['event'][-2]['kwargs']['name'] == 'registration'
637
    assert hooks.calls['event'][-2]['kwargs']['authentication_method'] == 'another'
638
    assert hooks.calls['event'][-1]['kwargs']['name'] == 'login'
639
    assert hooks.calls['event'][-1]['kwargs']['how'] == 'another'
605
-