Skip to content

Commit

Permalink
#354 Create AdminLoginAsMixin
Browse files Browse the repository at this point in the history
  • Loading branch information
viliambalaz committed Nov 28, 2021
1 parent 85270ff commit 19e8254
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
13 changes: 10 additions & 3 deletions chcemvediet/apps/accounts/backends.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
22 changes: 12 additions & 10 deletions chcemvediet/apps/accounts/tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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'))
Expand All @@ -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/')
Expand Down
4 changes: 2 additions & 2 deletions chcemvediet/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down

0 comments on commit 19e8254

Please sign in to comment.