From d499b781d4d349561068d7af7ef07fd62cd6d8ef Mon Sep 17 00:00:00 2001
From: Jan Dittberner
Date: Sat, 15 Apr 2023 11:48:53 +0200
Subject: [PATCH] Implement impersonation
---
gnuviechadmin/gnuviechadmin/settings.py | 70 ++++++++++---------
gnuviechadmin/gnuviechadmin/urls.py | 3 +-
gnuviechadmin/templates/base.html | 9 ++-
.../templates/impersonate/list_users.html | 31 ++++++++
.../templates/impersonate/search_users.html | 45 ++++++++++++
poetry.lock | 13 +++-
pyproject.toml | 1 +
7 files changed, 135 insertions(+), 37 deletions(-)
create mode 100644 gnuviechadmin/templates/impersonate/list_users.html
create mode 100644 gnuviechadmin/templates/impersonate/search_users.html
diff --git a/gnuviechadmin/gnuviechadmin/settings.py b/gnuviechadmin/gnuviechadmin/settings.py
index b1a6d1c..243b6e8 100644
--- a/gnuviechadmin/gnuviechadmin/settings.py
+++ b/gnuviechadmin/gnuviechadmin/settings.py
@@ -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",
+ ]
]
)
)
diff --git a/gnuviechadmin/gnuviechadmin/urls.py b/gnuviechadmin/gnuviechadmin/urls.py
index 8be802e..8bcfca5 100644
--- a/gnuviechadmin/gnuviechadmin/urls.py
+++ b/gnuviechadmin/gnuviechadmin/urls.py
@@ -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"),
]
diff --git a/gnuviechadmin/templates/base.html b/gnuviechadmin/templates/base.html
index 8040f63..de0c35b 100644
--- a/gnuviechadmin/templates/base.html
+++ b/gnuviechadmin/templates/base.html
@@ -71,6 +71,7 @@
{% trans "My Account" %}
+
+
+ {% trans "List all users" %}
+
+
+
+ {% if query and page.object_list %}
+
+ {% for user in page.object_list %}
+ - {{ user }} - Impersonate
+
+ {% endfor %}
+
+ {% endif %}
+
+
+
+ {% if query and page.has_previous %}
+ Previous
+ Page
+ {% endif %}
+
+ {% if query and page.has_next %}
+ Next
+ Page
+ {% endif %}
+
+{% endblock %}
\ No newline at end of file
diff --git a/poetry.lock b/poetry.lock
index 720f7ea..9825743 100644
--- a/poetry.lock
+++ b/poetry.lock
@@ -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"
diff --git a/pyproject.toml b/pyproject.toml
index 3f3b448..3fcd8d1 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -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]