Skip to content

Commit

Permalink
#354 Move AdminLoginAsAdminMixin to poleno directory
Browse files Browse the repository at this point in the history
  • Loading branch information
viliambalaz committed Nov 28, 2021
1 parent 7c2cc60 commit 9cf1c2f
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 50 deletions.
19 changes: 3 additions & 16 deletions chcemvediet/apps/accounts/admin.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# vim: expandtab
# -*- coding: utf-8 -*-
from django import forms
from django.conf.urls import patterns, url
from django.contrib import admin
from django.core.exceptions import ValidationError
from django.http import HttpResponseRedirect

from poleno.utils.admin_login_as import AdminLoginAsAdminMixin
from poleno.utils.admin import simple_list_filter_factory, admin_obj_format
from poleno.utils.misc import decorate
from poleno.utils.urls import reverse

from .models import Profile

Expand All @@ -27,7 +25,7 @@ def clean_custom_anonymized_strings(self):
return custom_anonymized_strings

@admin.register(Profile, site=admin.site)
class ProfileAdmin(admin.ModelAdmin):
class ProfileAdmin(AdminLoginAsAdminMixin, admin.ModelAdmin):
form = ProfileAdminForm
date_hierarchy = None
list_display = [
Expand Down Expand Up @@ -80,21 +78,10 @@ class ProfileAdmin(admin.ModelAdmin):
]
inlines = [
]
LOGIN_AS_REDIRECT_VIEWNAME = u'inforequests:mine'

def get_queryset(self, request):
queryset = super(ProfileAdmin, self).get_queryset(request)
queryset = queryset.select_related(u'user')
queryset = queryset.select_undecided_emails_count()
return queryset

def login_as_view(self, request, obj_pk):
request.session[u'admin_login_as'] = obj_pk
return HttpResponseRedirect(reverse(u'inforequests:mine'))

def get_urls(self):
info = self.model._meta.app_label, self.model._meta.model_name
login_as_view = self.admin_site.admin_view(self.login_as_view)
urls = patterns('',
url(r'^(\d+)/login-as/$', login_as_view, name=u'{}_{}_login_as'.format(*info)),
)
return urls + super(ProfileAdmin, self).get_urls()
11 changes: 11 additions & 0 deletions chcemvediet/auth_backends.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from allauth.account.auth_backends import AuthenticationBackend
from django.contrib.auth.backends import ModelBackend

from poleno.utils.admin_login_as import AdminLoginAsBackendMixin


class DjangoModelBackendWithAdminLoginAs(AdminLoginAsBackendMixin, ModelBackend):
pass

class AllauthAuthenticationBackendWithAdminLoginAs(AdminLoginAsBackendMixin, AuthenticationBackend):
pass
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'poleno.utils.backends.DjangoModelBackendWithAdminLoginAs',
u'poleno.utils.backends.AllauthAuthenticationBackendWithAdminLoginAs',
u'chcemvediet.auth_backends.DjangoModelBackendWithAdminLoginAs',
u'chcemvediet.auth_backends.AllauthAuthenticationBackendWithAdminLoginAs',
)

TEMPLATE_LOADERS = (
Expand Down
74 changes: 74 additions & 0 deletions poleno/utils/admin_login_as.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from django.conf.urls import patterns, url
from django.contrib import admin
from django.contrib.auth.backends import ModelBackend
from django.core.urlresolvers import resolve, Resolver404
from django.http import HttpResponseRedirect

from poleno.utils.http import get_request
from poleno.utils.urls import reverse


class AdminLoginAsBackendMixin(ModelBackend):
u"""
Django authentication ModelBackend that allows admin to log in as any user (without being logged
off) and perform any action on his behalf, without the admin knowing the user's password. Admin
can simultaneously perform actions on admin page. Must be used together with
``AdminLoginAsAdminMixin``. Note that admin application namespace must be called 'admin'.
Example:
class MyApplicationBackend(AdminLoginAsBackendMixin, SomeAuthenticationBackend):
...
Settings:
AUTHENTICATION_BACKENDS = (
'path.to.MyApplicationBackend',
...
)
"""
def is_admin_path(self, path):
return resolve(path).namespace == u'admin'

def get_user(self, user_id):
request = get_request()
user = super(AdminLoginAsBackendMixin, self).get_user(user_id)
if request is None:
return user
try:
resolve(request.path)
except Resolver404:
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(AdminLoginAsBackendMixin, self).get_user(admin_login_as) or user
return user

class AdminLoginAsAdminMixin(admin.ModelAdmin):
u"""
Django ModelAdmin that defines view, which allows admin to be simultaneously logged in as any
user. Must be used together with ``AdminLoginAsBackendMixin``.
Example:
class MyModelAdmin(AdminLoginAsAdminMixin, admin.ModelAdmin):
list_display = [
...
decorate(
lambda o: admin_obj_format(o, u'Log in', link=u'login_as'),
),
]
LOGIN_AS_REDIRECT_VIEWNAME = u'application:viewname'
...
"""
def login_as_view(self, request, obj_pk):
request.session[u'admin_login_as'] = obj_pk
return HttpResponseRedirect(reverse(self.LOGIN_AS_REDIRECT_VIEWNAME))

def get_urls(self):
info = self.model._meta.app_label, self.model._meta.model_name
login_as_view = self.admin_site.admin_view(self.login_as_view)
urls = patterns('',
url(r'^(\d+)/login-as/$', login_as_view, name=u'{}_{}_login_as'.format(*info)),
)
return urls + super(AdminLoginAsAdminMixin, self).get_urls()
31 changes: 0 additions & 31 deletions poleno/utils/backends.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def create_users(self):

def setUp(self):
self.settings_override = override_settings(
AUTHENTICATION_BACKENDS=(u'poleno.utils.backends.AdminLoginAsBackendMixin',),
AUTHENTICATION_BACKENDS=(u'poleno.utils.admin_login_as.AdminLoginAsBackendMixin',),
PASSWORD_HASHERS=(u'django.contrib.auth.hashers.MD5PasswordHasher',),
)
self.settings_override.enable()
Expand Down

0 comments on commit 9cf1c2f

Please sign in to comment.