| 1 |
|
| 2 |
import logging |
| 3 |
import hashlib |
| 4 |
from django.dispatch import Signal, receiver |
| 5 |
from django.utils.timezone import now as tz_now |
| 6 |
from django.utils.crypto import get_random_string |
| 7 |
|
| 8 |
from .settings import settings |
| 9 |
from .models import ImpersonationLog |
| 10 |
|
| 11 |
logger = logging.getLogger(__name__) |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
session_begin = Signal() |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
session_end = Signal() |
| 20 |
|
| 21 |
ID_LENGTH = 12 |
| 22 |
|
| 23 |
def gen_unique_id(): |
| 24 |
return hashlib.sha1( |
| 25 |
u'{0}:{1}'.format(get_random_string(ID_LENGTH), tz_now()).encode('utf-8') |
| 26 |
).hexdigest() |
| 27 |
|
| 28 |
|
| 29 |
@receiver(session_begin, dispatch_uid='impersonate.signals.on_session_begin') |
| 30 |
def on_session_begin(sender, **kwargs): |
| 31 |
''' Create a new ImpersonationLog object. |
| 32 |
''' |
| 33 |
impersonator = kwargs.get('impersonator') |
| 34 |
impersonating = kwargs.get('impersonating') |
| 35 |
logger.info(u'{0} has started impersonating {1}.'.format( |
| 36 |
impersonator, |
| 37 |
impersonating, |
| 38 |
)) |
| 39 |
|
| 40 |
if settings.DISABLE_LOGGING: |
| 41 |
return |
| 42 |
|
| 43 |
request = kwargs.get('request') |
| 44 |
session_key = gen_unique_id() |
| 45 |
|
| 46 |
ImpersonationLog.objects.create( |
| 47 |
impersonator=impersonator, |
| 48 |
impersonating=impersonating, |
| 49 |
session_key=session_key, |
| 50 |
session_started_at=tz_now() |
| 51 |
) |
| 52 |
|
| 53 |
request.session['_impersonate_session_id'] = session_key |
| 54 |
request.session.modified = True |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
@receiver(session_end, dispatch_uid='impersonate.signals.on_session_end') |
| 59 |
def on_session_end(sender, **kwargs): |
| 60 |
''' Update ImpersonationLog with the end timestamp. |
| 61 |
|
| 62 |
This uses the combination of session_key, impersonator and |
| 63 |
user being impersonated to look up the corresponding |
| 64 |
impersonation log object. |
| 65 |
''' |
| 66 |
impersonator = kwargs.get('impersonator') |
| 67 |
impersonating = kwargs.get('impersonating') |
| 68 |
logger.info(u'{0} has finished impersonating {1}.'.format( |
| 69 |
impersonator, |
| 70 |
impersonating, |
| 71 |
)) |
| 72 |
|
| 73 |
if settings.DISABLE_LOGGING: |
| 74 |
return |
| 75 |
|
| 76 |
request = kwargs.get('request') |
| 77 |
session_key = request.session.get('_impersonate_session_id', None) |
| 78 |
|
| 79 |
try: |
| 80 |
|
| 81 |
log = ImpersonationLog.objects.get( |
| 82 |
impersonator=impersonator, |
| 83 |
impersonating=impersonating, |
| 84 |
session_key=session_key, |
| 85 |
session_ended_at__isnull=True, |
| 86 |
) |
| 87 |
log.session_ended_at = tz_now() |
| 88 |
log.save() |
| 89 |
except ImpersonationLog.DoesNotExist: |
| 90 |
logger.debug( |
| 91 |
(u'Unfinished ImpersonationLog could not be found for: ' |
| 92 |
u'{0}, {1}, {2}').format( |
| 93 |
impersonator, |
| 94 |
impersonating, |
| 95 |
session_key, |
| 96 |
) |
| 97 |
) |
| 98 |
except ImpersonationLog.MultipleObjectsReturned: |
| 99 |
logger.debug( |
| 100 |
(u'Multiple unfinished ImpersonationLog matching: ' |
| 101 |
u'{0}, {1}, {2}').format( |
| 102 |
impersonator, |
| 103 |
impersonating, |
| 104 |
session_key, |
| 105 |
) |
| 106 |
) |
| 107 |
|
| 108 |
del request.session['_impersonate_session_id'] |
| 109 |
request.session.modified = True |