From e5c0878c7f8da415cc0574935111213df95f35cf Mon Sep 17 00:00:00 2001 From: Jonas Havelka Date: Thu, 16 Sep 2021 14:34:00 +0200 Subject: [PATCH] =?UTF-8?q?Fix=20p=C5=99=C3=ADstup=20org=C5=AF=20do=20Moje?= =?UTF-8?q?=20=C5=99e=C5=A1en=C3=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- seminar/urls.py | 4 ++-- seminar/utils.py | 16 +++++++++++++++- seminar/views/odevzdavatko.py | 3 ++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/seminar/urls.py b/seminar/urls.py index c2005cf0..ddd46d00 100644 --- a/seminar/urls.py +++ b/seminar/urls.py @@ -1,7 +1,7 @@ from django.urls import path, include, re_path from django.contrib.auth.decorators import login_required from . import views, export -from .utils import org_required, resitel_required, viewMethodSwitch +from .utils import org_required, resitel_required, viewMethodSwitch, resitel_or_org_required from django.views.generic.base import RedirectView urlpatterns = [ @@ -138,7 +138,7 @@ urlpatterns = [ path('odhlasit/', views.LogoutView.as_view(), name='logout'), path('logout/', RedirectView.as_view(pattern_name='logout', permanent=True, query_string=True)), path('resitel/', resitel_required(views.ResitelView.as_view()), name='seminar_resitel'), - path('resitel/odevzdana_reseni/', resitel_required(views.PrehledOdevzdanychReseni.as_view()), name='seminar_resitel_odevzdana_reseni'), + path('resitel/odevzdana_reseni/', resitel_or_org_required(views.PrehledOdevzdanychReseni.as_view()), name='seminar_resitel_odevzdana_reseni'), path('reset-hesla/', views.PasswordResetView.as_view(), name='reset_password'), path('zmena-hesla/', views.PasswordChangeView.as_view(), name='change_password'), path('reset-hesla/2/', views.PasswordResetDoneView.as_view(), name='reset_password_done'), diff --git a/seminar/utils.py b/seminar/utils.py index ac562048..1ff3c6e1 100644 --- a/seminar/utils.py +++ b/seminar/utils.py @@ -3,7 +3,8 @@ import datetime from django.contrib.auth import get_user_model -from django.contrib.auth.decorators import permission_required +from django.contrib.auth.decorators import permission_required, \ + user_passes_test from html.parser import HTMLParser from django import views as DjangoViews @@ -23,6 +24,19 @@ logger = logging.getLogger(__name__) org_required = permission_required('auth.org') resitel_required = permission_required('auth.resitel') + + +# inspirováno django.contrib.auth.decorators permission_required +def check_perms(user): + if user.has_perms(('auth.resitel',)): + return True + if user.has_perms(('auth.org',)): + return True + return False + + +resitel_or_org_required = user_passes_test(check_perms) + User = get_user_model() # Není to úplně hezké, ale budeme doufat, že to je funkční... User.je_org = property(lambda self: self.has_perm('auth.org')) diff --git a/seminar/views/odevzdavatko.py b/seminar/views/odevzdavatko.py index bafbf2ab..7ccfab6d 100644 --- a/seminar/views/odevzdavatko.py +++ b/seminar/views/odevzdavatko.py @@ -260,7 +260,8 @@ class PrehledOdevzdanychReseni(ListView): def get_queryset(self): if not self.request.user.is_authenticated: raise RuntimeError("Uživatel měl být přihlášený!") - resitel = m.Resitel.objects.get(osoba__user=self.request.user) + # get_or_none, aby neexistence řešitele (např. u orgů) neházela chybu + resitel = m.Resitel.objects.filter(osoba__user=self.request.user).first() qs = super().get_queryset() qs = qs.filter(reseni__resitele__in=[resitel]) return qs