2022-07-22 03:26:41 +02:00
|
|
|
from django.contrib.auth.decorators import user_passes_test
|
2019-03-20 13:47:57 +01:00
|
|
|
from django.core.exceptions import PermissionDenied
|
|
|
|
from django.http import Http404
|
|
|
|
from django.shortcuts import redirect
|
2022-07-22 03:26:41 +02:00
|
|
|
from functools import wraps
|
2017-10-04 03:42:26 +02:00
|
|
|
from helpdesk import settings as helpdesk_settings
|
|
|
|
|
2018-07-19 06:06:57 +02:00
|
|
|
|
|
|
|
def check_staff_status(check_staff=False):
|
|
|
|
"""
|
|
|
|
Somewhat ridiculous currying to check user permissions without using lambdas.
|
|
|
|
The function most only take one User parameter at the end for use with
|
|
|
|
the Django function user_passes_test.
|
|
|
|
"""
|
|
|
|
def check_superuser_status(check_superuser):
|
|
|
|
def check_user_status(u):
|
|
|
|
is_ok = u.is_authenticated and u.is_active
|
|
|
|
if check_staff:
|
|
|
|
return is_ok and u.is_staff
|
|
|
|
elif check_superuser:
|
|
|
|
return is_ok and u.is_superuser
|
|
|
|
else:
|
|
|
|
return is_ok
|
|
|
|
return check_user_status
|
|
|
|
return check_superuser_status
|
|
|
|
|
|
|
|
|
2017-10-30 08:17:40 +01:00
|
|
|
if callable(helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE):
|
2014-07-28 11:46:02 +02:00
|
|
|
# apply a custom user validation condition
|
2017-10-30 08:17:40 +01:00
|
|
|
is_helpdesk_staff = helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE
|
|
|
|
elif helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE:
|
2014-07-28 11:46:02 +02:00
|
|
|
# treat 'normal' users like 'staff'
|
2018-07-19 06:06:57 +02:00
|
|
|
is_helpdesk_staff = check_staff_status(False)(False)
|
2014-07-28 11:46:02 +02:00
|
|
|
else:
|
2018-07-19 06:06:57 +02:00
|
|
|
is_helpdesk_staff = check_staff_status(True)(False)
|
2014-07-28 11:46:02 +02:00
|
|
|
|
2014-07-29 20:55:25 +02:00
|
|
|
helpdesk_staff_member_required = user_passes_test(is_helpdesk_staff)
|
2018-07-19 06:06:57 +02:00
|
|
|
helpdesk_superuser_required = user_passes_test(check_staff_status(False)(True))
|
|
|
|
|
2017-10-04 03:42:26 +02:00
|
|
|
|
|
|
|
def protect_view(view_func):
|
|
|
|
"""
|
|
|
|
Decorator for protecting the views checking user, redirecting
|
|
|
|
to the log-in page if necessary or returning 404 status code
|
|
|
|
"""
|
2020-10-13 00:11:07 +02:00
|
|
|
@wraps(view_func)
|
2017-10-04 03:42:26 +02:00
|
|
|
def _wrapped_view(request, *args, **kwargs):
|
2017-12-28 13:23:51 +01:00
|
|
|
if not request.user.is_authenticated and helpdesk_settings.HELPDESK_REDIRECT_TO_LOGIN_BY_DEFAULT:
|
2019-03-20 13:47:57 +01:00
|
|
|
return redirect('helpdesk:login')
|
2017-12-28 13:23:51 +01:00
|
|
|
elif not request.user.is_authenticated and helpdesk_settings.HELPDESK_ANON_ACCESS_RAISES_404:
|
2017-10-04 03:42:26 +02:00
|
|
|
raise Http404
|
|
|
|
return view_func(request, *args, **kwargs)
|
|
|
|
|
|
|
|
return _wrapped_view
|
2019-03-20 13:47:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
def staff_member_required(view_func):
|
|
|
|
"""
|
|
|
|
Decorator for staff member the views checking user, redirecting
|
|
|
|
to the log-in page if necessary or returning 403
|
|
|
|
"""
|
2020-10-13 00:11:07 +02:00
|
|
|
@wraps(view_func)
|
2019-03-20 13:47:57 +01:00
|
|
|
def _wrapped_view(request, *args, **kwargs):
|
|
|
|
if not request.user.is_authenticated and not request.user.is_active:
|
|
|
|
return redirect('helpdesk:login')
|
|
|
|
if not helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE and not request.user.is_staff:
|
|
|
|
raise PermissionDenied()
|
|
|
|
return view_func(request, *args, **kwargs)
|
|
|
|
|
|
|
|
return _wrapped_view
|
|
|
|
|
|
|
|
|
|
|
|
def superuser_required(view_func):
|
|
|
|
"""
|
|
|
|
Decorator for superuser member the views checking user, redirecting
|
|
|
|
to the log-in page if necessary or returning 403
|
|
|
|
"""
|
2020-10-13 00:11:07 +02:00
|
|
|
@wraps(view_func)
|
2019-03-20 13:47:57 +01:00
|
|
|
def _wrapped_view(request, *args, **kwargs):
|
|
|
|
if not request.user.is_authenticated and not request.user.is_active:
|
|
|
|
return redirect('helpdesk:login')
|
|
|
|
if not request.user.is_superuser:
|
|
|
|
raise PermissionDenied()
|
|
|
|
return view_func(request, *args, **kwargs)
|
|
|
|
|
|
|
|
return _wrapped_view
|