2020-12-27 10:49:54 +00:00
|
|
|
# Copyright (C) 2020 by Animath
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
|
|
from django.core.exceptions import PermissionDenied
|
|
|
|
from haystack.generic_views import SearchView
|
|
|
|
|
|
|
|
|
|
|
|
class AdminMixin(LoginRequiredMixin):
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
2020-12-29 15:14:56 +00:00
|
|
|
if request.user.is_authenticated and not request.user.registration.is_admin:
|
|
|
|
raise PermissionDenied
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
class UserMixin(LoginRequiredMixin):
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
user = request.user
|
|
|
|
if user.is_authenticated and not user.registration.is_admin and user.registration.pk != kwargs["pk"]:
|
2020-12-27 10:49:54 +00:00
|
|
|
raise PermissionDenied
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
class AdminSearchView(AdminMixin, SearchView):
|
|
|
|
pass
|