Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions requirements-frozen.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ django==2.2.18
# django-select2
# djangorestframework
# drf-spectacular
django-su==0.9.0
# via -r requirements/./base.txt
djangorestframework-jwt==1.11.0
# via -r requirements/./base.txt
djangorestframework==3.12.2
Expand Down
1 change: 1 addition & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ Sphinx>=3.3
whitenoise>=5.2
drf-spectacular>=0.11
weasyPrint==52.1
django-su>=0.9.0
2 changes: 2 additions & 0 deletions src/easydmp/auth/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

@admin.register(User)
class EasyDMPUserAdmin(UserAdmin):
change_form_template = "admin/auth/user/change_form.html" # For Django-su
change_list_template = "admin/auth/user/change_list.html" # For Django-su
date_hierarchy = 'date_joined'
list_display = ('username', 'email', 'full_name', 'is_staff')
search_fields = ('username', 'full_name', 'email')
Expand Down
17 changes: 17 additions & 0 deletions src/easydmp/auth/lib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.contrib.auth import login

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file, I would personally call it for example su.py.


from easydmp.eventlog.utils import log_event


def su_login(request, user):
original_user = request.user
Comment thread
hmpf marked this conversation as resolved.
switch_to_user = user
exit_users_pk = request.session.get("exit_users_pk", default=[])
if exit_users_pk:
template = '{timestamp} {actor} impersonated {target}'
verb = 'impersonate'
else:
template = '{timestamp} {actor} stopped impersonating {target}'
verb = 'stop impersonating'
log_event(request.user, 'impersonate', target=user, template='')
login(request, user)
7 changes: 7 additions & 0 deletions src/easydmp/site/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
# Application definition

INSTALLED_APPS = [
'django_su',
'django.contrib.admin',
'django.contrib.admindocs',
'django.contrib.auth',
Expand Down Expand Up @@ -92,6 +93,7 @@
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django_su.context_processors.is_su',
'easydmp.lib.context_processors.common',
],
},
Expand Down Expand Up @@ -194,6 +196,7 @@
LOGIN_REDIRECT_URL = '/plan/'

AUTHENTICATION_BACKENDS = [
'django_su.backends.SuBackend',
'django.contrib.auth.backends.RemoteUserBackend',
'django.contrib.auth.backends.ModelBackend',
'guardian.backends.ObjectPermissionBackend',
Expand All @@ -205,6 +208,10 @@

X_FRAME_OPTIONS = 'ALLOW-FROM https://bibsys.no/'

# Django-su

SU_CUSTOM_LOGIN_ACTION = 'easydmp.auth.lib.su_login'

# REST API

REST_FRAMEWORK = {
Expand Down
7 changes: 6 additions & 1 deletion src/easydmp/site/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@
<body>
<header role="banner">
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
{% if instance_message or messages %}
{% if instance_message or messages or IS_SU %}
<div class="container messages">
{% if IS_SU %}
<div role="alert" class="alert impersonation">You are impersonating {{ request.user }}
<a href="{% url 'su_logout' %}">Switch back to yourself</a>
</div>
{% endif %}
{% if instance_message %}
<div role="alert" class="alert instance">{{ instance_message }}</div>
{% endif %}
Expand Down
1 change: 1 addition & 0 deletions src/easydmp/site/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
path('logout', logout_view, name='logout'),
path('privacy/', PublicTemplateView.as_view(template_name='privacy.html'), name='privacy'),
path('account/', include('easydmp.auth.urls')),
path('^su/', include('django_su.urls')),

path('plan/', include('easydmp.plan.urls')),
path('invitation/', include('easydmp.invitation.urls')),
Expand Down