From 86df505467fc1fb0ac880d87fce88f3e14e1e318 Mon Sep 17 00:00:00 2001
From: Paul Marillonnet <pmarillonnet@entrouvert.com>
Date: Tue, 26 Feb 2019 15:56:06 +0100
Subject: [PATCH] manager: add settings for password section options on user
 add (#25666)

---
 src/authentic2/app_settings.py       |  4 ++++
 src/authentic2/manager/user_views.py |  7 +++++-
 src/authentic2/manager/utils.py      | 13 +++++++++++
 tests/test_manager.py                | 32 ++++++++++++++++++++++++++++
 4 files changed, 55 insertions(+), 1 deletion(-)

diff --git a/src/authentic2/app_settings.py b/src/authentic2/app_settings.py
index 67ecb553..a51f7045 100644
--- a/src/authentic2/app_settings.py
+++ b/src/authentic2/app_settings.py
@@ -207,6 +207,10 @@ default_settings = dict(
     A2_ACCOUNTS_URL=Setting(default=None, definition='IdP has no account page, redirect to this one.'),
     A2_CACHE_ENABLED=Setting(default=True, definition='Disable all cache decorators for testing purpose.'),
     A2_ACCEPT_EMAIL_AUTHENTICATION=Setting(default=True, definition='Enable authentication by email'),
+    A2_USER_ADD_GENERATE_PASSWORD=Setting(default=True, definition='Generate a password for newly created users.'),
+    A2_USER_ADD_RESET_PASSWORD_AT_NEXT_LOGIN=Setting(default=False, definition='Choose whether the user needs to change their password on next login.'),
+    A2_USER_ADD_SEND_MAIL=Setting(default=False, definition='Send a mail for newly created users that contain account information.'),
+    A2_USER_ADD_SEND_PASSWORD_RESET=Setting(default=False, definition='Send a mail for newly created users to define their password.'),
 
 )
 
diff --git a/src/authentic2/manager/user_views.py b/src/authentic2/manager/user_views.py
index c94b46d0..87ae0fc1 100644
--- a/src/authentic2/manager/user_views.py
+++ b/src/authentic2/manager/user_views.py
@@ -35,7 +35,7 @@ from .tables import UserTable, UserRolesTable, OuUserRolesTable
 from .forms import (UserSearchForm, UserAddForm, UserEditForm,
     UserChangePasswordForm, ChooseUserRoleForm, UserRoleSearchForm, UserChangeEmailForm)
 from .resources import UserResource
-from .utils import get_ou_count
+from .utils import get_ou_count, get_user_add_policies
 from . import app_settings
 
 
@@ -150,6 +150,11 @@ class UserAddView(BaseAddView):
                          instance=form.instance, form=form)
         return response
 
+    def get_initial(self, *args, **kwargs):
+        initial = super(UserAddView, self).get_initial(*args, **kwargs)
+        initial.update(get_user_add_policies())
+        return initial
+
 user_add = UserAddView.as_view()
 
 
diff --git a/src/authentic2/manager/utils.py b/src/authentic2/manager/utils.py
index 619b5e7c..ed0579b0 100644
--- a/src/authentic2/manager/utils.py
+++ b/src/authentic2/manager/utils.py
@@ -3,6 +3,7 @@
 from django_rbac.utils import get_ou_model
 
 from authentic2.decorators import GlobalCache
+from authentic2 import app_settings
 
 
 def label_from_user(user):
@@ -26,3 +27,15 @@ def label_from_user(user):
 @GlobalCache(timeout=10)
 def get_ou_count():
     return get_ou_model().objects.count()
+
+
+def get_user_add_policies():
+    options = (
+        'generate_password',
+        'reset_password_at_next_login',
+        'send_mail',
+        'send_password_reset',
+    )
+
+    return {key: getattr(app_settings, 'A2_USER_ADD_{}'.format(key.upper()))
+            for key in options}
diff --git a/tests/test_manager.py b/tests/test_manager.py
index 9bcdc8ea..bfbbe090 100644
--- a/tests/test_manager.py
+++ b/tests/test_manager.py
@@ -836,3 +836,35 @@ def test_roles_widget(admin, app, db):
     response = app.get(url, params={'field_id': field_id, 'term': 'Admin édou'})
     assert len(response.json['results']) == 1
     assert response.json['results'][0]['text'] == u'La Bédoule - Administrateur'
+
+
+def test_user_add_settings(settings, admin, app, db):
+    possible_configs = (
+        ((False, False, False, False), (None, None, None, None)),
+        ((False, True, False, False), (None, 'on', None, None)),
+        ((False, False, True, False), (None, None, 'on', None)),
+        ((True, False, True, False), ('on', None, 'on', None)),
+        ((False, True, True, False), (None, 'on', 'on', None)),
+        ((True, True, True, False), ('on', 'on', 'on', None)),
+        ((False, False, False, True), (None, None, None, 'on')),
+        ((False, True, False, True), (None, 'on', None, 'on')),
+        ((False, False, True, True), (None, None, 'on', 'on')),
+        ((True, False, True, True), ('on', None, 'on', 'on')),
+        ((False, True, True, True), (None, 'on', 'on', 'on')),
+        ((True, True, True, True), ('on', 'on', 'on', 'on'))
+    )
+
+    for config in possible_configs:
+        settings.A2_USER_ADD_GENERATE_PASSWORD = config[0][0]
+        settings.A2_USER_ADD_RESET_PASSWORD_AT_NEXT_LOGIN = config[0][1]
+        settings.A2_USER_ADD_SEND_MAIL = config[0][2]
+        settings.A2_USER_ADD_SEND_PASSWORD_RESET = config[0][3]
+
+        user_add = login(app, admin, '/manage/users/add/').follow()
+
+        assert user_add.form.get('generate_password').value == config[1][0]
+        assert user_add.form.get('reset_password_at_next_login').value == config[1][1]
+        assert user_add.form.get('send_mail').value == config[1][2]
+        assert user_add.form.get('send_password_reset').value == config[1][3]
+
+        app.get('/logout/').form.submit()
-- 
2.20.1

