From f1291fec8d33256bc0d38b8764f3f59cae9f3ed1 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sun, 10 Mar 2019 02:41:31 +0100 Subject: [PATCH] add impersonation middleware, add to templates --- passbook/admin/middleware.py | 25 ++++++ passbook/admin/settings.py | 5 ++ .../templates/administration/user/list.html | 2 + passbook/core/templates/base/skeleton.html | 81 +++++++++++-------- 4 files changed, 78 insertions(+), 35 deletions(-) create mode 100644 passbook/admin/middleware.py create mode 100644 passbook/admin/settings.py diff --git a/passbook/admin/middleware.py b/passbook/admin/middleware.py new file mode 100644 index 000000000..1c4d1c42a --- /dev/null +++ b/passbook/admin/middleware.py @@ -0,0 +1,25 @@ +"""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: + request.session['impersonate_id'] = request.GET["__impersonate"] + # user wants to stop impersonation + elif "__unimpersonate" in request.GET and 'impersonate_id' in request.session: + del request.session['impersonate_id'] + + # Actually impersonate user + if request.user.is_superuser and 'impersonate_id' in request.session: + request.user = User.objects.get(pk=request.session['impersonate_id']) + + response = get_response(request) + return response + return middleware diff --git a/passbook/admin/settings.py b/passbook/admin/settings.py new file mode 100644 index 000000000..14f5e9421 --- /dev/null +++ b/passbook/admin/settings.py @@ -0,0 +1,5 @@ +"""passbook admin settings""" + +MIDDLEWARE = [ + 'passbook.admin.middleware.impersonate', +] diff --git a/passbook/admin/templates/administration/user/list.html b/passbook/admin/templates/administration/user/list.html index fe641038c..726936b1e 100644 --- a/passbook/admin/templates/administration/user/list.html +++ b/passbook/admin/templates/administration/user/list.html @@ -31,6 +31,8 @@ href="{% url 'passbook_admin:user-delete' pk=user.pk %}?back={{ request.get_full_path }}">{% trans 'Delete' %} {% trans 'Reset Password' %} + {% trans 'Impersonate' %} {% endfor %} diff --git a/passbook/core/templates/base/skeleton.html b/passbook/core/templates/base/skeleton.html index dad228996..dba86a0b8 100644 --- a/passbook/core/templates/base/skeleton.html +++ b/passbook/core/templates/base/skeleton.html @@ -4,40 +4,51 @@ - - - - - {% block title %} - {% title %} - {% endblock %} - - - - - - - - {% block head %} + + + + + + {% block title %} + {% title %} {% endblock %} - </head> - <body {% if is_login %} class="login-pf" {% endif %}> - {% block body %} - {% endblock %} - <script src="{% static 'js/jquery.min.js' %}"></script> - <script src="{% static 'js/bootstrap.min.js' %}"></script> - <script src="{% static 'js/patternfly.min.js' %}"></script> - <script src="{% static 'js/passbook.js' %}"></script> - {% block scripts %} - {% endblock %} - <div class="modals"> - {% include 'partials/about_modal.html' %} - </div> - </body> + + + + + + + + {% block head %} + {% endblock %} + + + + {% if 'impersonate_id' in request.session %} +
+ + {% blocktrans with user=user %}You're currently impersonating {{ user }}.{% endblocktrans %} + {% trans 'Stop impersonation' %} + +
+ {% endif %} + {% block body %} + {% endblock %} + + + + + {% block scripts %} + {% endblock %} +
+ {% include 'partials/about_modal.html' %} +
+ +