Implement impersonation

This commit is contained in:
Jan Dittberner 2023-04-15 11:48:53 +02:00
parent 472e272305
commit d499b781d4
7 changed files with 135 additions and 37 deletions

View file

@ -86,7 +86,6 @@ USE_TZ = True
LOCALE_PATHS = (normpath(join(SITE_ROOT, "gnuviechadmin", "locale")),)
# ######### MEDIA CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-root
MEDIA_ROOT = normpath(join(SITE_ROOT, "media"))
@ -180,7 +179,6 @@ AUTHENTICATION_BACKENDS = (
"allauth.account.auth_backends.AuthenticationBackend",
)
# ######### URL CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#root-urlconf
ROOT_URLCONF = "%s.urls" % SITE_NAME
@ -208,6 +206,7 @@ DJANGO_APPS = (
# Flatpages for about page
"django.contrib.flatpages",
"crispy_forms",
"impersonate",
)
ALLAUTH_APPS = (
@ -277,7 +276,7 @@ LOGGING = {
"formatters": {
"verbose": {
"format": "%(levelname)s %(asctime)s %(name)s "
"%(module)s:%(lineno)d %(process)d %(thread)d %(message)s"
"%(module)s:%(lineno)d %(process)d %(thread)d %(message)s"
},
"simple": {"format": "%(levelname)s %(name)s:%(lineno)d %(message)s"},
},
@ -366,7 +365,10 @@ def show_debug_toolbar(request):
# See: http://django-debug-toolbar.readthedocs.org/en/latest/installation.html#explicit-setup # noqa
INSTALLED_APPS += ("debug_toolbar",)
MIDDLEWARE += ["debug_toolbar.middleware.DebugToolbarMiddleware"]
MIDDLEWARE += [
"impersonate.middleware.ImpersonateMiddleware",
"debug_toolbar.middleware.DebugToolbarMiddleware",
]
DEBUG_TOOLBAR_CONFIG = {
"SHOW_TOOLBAR_CALLBACK": "gnuviechadmin.settings.show_debug_toolbar"
@ -403,21 +405,21 @@ if GVA_ENVIRONMENT == "local":
[
(key, {"handlers": ["console"], "level": "DEBUG", "propagate": True})
for key in [
"dashboard",
"domains",
"fileservertasks",
"gvacommon",
"gvawebcore",
"hostingpackages",
"ldaptasks",
"managemails",
"mysqltasks",
"osusers",
"pgsqltasks",
"taskresults",
"userdbs",
"websites",
]
"dashboard",
"domains",
"fileservertasks",
"gvacommon",
"gvawebcore",
"hostingpackages",
"ldaptasks",
"managemails",
"mysqltasks",
"osusers",
"pgsqltasks",
"taskresults",
"userdbs",
"websites",
]
]
)
)
@ -438,21 +440,21 @@ elif GVA_ENVIRONMENT == "test":
[
(key, {"handlers": ["console"], "level": "ERROR", "propagate": True})
for key in [
"dashboard",
"domains",
"fileservertasks",
"gvacommon",
"gvawebcore",
"hostingpackages",
"ldaptasks",
"managemails",
"mysqltasks",
"osusers",
"pgsqltasks",
"taskresults",
"userdbs",
"websites",
]
"dashboard",
"domains",
"fileservertasks",
"gvacommon",
"gvawebcore",
"hostingpackages",
"ldaptasks",
"managemails",
"mysqltasks",
"osusers",
"pgsqltasks",
"taskresults",
"userdbs",
"websites",
]
]
)
)

View file

@ -11,6 +11,8 @@ admin.autodiscover()
urlpatterns = [
re_path(r"", include("dashboard.urls")),
re_path(r"^admin/", admin.site.urls),
re_path(r"^impersonate/", include("impersonate.urls")),
re_path(r"^accounts/", include("allauth.urls")),
re_path(r"^database/", include("userdbs.urls")),
re_path(r"^domains/", include("domains.urls")),
@ -18,7 +20,6 @@ urlpatterns = [
re_path(r"^website/", include("websites.urls")),
re_path(r"^mail/", include("managemails.urls")),
re_path(r"^osuser/", include("osusers.urls")),
re_path(r"^admin/", admin.site.urls),
re_path(r"^contact/", include("contact_form.urls")),
re_path(r"^impressum/$", views.flatpage, {"url": "/impressum/"}, name="imprint"),
]

View file

@ -71,6 +71,7 @@
<li class="dropdown{% if active_item == 'account' %} active{% endif %}">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-user"></i> {% trans "My Account" %} <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
{% if user.is_superuser %}<li><a href="{% url 'impersonate-search' %}"><i class="fa fa-angellist"></i> {% trans "Impersonate user" %}</a></li>{% endif %}
{% if user.is_staff %}<li><a href="{% url 'admin:index' %}"><i class="fa fa-wrench"></i> {% trans "Admin site" %}</a></li>{% endif %}
<li><a href="{% url 'account_email' %}"><i class="fa fa-at"></i> {% trans "Change Email" %}</a></li>
<li><a href="{% url 'socialaccount_connections' %}"><i class="fa fa-users"></i> {% trans "Social Accounts" %}</a></li>
@ -85,7 +86,13 @@
{% if user.is_authenticated %}
{% user_display user as user_display %}
{% url 'user_profile' slug=user.username as profile_url %}
<p class="navbar-text navbar-right">{% blocktrans %}Signed in as <a href="{{ profile_url }}" class="navbar-link" title="My Profile">{{ user_display }}</a>{% endblocktrans %}</p>
{% if user.is_impersonate %}
{% user_display user.impersonator as impersonator_display %}
{% url 'impersonate-stop' as stop_impersonation_url %}
<p class="navbar-text navbar-right">{% blocktrans %}Signed in as <a href="{{ profile_url }}" class="navbar-link" title="My Profile">{{ user_display }}</a> (impersonated by {{ impersonator_display }}, <a href="{{ stop_impersonation_url }}" class="navbar-link">stop impersonation</a>){% endblocktrans %}</p>
{% else %}
<p class="navbar-text navbar-right">{% blocktrans %}Signed in as <a href="{{ profile_url }}" class="navbar-link" title="My Profile">{{ user_display }}</a>{% endblocktrans %}</p>
{% endif %}
{% endif %}
</div><!--/.nav-collapse -->
</div>

View file

@ -0,0 +1,31 @@
{% extends "base.html" %}
{% load i18n %}
{% block title %}{{ block.super }} - {% trans "Django Impersonate - User List" %}{% endblock title %}
{% block page_title %}{% blocktrans %}User List - Page {{ page_number }}{% endblocktrans %}{% endblock page_title %}
{% block content %}
{% if page.object_list %}
<ul class="list-group">
{% for user in page.object_list %}
<li class="list-group-item"><a href="{% url 'impersonate-start' user.pk %}{{ redirect }}">{{ user }}</a>
- Impersonate
</li>
{% endfor %}
</ul>
{% endif %}
<p>
<a href="{% url 'impersonate-search' %}">{% trans "Search users" %}</a>
</p>
<p>
{% if page.has_previous %}
<a href="{% url 'impersonate-list' %}?page={{ page.previous_page_number }}">Previous Page</a> &nbsp;
{% endif %}
{% if page.has_next %}
<a href="{% url 'impersonate-list' %}?page={{ page.next_page_number }}">Next Page</a> &nbsp;
{% endif %}
</p>
{% endblock %}

View file

@ -0,0 +1,45 @@
{% extends "base.html" %}
{% load i18n %}
{% block title %}{{ block.super }} - {% trans "Django Impersonate - Search Users" %}{% endblock title %}
{% block page_title %}Search Users {% if query %}- Page {{ page_number }}{% endif %}{% endblock page_title %}
{% block content %}
<form action="{% url 'impersonate-search' %}" method="GET">
{{ redirect_field }}
<div class="form-group">
<label for="user-query">{% trans "Enter Search Query:" %}</label>
<input type="text" name="q" id="user-query" class="form-control"
value="{% if query %}{{ query }}{% endif %}">
</div>
<button type="submit" class="btn btn-primary">{% trans "Search" %}</button>
</form>
<p>
<a href="{% url 'impersonate-list' %}">{% trans "List all users" %}</a>
</p>
<p>
{% if query and page.object_list %}
<ul class="list-group">
{% for user in page.object_list %}
<li class="list-group-item"><a
href="{% url 'impersonate-start' user.pk %}{{ redirect }}">{{ user }}</a> - Impersonate
</li>
{% endfor %}
</ul>
{% endif %}
</p>
<p>
{% if query and page.has_previous %}
<a href="{% url 'impersonate-search' %}?page={{ page.previous_page_number }}&q={{ query|urlencode }}">Previous
Page</a> &nbsp;
{% endif %}
{% if query and page.has_next %}
<a href="{% url 'impersonate-search' %}?page={{ page.next_page_number }}&q={{ query|urlencode }}">Next
Page</a>
{% endif %}
</p>
{% endblock %}

13
poetry.lock generated
View file

@ -666,6 +666,17 @@ files = [
django = ">=3.2.4"
sqlparse = ">=0.2"
[[package]]
name = "django-impersonate"
version = "1.9.1"
description = "Django app to allow superusers to impersonate other users."
category = "main"
optional = false
python-versions = "*"
files = [
{file = "django-impersonate-1.9.1.tar.gz", hash = "sha256:0befdb096198b458507239a6f21574c9e0f608ab01fad352d71eb9284e5bb9c9"},
]
[[package]]
name = "django-model-utils"
version = "4.3.1"
@ -1742,4 +1753,4 @@ testing = ["big-O", "flake8 (<5)", "jaraco.functools", "jaraco.itertools", "more
[metadata]
lock-version = "2.0"
python-versions = "^3.7"
content-hash = "6041c8bb49cd1df098f1948f8ad2cbd48fd8f42ff44e410f3fecb61be7e80a18"
content-hash = "dd56e0233689448f08dfcae943871bf9d72c05ad7bfd326c69f9ecb33ea8a461"

View file

@ -19,6 +19,7 @@ gvacommon = {version = "^0.6.0", source = "gnuviech"}
passlib = "^1.7.4"
redis = "^4.5.1"
requests-oauthlib = "^1.3.1"
django-impersonate = "^1.9.1"
[tool.poetry.group.dev.dependencies]