mirror of
https://gitlab.com/animath/si/plateforme-corres2math.git
synced 2025-02-06 08:53:00 +00:00
Add impersonification
This commit is contained in:
parent
d940acb226
commit
28e2fa10c3
@ -1,7 +1,8 @@
|
|||||||
from django.urls import path
|
from django.urls import path
|
||||||
|
|
||||||
from .views import MyAccountDetailView, SignupView, UserDetailView, UserResendValidationEmailView,\
|
from .views import MyAccountDetailView, ResetAdminView, SignupView, UserDetailView, UserImpersonateView, \
|
||||||
UserUpdateView, UserUploadPhotoAuthorizationView, UserValidateView, UserValidationEmailSentView
|
UserResendValidationEmailView, UserUpdateView, UserUploadPhotoAuthorizationView, UserValidateView, \
|
||||||
|
UserValidationEmailSentView
|
||||||
|
|
||||||
app_name = "registration"
|
app_name = "registration"
|
||||||
|
|
||||||
@ -16,4 +17,6 @@ urlpatterns = [
|
|||||||
path("user/<int:pk>/update/", UserUpdateView.as_view(), name="update_user"),
|
path("user/<int:pk>/update/", UserUpdateView.as_view(), name="update_user"),
|
||||||
path("user/<int:pk>/upload-photo-authorization/", UserUploadPhotoAuthorizationView.as_view(),
|
path("user/<int:pk>/upload-photo-authorization/", UserUploadPhotoAuthorizationView.as_view(),
|
||||||
name="upload_user_photo_authorization"),
|
name="upload_user_photo_authorization"),
|
||||||
|
path("user/<int:pk>/impersonate/", UserImpersonateView.as_view(), name="user_impersonate"),
|
||||||
|
path("reset-admin/", ResetAdminView.as_view(), name="reset_admin"),
|
||||||
]
|
]
|
||||||
|
@ -212,3 +212,35 @@ class PhotoAuthorizationView(LoginRequiredMixin, View):
|
|||||||
ext = mime_type.split("/")[1].replace("jpeg", "jpg")
|
ext = mime_type.split("/")[1].replace("jpeg", "jpg")
|
||||||
true_file_name = _("Photo authorization of {student}.{ext}").format(student=str(student), ext=ext)
|
true_file_name = _("Photo authorization of {student}.{ext}").format(student=str(student), ext=ext)
|
||||||
return FileResponse(open(path, "rb"), content_type=mime_type, filename=true_file_name)
|
return FileResponse(open(path, "rb"), content_type=mime_type, filename=true_file_name)
|
||||||
|
|
||||||
|
|
||||||
|
class UserImpersonateView(LoginRequiredMixin, RedirectView):
|
||||||
|
def dispatch(self, request, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
An administrator can log in through this page as someone else, and act as this other person.
|
||||||
|
"""
|
||||||
|
if self.request.user.registration.is_admin:
|
||||||
|
if not User.objects.filter(pk=kwargs["pk"]).exists():
|
||||||
|
raise Http404
|
||||||
|
session = request.session
|
||||||
|
session["admin"] = request.user.pk
|
||||||
|
session["_fake_user_id"] = kwargs["pk"]
|
||||||
|
return redirect(request.path)
|
||||||
|
return super().dispatch(request, *args, **kwargs)
|
||||||
|
|
||||||
|
def get_redirect_url(self, *args, **kwargs):
|
||||||
|
return reverse_lazy("registration:user_detail", args=(kwargs["pk"],))
|
||||||
|
|
||||||
|
|
||||||
|
class ResetAdminView(LoginRequiredMixin, View):
|
||||||
|
"""
|
||||||
|
Return to admin view, clear the session field that let an administrator to log in as someone else.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def dispatch(self, request, *args, **kwargs):
|
||||||
|
user = request.user
|
||||||
|
if not user.is_authenticated:
|
||||||
|
return self.handle_no_permission()
|
||||||
|
if "_fake_user_id" in request.session:
|
||||||
|
del request.session["_fake_user_id"]
|
||||||
|
return redirect(request.GET.get("path", "/"))
|
||||||
|
@ -111,7 +111,7 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
{% if "_fake_user_id" in request.session %}
|
{% if "_fake_user_id" in request.session %}
|
||||||
<li class="nav-item active">
|
<li class="nav-item active">
|
||||||
<a class="nav-link" href="{% url "member:reset_admin" %}?path={{ request.path }}"><i class="fas fa-tools"></i> {% trans "Return to admin view" %}</a>
|
<a class="nav-link" href="{% url "registration:reset_admin" %}?path={{ request.path }}"><i class="fas fa-tools"></i> {% trans "Return to admin view" %}</a>
|
||||||
</li>
|
</li>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if not user.is_authenticated %}
|
{% if not user.is_authenticated %}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user