Project

General

Profile

0001-manager-add-settings-for-password-section-options-on.patch

Paul Marillonnet, 27 Feb 2019 03:57 PM

Download (5.85 KB)

View differences:

Subject: [PATCH] manager: add settings for password section options on user
 add (#25666)

 src/authentic2/a2_rbac/admin.py               |  3 ++-
 ...nizationalunit_user_add_password_policy.py | 20 +++++++++++++++++++
 src/authentic2/a2_rbac/models.py              | 19 ++++++++++++++++++
 src/authentic2/manager/user_views.py          | 16 +++++++++++++++
 tests/test_manager.py                         | 17 ++++++++++++++++
 5 files changed, 74 insertions(+), 1 deletion(-)
 create mode 100644 src/authentic2/a2_rbac/migrations/0018_organizationalunit_user_add_password_policy.py
src/authentic2/a2_rbac/admin.py
43 43

  
44 44
class OrganizationalUnitAdmin(admin.ModelAdmin):
45 45
    fields = ('uuid', 'name', 'slug', 'description', 'username_is_unique',
46
              'email_is_unique', 'default', 'validate_emails')
46
              'email_is_unique', 'default', 'validate_emails',
47
              'user_add_password_policy')
47 48
    readonly_fields = ('uuid',)
48 49
    prepopulated_fields = {"slug": ("name",)}
49 50
    list_display = ('name', 'slug')
src/authentic2/a2_rbac/migrations/0018_organizationalunit_user_add_password_policy.py
1
# -*- coding: utf-8 -*-
2
# Generated by Django 1.11.18 on 2019-02-27 14:28
3
from __future__ import unicode_literals
4

  
5
from django.db import migrations, models
6

  
7

  
8
class Migration(migrations.Migration):
9

  
10
    dependencies = [
11
        ('a2_rbac', '0017_organizationalunit_user_can_reset_password'),
12
    ]
13

  
14
    operations = [
15
        migrations.AddField(
16
            model_name='organizationalunit',
17
            name='user_add_password_policy',
18
            field=models.CharField(choices=[(b'0', 'Send reset link'), (b'1', 'Manual password definition')], default=b'0', max_length=2, verbose_name='User creation password policy'),
19
        ),
20
    ]
src/authentic2/a2_rbac/models.py
23 23
from . import managers, fields
24 24

  
25 25

  
26
USER_ADD_PASSWD_POLICY_CHOICES = (
27
        ('0', _('Send reset link')),
28
        ('1', _('Manual password definition')),
29
)
30

  
31

  
32
# Order: gen passwd, reset passwd at next login, send mail, send passwd reset
33
USER_ADD_PASSWD_POLICY_VALUES = (
34
        (False, False, False, True), # Send reset link
35
        (False, False, True, False), # Manual password definition
36
)
37

  
38

  
26 39
class OrganizationalUnit(OrganizationalUnitAbstractBase):
27 40
    username_is_unique = models.BooleanField(
28 41
        blank=True,
......
47 60
    user_can_reset_password = models.NullBooleanField(
48 61
        verbose_name=_('Users can reset password'))
49 62

  
63
    user_add_password_policy = models.CharField(
64
        verbose_name=_('User creation password policy'),
65
        choices=USER_ADD_PASSWD_POLICY_CHOICES,
66
        default='0',
67
        max_length=2)
68

  
50 69
    objects = managers.OrganizationalUnitManager()
51 70

  
52 71
    class Meta:
src/authentic2/manager/user_views.py
150 150
                         instance=form.instance, form=form)
151 151
        return response
152 152

  
153
    def get_initial(self, *args, **kwargs):
154
        initial = super(UserAddView, self).get_initial(*args, **kwargs)
155
        initial.update(self.get_user_add_policies())
156
        return initial
157

  
158
    def get_user_add_policies(self, *args, **kwargs):
159
        from authentic2.a2_rbac.models import USER_ADD_PASSWD_POLICY_VALUES
160
        options = ('generate_password', 'reset_password_at_next_login',
161
            'send_mail', 'send_password_reset',)
162
        ou_id = self.request.get_full_path().rsplit('/', 3)[1]
163
        ou = get_ou_model().objects.get(id=ou_id)
164
        value = int(ou.user_add_password_policy)
165

  
166
        return dict(zip(options, USER_ADD_PASSWD_POLICY_VALUES[value]))
167

  
168

  
153 169
user_add = UserAddView.as_view()
154 170

  
155 171

  
tests/test_manager.py
836 836
    response = app.get(url, params={'field_id': field_id, 'term': 'Admin édou'})
837 837
    assert len(response.json['results']) == 1
838 838
    assert response.json['results'][0]['text'] == u'La Bédoule - Administrateur'
839

  
840

  
841
def test_user_add_settings(settings, admin, app, db):
842
    from authentic2.a2_rbac.models import (USER_ADD_PASSWD_POLICY_VALUES,
843
            USER_ADD_PASSWD_POLICY_CHOICES)
844
    passwd_options = ('generate_password', 'reset_password_at_next_login',
845
            'send_mail', 'send_password_reset')
846

  
847
    for policy in [choice[0] for choice in USER_ADD_PASSWD_POLICY_CHOICES]:
848
        ou = get_default_ou()
849
        ou.user_add_password_policy = policy
850
        ou.save()
851
        user_add = login(app, admin, '/manage/users/add/').follow()
852
        for option, i in zip(passwd_options, range(4)):
853
            assert user_add.form.get(option).value == {False:None, True:'on'}. \
854
                    get(USER_ADD_PASSWD_POLICY_VALUES[int(policy)][i])
855
        app.get('/logout/').form.submit()
839
-