Project

General

Profile

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

Paul Marillonnet, 27 Feb 2019 05:12 PM

Download (5.99 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 | 19 +++++++++++++++
 src/authentic2/a2_rbac/models.py              | 24 +++++++++++++++++++
 src/authentic2/manager/user_views.py          | 15 ++++++++++++
 tests/test_manager.py                         | 14 +++++++++++
 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
from __future__ import unicode_literals
3

  
4
from django.db import models, migrations
5

  
6

  
7
class Migration(migrations.Migration):
8

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

  
13
    operations = [
14
        migrations.AddField(
15
            model_name='organizationalunit',
16
            name='user_add_password_policy',
17
            field=models.IntegerField(default=0, verbose_name='User creation password policy', choices=[(0, 'Send reset link'), (1, 'Manual password definition')]),
18
        ),
19
    ]
src/authentic2/a2_rbac/models.py
1
from collections import namedtuple
1 2
from django.core.exceptions import ValidationError
2 3
from django.utils.translation import ugettext_lazy as _
3 4
from django.utils.text import slugify
......
24 25

  
25 26

  
26 27
class OrganizationalUnit(OrganizationalUnitAbstractBase):
28

  
29
    RESET_LINK_POLICY = 0
30
    MANUAL_PASSWORD_POLICY = 1
31

  
32
    USER_ADD_PASSWD_POLICY_CHOICES = (
33
            (RESET_LINK_POLICY, _('Send reset link')),
34
            (MANUAL_PASSWORD_POLICY, _('Manual password definition')),
35
    )
36

  
37
    PolicyValue = namedtuple('PolicyValue', [
38
            'generate_password', 'reset_password_at_next_login',
39
            'send_mal', 'send_password_reset'])
40

  
41
    USER_ADD_PASSWD_POLICY_VALUES = {
42
            RESET_LINK_POLICY: PolicyValue(False, False, False, True),
43
            MANUAL_PASSWORD_POLICY: PolicyValue(False, False, True, False),
44
    }
45

  
27 46
    username_is_unique = models.BooleanField(
28 47
        blank=True,
29 48
        default=False,
......
47 66
    user_can_reset_password = models.NullBooleanField(
48 67
        verbose_name=_('Users can reset password'))
49 68

  
69
    user_add_password_policy = models.IntegerField(
70
        verbose_name=_('User creation password policy'),
71
        choices=USER_ADD_PASSWD_POLICY_CHOICES,
72
        default=0)
73

  
50 74
    objects = managers.OrganizationalUnitManager()
51 75

  
52 76
    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
        options = ('generate_password', 'reset_password_at_next_login',
160
            'send_mail', 'send_password_reset',)
161
        ou_id = self.request.get_full_path().rsplit('/', 3)[1]
162
        ou = get_ou_model().objects.get(id=ou_id)
163
        value = ou.user_add_password_policy
164

  
165
        return dict(zip(options, get_ou_model().USER_ADD_PASSWD_POLICY_VALUES[value]))
166

  
167

  
153 168
user_add = UserAddView.as_view()
154 169

  
155 170

  
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
    passwd_options = ('generate_password', 'reset_password_at_next_login',
843
            'send_mail', 'send_password_reset')
844
    for policy in [choice[0] for choice in get_ou_model().USER_ADD_PASSWD_POLICY_CHOICES]:
845
        ou = get_default_ou()
846
        ou.user_add_password_policy = policy
847
        ou.save()
848
        user_add = login(app, admin, '/manage/users/add/').follow()
849
        for option, i in zip(passwd_options, range(4)):
850
            assert user_add.form.get(option).value == {False:None, True:'on'}. \
851
                    get(get_ou_model().USER_ADD_PASSWD_POLICY_VALUES[policy][i])
852
        app.get('/logout/').form.submit()
839
-