2019-03-10 01:41:31 +00:00
|
|
|
"""passbook admin Middleware to impersonate users"""
|
|
|
|
|
|
|
|
from passbook.core.models import User
|
|
|
|
|
|
|
|
|
|
|
|
def impersonate(get_response):
|
|
|
|
"""Middleware to impersonate users"""
|
|
|
|
|
|
|
|
def middleware(request):
|
|
|
|
"""Middleware to impersonate users"""
|
|
|
|
|
|
|
|
# User is superuser and has __impersonate ID set
|
|
|
|
if request.user.is_superuser and "__impersonate" in request.GET:
|
2019-12-31 11:51:16 +00:00
|
|
|
request.session["impersonate_id"] = request.GET["__impersonate"]
|
2019-03-10 01:41:31 +00:00
|
|
|
# user wants to stop impersonation
|
2019-12-31 11:51:16 +00:00
|
|
|
elif "__unimpersonate" in request.GET and "impersonate_id" in request.session:
|
|
|
|
del request.session["impersonate_id"]
|
2019-03-10 01:41:31 +00:00
|
|
|
|
|
|
|
# Actually impersonate user
|
2019-12-31 11:51:16 +00:00
|
|
|
if request.user.is_superuser and "impersonate_id" in request.session:
|
|
|
|
request.user = User.objects.get(pk=request.session["impersonate_id"])
|
2019-03-10 01:41:31 +00:00
|
|
|
|
|
|
|
response = get_response(request)
|
|
|
|
return response
|
2019-12-31 11:51:16 +00:00
|
|
|
|
2019-03-10 01:41:31 +00:00
|
|
|
return middleware
|