From 19e8254744eabdf4ab5f6f8dd63e0f8e1c6a0740 Mon Sep 17 00:00:00 2001 From: viliambalaz Date: Fri, 19 Nov 2021 20:00:40 +0100 Subject: [PATCH] #354 Create AdminLoginAsMixin --- chcemvediet/apps/accounts/backends.py | 13 ++++++++--- .../apps/accounts/tests/test_backend.py | 22 ++++++++++--------- chcemvediet/settings/common.py | 4 ++-- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/chcemvediet/apps/accounts/backends.py b/chcemvediet/apps/accounts/backends.py index 6c3ea342..7a5150be 100644 --- a/chcemvediet/apps/accounts/backends.py +++ b/chcemvediet/apps/accounts/backends.py @@ -1,17 +1,18 @@ +from allauth.account.auth_backends import AuthenticationBackend from django.contrib.auth.backends import ModelBackend from django.core.urlresolvers import resolve, Resolver404 from poleno.utils.http import get_request -class AdminLoginAsBackend(ModelBackend): +class AdminLoginAsBackendMixin(ModelBackend): def is_admin_path(self, path): return resolve(path).namespace == u'admin' def get_user(self, user_id): request = get_request() - user = super(AdminLoginAsBackend, self).get_user(user_id) + user = super(AdminLoginAsBackendMixin, self).get_user(user_id) if request is None: return user try: @@ -20,5 +21,11 @@ def get_user(self, user_id): return user admin_login_as = request.session.get(u'admin_login_as') if user and user.is_staff and not self.is_admin_path(request.path) and admin_login_as: - return super(AdminLoginAsBackend, self).get_user(admin_login_as) or user + return super(AdminLoginAsBackendMixin, self).get_user(admin_login_as) or user return user + +class DjangoModelBackendWithAdminLoginAs(AdminLoginAsBackendMixin, ModelBackend): + pass + +class AllauthAuthenticationBackendWithAdminLoginAs(AdminLoginAsBackendMixin, AuthenticationBackend): + pass diff --git a/chcemvediet/apps/accounts/tests/test_backend.py b/chcemvediet/apps/accounts/tests/test_backend.py index 46344349..09433cf3 100644 --- a/chcemvediet/apps/accounts/tests/test_backend.py +++ b/chcemvediet/apps/accounts/tests/test_backend.py @@ -6,22 +6,22 @@ from django.test.utils import override_settings -class AdminLoginAsBackendTest(TestCase): +class AdminLoginAsBackendMixinTest(TestCase): def public_view(request): if isinstance(request.user, User): pass # force request.user to evaluate return HttpResponse() - @user_passes_test(lambda u: u.is_staff) + @user_passes_test(lambda u: u.is_staff, login_url=u'/login/', redirect_field_name=u'next') def admin_view(request): if isinstance(request.user, User): pass return HttpResponse() - @user_passes_test(lambda u: u.is_staff) - def set_admin_login_as_attribute_admin_view(request, id): - request.session[u'admin_login_as'] = id + @user_passes_test(lambda u: u.is_staff, login_url=u'/login/', redirect_field_name=u'next') + def set_admin_login_as_attribute_admin_view(request, obj_pk): + request.session[u'admin_login_as'] = obj_pk if isinstance(request.user, User): pass return HttpResponse() @@ -48,7 +48,7 @@ def create_users(self): def setUp(self): self.settings_override = override_settings( - AUTHENTICATION_BACKENDS=(u'chcemvediet.apps.accounts.backends.AdminLoginAsBackend',), + AUTHENTICATION_BACKENDS=(u'chcemvediet.apps.accounts.backends.AdminLoginAsBackendMixin',), PASSWORD_HASHERS=(u'django.contrib.auth.hashers.MD5PasswordHasher',), ) self.settings_override.enable() @@ -67,6 +67,7 @@ def test_admin_route_uses_anonymous_user_and_fails_if_user_is_not_logged_in(self response = self.client.get(u'/admin/') self.assertTrue(response.wsgi_request.user.is_anonymous()) self.assertEqual(response.status_code, 302) + self.assertRedirects(response, u'/login/?next=/admin/', fetch_redirect_response=False) def test_public_route_uses_the_user_if_user_is_logged_in(self): self.assertTrue(self.client.login(username=self.user.username, password=u'test')) @@ -78,28 +79,29 @@ def test_admin_route_uses_the_user_and_fails_if_user_is_logged_in(self): self.assertTrue(self.client.login(username=self.user.username, password=u'test')) response = self.client.get(u'/admin/') self.assertEqual(response.status_code, 302) + self.assertRedirects(response, u'/login/?next=/admin/', fetch_redirect_response=False) self.assertEqual(response.wsgi_request.user, self.user) - def test_public_route_uses_the_admin_if_admin_logged_in(self): + def test_public_route_uses_the_admin_if_admin_is_logged_in(self): self.assertTrue(self.client.login(username=self.superuser.username, password=u'test')) response = self.client.get(u'/') self.assertEqual(response.status_code, 200) self.assertTrue(response.wsgi_request.user, self.superuser) - def test_admin_route_uses_the_admin_if_admin_logged_in(self): + def test_admin_route_uses_the_admin_if_admin_is_logged_in(self): self.assertTrue(self.client.login(username=self.superuser.username, password=u'test')) response = self.client.get(u'/admin/') self.assertEqual(response.status_code, 200) self.assertTrue(response.wsgi_request.user, self.superuser) - def test_public_route_uses_the_user_if_admin_logged_in_as_another_user(self): + def test_public_route_uses_the_user_if_admin_is_logged_in_as_another_user(self): self.assertTrue(self.client.login(username=self.superuser.username, password=u'test')) self.client.get(u'/admin/{}/login-as/'.format(self.user.pk)) response = self.client.get(u'/') self.assertEqual(response.status_code, 200) self.assertTrue(response.wsgi_request.user, self.user) - def test_admin_route_uses_the_admin_if_admin_logged_in_as_another_user(self): + def test_admin_route_uses_the_admin_if_admin_is_logged_in_as_another_user(self): self.assertTrue(self.client.login(username=self.superuser.username, password=u'test')) self.client.get(u'/admin/{}/login-as/'.format(self.user.pk)) response = self.client.get(u'/admin/') diff --git a/chcemvediet/settings/common.py b/chcemvediet/settings/common.py index 4c0ea24d..ecec520f 100644 --- a/chcemvediet/settings/common.py +++ b/chcemvediet/settings/common.py @@ -84,8 +84,8 @@ ) AUTHENTICATION_BACKENDS = ( - u'chcemvediet.apps.accounts.backends.AdminLoginAsBackend', - u'allauth.account.auth_backends.AuthenticationBackend', + u'chcemvediet.apps.accounts.backends.DjangoModelBackendWithAdminLoginAs', + u'chcemvediet.apps.accounts.backends.AllauthAuthenticationBackendWithAdminLoginAs', ) TEMPLATE_LOADERS = (