diff --git a/accounting/views.py b/accounting/views.py index 321ac29..14cdb3e 100644 --- a/accounting/views.py +++ b/accounting/views.py @@ -23,6 +23,7 @@ from django.http import HttpResponseRedirect, HttpResponse from django.urls import reverse from django.utils import dateformat, timezone +from django.utils.decorators import method_decorator from django.views import generic from django.views.decorators.http import require_GET @@ -30,10 +31,12 @@ from accounting.models import Record from accounting.utils import PeriodParser, Pagination, \ PageNoOutOfRangeError from mia import settings +from mia_core.digest_auth import digest_login_required from mia_core.utils import UrlBuilder @require_GET +@digest_login_required def home(request): """The accounting home page. @@ -45,6 +48,7 @@ def home(request): @require_GET +@digest_login_required def cash_home(request): """The accounting cash report home page. @@ -58,6 +62,7 @@ def cash_home(request): reverse("accounting:cash", args=(subject_code, period_spec))) +@method_decorator(digest_login_required, name='dispatch') class BaseReportView(generic.ListView): """A base account report. @@ -115,7 +120,6 @@ class BaseReportView(generic.ListView): return r - class CashReportView(BaseReportView): """The accounting cash report.""" http_method_names = ["get"] diff --git a/mia_core/digest_auth.py b/mia_core/digest_auth.py index 6eeaae1..12c8c52 100644 --- a/mia_core/digest_auth.py +++ b/mia_core/digest_auth.py @@ -18,6 +18,10 @@ """The utilities for the HTTP digest authentication. """ +from functools import wraps + +from django.http import HttpResponse + from mia_core.models import User @@ -52,3 +56,19 @@ class AccountBackend: print("mia_core.digest_auth.AccountBackend.get_user(): " + str(User.objects.filter( login_id=username).first())) return User.objects.filter(login_id=username).first() + + +def digest_login_required(function=None): + """The decorator to check if the user has logged in, and send + HTTP 401 if the user has not logged in. + """ + def decorator(view_func): + @wraps(view_func) + def _wrapped_view(request, *args, **kwargs): + if request.user.is_anonymous: + return HttpResponse(status=401) + return view_func(request, *args, **kwargs) + return _wrapped_view + if function: + return decorator(function) + return decorator