| 1 |
|
| 2 |
import logging |
| 3 |
|
| 4 |
from django.contrib import admin |
| 5 |
from django.db.utils import NotSupportedError |
| 6 |
from django.utils.html import format_html |
| 7 |
|
| 8 |
from .compat import reverse |
| 9 |
from .helpers import User, check_allow_impersonate |
| 10 |
from .models import ImpersonationLog |
| 11 |
from .settings import settings |
| 12 |
|
| 13 |
logger = logging.getLogger(__name__) |
| 14 |
|
| 15 |
|
| 16 |
def friendly_name(user): |
| 17 |
'''Return proper name if exists, else username.''' |
| 18 |
name = None |
| 19 |
if hasattr(user, 'get_full_name'): |
| 20 |
name = user.get_full_name() |
| 21 |
return name or getattr(user, getattr(User, 'USERNAME_FIELD', 'username')) |
| 22 |
|
| 23 |
|
| 24 |
class SessionStateFilter(admin.SimpleListFilter): |
| 25 |
''' Custom admin filter based on the session state. |
| 26 |
|
| 27 |
Provides two filter values - 'complete' and 'incomplete'. |
| 28 |
A session that has no session_ended_at timestamp is |
| 29 |
considered incomplete. This field is set from the |
| 30 |
session_end signal receiver. |
| 31 |
''' |
| 32 |
|
| 33 |
title = 'session state' |
| 34 |
parameter_name = 'session' |
| 35 |
|
| 36 |
def lookups(self, request, model_admin): |
| 37 |
return ( |
| 38 |
('incomplete', 'Incomplete'), |
| 39 |
('complete', 'Complete'), |
| 40 |
) |
| 41 |
|
| 42 |
def queryset(self, request, queryset): |
| 43 |
if self.value() == 'incomplete': |
| 44 |
return queryset.filter(session_ended_at__isnull=True) |
| 45 |
if self.value() == 'complete': |
| 46 |
return queryset.filter(session_ended_at__isnull=False) |
| 47 |
else: |
| 48 |
return queryset |
| 49 |
|
| 50 |
|
| 51 |
class ImpersonatorFilter(admin.SimpleListFilter): |
| 52 |
''' Custom admin filter based on the impersonator. |
| 53 |
|
| 54 |
Provides a set of users who have impersonated at some point. |
| 55 |
It is assumed that this is a small list of users - a subset |
| 56 |
of staff and superusers. There is no corresponding filter |
| 57 |
for users who have been impersonated, as this could be a |
| 58 |
very large set of users. |
| 59 |
|
| 60 |
If the number of unique impersonators exceeds MAX_FILTER_SIZE, |
| 61 |
then the users are limited to the value of MAX_FILTER_SIZE. |
| 62 |
''' |
| 63 |
|
| 64 |
title = 'impersonator' |
| 65 |
parameter_name = 'impersonator' |
| 66 |
|
| 67 |
def lookups(self, request, model_admin): |
| 68 |
''' Return list of unique users who have been an impersonator. |
| 69 |
''' |
| 70 |
|
| 71 |
MAX_FILTER_SIZE = settings.MAX_FILTER_SIZE |
| 72 |
try: |
| 73 |
|
| 74 |
ids = list( |
| 75 |
model_admin.get_queryset(request,) |
| 76 |
.order_by() |
| 77 |
.values_list('impersonator_id', flat=True,) |
| 78 |
.distinct('impersonator_id') |
| 79 |
)[:MAX_FILTER_SIZE] |
| 80 |
except (NotSupportedError, NotImplementedError): |
| 81 |
|
| 82 |
qs = model_admin.get_queryset(request).only('impersonator_id')[ |
| 83 |
:MAX_FILTER_SIZE |
| 84 |
] |
| 85 |
ids = set([x.impersonator_id for x in qs]) |
| 86 |
|
| 87 |
if len(ids) > MAX_FILTER_SIZE: |
| 88 |
logger.info( |
| 89 |
( |
| 90 |
'Limiting admin list filter as number of impersonators ' |
| 91 |
'[{0}] exceeds MAX_FILTER_SIZE [{1}]' |
| 92 |
).format( |
| 93 |
len(ids), MAX_FILTER_SIZE, |
| 94 |
) |
| 95 |
) |
| 96 |
|
| 97 |
impersonators = User.objects.filter(id__in=ids).order_by( |
| 98 |
User.USERNAME_FIELD |
| 99 |
) |
| 100 |
for i in impersonators: |
| 101 |
yield (i.id, friendly_name(i)) |
| 102 |
|
| 103 |
def queryset(self, request, queryset): |
| 104 |
if self.value() in (None, ''): |
| 105 |
return queryset |
| 106 |
else: |
| 107 |
return queryset.filter(impersonator_id=self.value()) |
| 108 |
|
| 109 |
|
| 110 |
class UserAdminImpersonateMixin(object): |
| 111 |
''' Mixin to easily add user impersonation support via the Django |
| 112 |
admin change list page. |
| 113 |
''' |
| 114 |
|
| 115 |
open_new_window = False |
| 116 |
|
| 117 |
def get_list_display(self, request): |
| 118 |
if not check_allow_impersonate(request): |
| 119 |
return self.list_display |
| 120 |
list_display = list(self.list_display) |
| 121 |
list_display.append('impersonate_user') |
| 122 |
return list_display |
| 123 |
|
| 124 |
def impersonate_user(self, obj): |
| 125 |
target = '' |
| 126 |
if self.open_new_window: |
| 127 |
target = ' target="_blank"' |
| 128 |
return format_html( |
| 129 |
'<a href="{}"{}>Impersonate</a>', |
| 130 |
reverse('impersonate-start', args=[obj.id]), |
| 131 |
target, |
| 132 |
) |
| 133 |
|
| 134 |
impersonate_user.short_description = 'Impersonate User' |
| 135 |
|
| 136 |
|
| 137 |
class ImpersonationLogAdmin(admin.ModelAdmin): |
| 138 |
list_select_related = ['impersonator', 'impersonating'] |
| 139 |
list_display = ( |
| 140 |
'_impersonator', |
| 141 |
'_impersonating', |
| 142 |
'session_key', |
| 143 |
'session_started_at', |
| 144 |
'duration', |
| 145 |
) |
| 146 |
readonly_fields = ( |
| 147 |
'impersonator', |
| 148 |
'impersonating', |
| 149 |
'session_key', |
| 150 |
'session_started_at', |
| 151 |
'session_ended_at', |
| 152 |
) |
| 153 |
list_filter = ( |
| 154 |
SessionStateFilter, |
| 155 |
ImpersonatorFilter, |
| 156 |
'session_started_at', |
| 157 |
) |
| 158 |
|
| 159 |
def _impersonator(self, obj): |
| 160 |
return friendly_name(obj.impersonator) |
| 161 |
|
| 162 |
def _impersonating(self, obj): |
| 163 |
return friendly_name(obj.impersonating) |
| 164 |
|
| 165 |
def has_add_permission(self, request): |
| 166 |
return settings.ADMIN_ADD_PERMISSION |
| 167 |
|
| 168 |
def has_delete_permission(self, request, obj=None): |
| 169 |
return settings.ADMIN_DELETE_PERMISSION |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
def has_change_permission(self, request, obj=None): |
| 174 |
if settings.ADMIN_READ_ONLY: |
| 175 |
return request.method in ['GET', 'HEAD', 'OPTIONS'] |
| 176 |
return True |
| 177 |
|
| 178 |
|
| 179 |
admin.site.register(ImpersonationLog, ImpersonationLogAdmin) |