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
|
|
|
|
|
|
|
|
from helpdesk import settings as helpdesk_settings
|
|
|
|
|
|
|
|
|
|
|
|
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
|