From b1b83cd5936cd1fe804bd85d5bb2a74b5de6bc71 Mon Sep 17 00:00:00 2001 From: Timothy Hobbs Date: Sun, 18 Feb 2024 10:04:01 +0100 Subject: [PATCH] Add some convenient functions for protecting views in custom installations --- helpdesk/decorators.py | 4 ++++ helpdesk/settings.py | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/helpdesk/decorators.py b/helpdesk/decorators.py index e003440a..82ef925d 100644 --- a/helpdesk/decorators.py +++ b/helpdesk/decorators.py @@ -49,6 +49,8 @@ def protect_view(view_func): return redirect('helpdesk:login') elif not request.user.is_authenticated and helpdesk_settings.HELPDESK_ANON_ACCESS_RAISES_404: raise Http404 + if auth_redirect := helpdesk_settings.HELPDESK_PUBLIC_VIEW_PROTECTOR(request): + return auth_redirect return view_func(request, *args, **kwargs) return _wrapped_view @@ -65,6 +67,8 @@ def staff_member_required(view_func): return redirect('helpdesk:login') if not helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE and not request.user.is_staff: raise PermissionDenied() + if auth_redirect := helpdesk_settings.HELPDESK_STAFF_VIEW_PROTECTOR(request): + return auth_redirect return view_func(request, *args, **kwargs) return _wrapped_view diff --git a/helpdesk/settings.py b/helpdesk/settings.py index cf5ca644..2cd0ac4e 100644 --- a/helpdesk/settings.py +++ b/helpdesk/settings.py @@ -47,6 +47,14 @@ HELPDESK_REDIRECT_TO_LOGIN_BY_DEFAULT = getattr(settings, 'HELPDESK_REDIRECT_TO_LOGIN_BY_DEFAULT', False) +HELPDESK_PUBLIC_VIEW_PROTECTOR = getattr(settings, + 'HELPDESK_PUBLIC_VIEW_PROTECTOR', + lambda _: None) + +HELPDESK_STAFF_VIEW_PROTECTOR = getattr(settings, + 'HELPDESK_STAFF_VIEW_PROTECTOR', + lambda _: None) + # Enable the Dependencies field on ticket view HELPDESK_ENABLE_DEPENDENCIES_ON_TICKET = getattr(settings, 'HELPDESK_ENABLE_DEPENDENCIES_ON_TICKET',