Commit 339f9080 by Brodie Rao

Fixed issues involving combining multiple login decorators by removing…

Fixed issues involving combining multiple login decorators by removing _CheckLogin usage (and removed login_required, which isn't useful to have the 403 behavior with)
parent 58229f4d
"""Replacement authentication decorators that work around redirection loops""" """Replacement authentication decorators that work around redirection loops"""
try:
from functools import wraps
except ImportError:
from django.utils.functional import wraps
from django.contrib.auth import REDIRECT_FIELD_NAME from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.auth.decorators import _CheckLogin
from django.http import HttpResponseForbidden from django.http import HttpResponseForbidden
__all__ = ['login_required', 'permission_required', 'user_passes_test'] __all__ = ['permission_required', 'user_passes_test']
class CheckLoginOrForbid(_CheckLogin):
def __call__(self, request, *args, **kwargs):
if self.test_func(request.user):
return self.view_func(request, *args, **kwargs)
else:
return HttpResponseForbidden('<h1>Permission denied</h1>')
def user_passes_test(test_func, login_url=None, def user_passes_test(test_func, login_url=None,
redirect_field_name=REDIRECT_FIELD_NAME): redirect_field_name=REDIRECT_FIELD_NAME):
...@@ -21,10 +16,15 @@ def user_passes_test(test_func, login_url=None, ...@@ -21,10 +16,15 @@ def user_passes_test(test_func, login_url=None,
returns 403 Forbidden if the user is already logged in. returns 403 Forbidden if the user is already logged in.
""" """
def decorate(view_func): def decorator(view_func):
return CheckLoginOrForbid(view_func, test_func, login_url, @wraps(view_func)
redirect_field_name) def wrapper(request, *args, **kwargs):
return decorate if test_func(request.user):
return view_func(request, *args, **kwargs)
else:
return HttpResponseForbidden('<h1>Permission denied</h1>')
return wrapper
return decorator
def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME): def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment