Add some convenient functions for protecting views in custom installations

This commit is contained in:
Timothy Hobbs 2024-02-18 10:04:01 +01:00
parent f379bbe80e
commit b1b83cd593
2 changed files with 12 additions and 0 deletions

View File

@ -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

View File

@ -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',