Commit 58229f4d by Brodie Rao

Added @login_required replacement

parent d0e24f70
...@@ -4,7 +4,7 @@ from django.contrib.auth import REDIRECT_FIELD_NAME ...@@ -4,7 +4,7 @@ from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.auth.decorators import _CheckLogin from django.contrib.auth.decorators import _CheckLogin
from django.http import HttpResponseForbidden from django.http import HttpResponseForbidden
__all__ = ['permission_required', 'user_passes_test'] __all__ = ['login_required', 'permission_required', 'user_passes_test']
class CheckLoginOrForbid(_CheckLogin): class CheckLoginOrForbid(_CheckLogin):
...@@ -27,6 +27,20 @@ def user_passes_test(test_func, login_url=None, ...@@ -27,6 +27,20 @@ def user_passes_test(test_func, login_url=None,
return decorate return decorate
def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME):
"""Replacement for django.contrib.auth.decorators.login_required that
returns 403 Forbidden if the user is already logged in.
"""
actual_decorator = user_passes_test(
lambda u: u.is_authenticated(),
redirect_field_name=redirect_field_name
)
if function:
return actual_decorator(function)
return actual_decorator
def permission_required(perm, login_url=None): def permission_required(perm, login_url=None):
"""Replacement for django.contrib.auth.decorators.permission_required that """Replacement for django.contrib.auth.decorators.permission_required that
returns 403 Forbidden if the user is already logged in. returns 403 Forbidden if the user is already logged in.
......
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