From e6b744c1bc2ca655433a0c1f407575e6627ecf3d Mon Sep 17 00:00:00 2001 From: Paul Marillonnet Date: Wed, 31 Oct 2018 12:00:30 +0100 Subject: [PATCH] backoffice: support next url after user creation (#26652) --- .../templates/authentic2/manager/form.html | 2 + .../authentic2/manager/user_add.html | 5 +++ src/authentic2/manager/user_views.py | 5 ++- tests/test_manager.py | 41 +++++++++++++++++++ 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/authentic2/manager/templates/authentic2/manager/form.html b/src/authentic2/manager/templates/authentic2/manager/form.html index 7b8b40a7..bf2f15e3 100644 --- a/src/authentic2/manager/templates/authentic2/manager/form.html +++ b/src/authentic2/manager/templates/authentic2/manager/form.html @@ -91,5 +91,7 @@ } }) + {% block hidden_inputs %} + {% endblock %} {% endblock %} diff --git a/src/authentic2/manager/templates/authentic2/manager/user_add.html b/src/authentic2/manager/templates/authentic2/manager/user_add.html index ebc1cdd6..2192505d 100644 --- a/src/authentic2/manager/templates/authentic2/manager/user_add.html +++ b/src/authentic2/manager/templates/authentic2/manager/user_add.html @@ -5,6 +5,11 @@ {% trans "Add an user" %} {% endblock %} + {% block hidden_inputs %} + {{ block.super }} + {% if next_url %}{% endif %} + {% endblock %} + {% block breadcrumb %} {{ block.super }} {% trans 'Users' %}{% if multiple_ou and ou %} : {{ ou }}{% endif %} diff --git a/src/authentic2/manager/user_views.py b/src/authentic2/manager/user_views.py index 9e07edd9..de834921 100644 --- a/src/authentic2/manager/user_views.py +++ b/src/authentic2/manager/user_views.py @@ -129,12 +129,15 @@ class UserAddView(BaseAddView): return fields def get_success_url(self): - return reverse('a2-manager-user-detail', kwargs={'pk': self.object.pk}) + return self.request.POST.get('next_url') or \ + reverse('a2-manager-user-detail', kwargs={'pk': self.object.pk}) def get_context_data(self, **kwargs): context = super(UserAddView, self).get_context_data(**kwargs) context['cancel_url'] = '../..' context['ou'] = self.ou + if hasattr(self.request, 'GET') and 'next_url' in self.request.GET: + context['next_url'] = self.request.GET['next_url'] return context def form_valid(self, form): diff --git a/tests/test_manager.py b/tests/test_manager.py index f9ef9471..82072c08 100644 --- a/tests/test_manager.py +++ b/tests/test_manager.py @@ -687,3 +687,44 @@ def test_return_on_logout(superuser, app): manager_home_page = login(app, superuser, reverse('a2-manager-homepage')) response = manager_home_page.click('Logout').maybe_follow() assert response.request.query_string == 'next=/manage/' + + +def test_manager_create_user_next_url(superuser_or_admin, app, ou1): + next_url = u'https://example.nowhere.null/' + url = u'/manage/users/%s/add/?next_url=%s' % (ou1.pk, next_url) + login(app, superuser_or_admin, '/manage/') + response = app.get(url) + form = response.form + form.set('first_name', 'John') + form.set('last_name', 'Doe') + form.set('email', 'john.doe@gmail.com') + form.set('password1', 'ABcd1234') + form.set('password2', 'ABcd1234') + assert form.submit().location == next_url + + +def test_manager_create_user_next_url_form_cancelation(superuser_or_admin, app, ou1): + next_url = u'https://example.nowhere.null/' + url = u'/manage/users/%s/add/?next_url=%s' % (ou1.pk, next_url) + login(app, superuser_or_admin, '/manage/') + response = app.get(url) + form = response.form + form.set('first_name', 'John') + form.set('last_name', 'Doe') + form.set('email', 'john.doe@gmail.com') + form.set('password1', 'ABcd1234') + form.set('password2', 'ABcd1234') + assert form.submit('cancel').location == next_url + + +def test_manager_create_user_next_url_form_error(superuser_or_admin, app, ou1): + next_url = u'https://example.nowhere.null/' + url = u'/manage/users/%s/add/?next_url=%s' % (ou1.pk, next_url) + login(app, superuser_or_admin, '/manage/') + response = app.get(url) + form = response.form + form.set('first_name', 'John') + form.set('last_name', 'Doe') + form.set('email', 'jd') # erroneous + form.set('password1', 'notvalid') # erroneous + assert '' % next_url in form.submit().body -- 2.19.1