2017-10-04 03:42:26 +02:00
|
|
|
from functools import wraps
|
|
|
|
|
2017-12-28 15:11:34 +01:00
|
|
|
from django.urls import reverse
|
2017-10-04 03:42:26 +02:00
|
|
|
from django.http import HttpResponseRedirect, Http404
|
|
|
|
from django.utils.decorators import available_attrs
|
2014-07-28 11:46:02 +02:00
|
|
|
from django.contrib.auth.decorators import user_passes_test
|
2017-10-04 03:42:26 +02:00
|
|
|
|
|
|
|
from helpdesk import settings as helpdesk_settings
|
|
|
|
|
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-03-04 10:24:30 +01:00
|
|
|
is_helpdesk_staff = lambda u: u.is_authenticated and u.is_active
|
2014-07-28 11:46:02 +02:00
|
|
|
else:
|
2018-03-04 10:24:30 +01:00
|
|
|
is_helpdesk_staff = lambda u: u.is_authenticated and u.is_active and u.is_staff
|
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-03-04 10:24:30 +01:00
|
|
|
helpdesk_superuser_required = user_passes_test(lambda u: u.is_authenticated and u.is_active and u.is_superuser)
|
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
|
|
|
|
"""
|
|
|
|
@wraps(view_func, assigned=available_attrs(view_func))
|
|
|
|
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:
|
2017-10-04 03:42:26 +02:00
|
|
|
return HttpResponseRedirect(reverse('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
|
2017-10-30 08:17:40 +01:00
|
|
|
|