Commit 3694d44d authored by Kostas Papadimitriou's avatar Kostas Papadimitriou
Browse files

astakos: Dynamically resolve login_url in login_required

duplicated django user_passes_test and login_required decorators to be able
to dynamicaly resolve the url to which we redirect unauthorized users. Replaced
django login_required with astakos.im.views.decorators.login_required.
parent 872409b0
......@@ -33,16 +33,58 @@
from functools import wraps
from django.utils.http import urlquote
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse
from django.http import HttpResponse, HttpResponseRedirect
from django.utils.decorators import available_attrs
from django.utils.http import urlencode
from astakos.im import auth_providers as auth
from astakos.im.cookie import CookieHandler
REDIRECT_FIELD_NAME = 'next'
def user_passes_test(test_func, login_url=None,
redirect_field_name=REDIRECT_FIELD_NAME):
"""
Decorator for views that checks that the user passes the given test,
redirecting to the log-in page if necessary. The test should be a callable
that takes the user object and returns True if the user passes.
"""
if not login_url:
from django.conf import settings
login_url = settings.LOGIN_URL
def decorator(view_func):
def _wrapped_view(request, *args, **kwargs):
if test_func(request.user):
return view_func(request, *args, **kwargs)
path = urlquote(request.get_full_path())
tup = reverse('login'), redirect_field_name, path
return HttpResponseRedirect('%s?%s=%s' % tup)
return wraps(view_func,
assigned=available_attrs(view_func))(_wrapped_view)
return decorator
def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME,
login_url=None):
"""
Decorator for views that checks that the user is logged in, redirecting
to the log-in page if necessary.
"""
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 cookie_fix(func):
"""
......@@ -71,6 +113,8 @@ def cookie_fix(func):
def requires_auth_provider(provider_id, **perms):
"""
View requires specified authentication module to be enabled in
ASTAKOS_IM_MODULES setting.
"""
def decorator(func, *args, **kwargs):
@wraps(func)
......
......@@ -40,7 +40,6 @@ from urllib import quote
from django.shortcuts import get_object_or_404
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.db import transaction
......@@ -71,7 +70,7 @@ from astakos.im import auth_providers as auth
from astakos.im import quotas
from astakos.im.views.util import render_response, _resources_catalog
from astakos.im.views.decorators import cookie_fix, signed_terms_required,\
required_auth_methods_assigned, valid_astakos_user_required
required_auth_methods_assigned, valid_astakos_user_required, login_required
logger = logging.getLogger(__name__)
......
......@@ -40,7 +40,6 @@ from django_tables2 import RequestConfig
from django.shortcuts import get_object_or_404
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from django.http import Http404
from django.shortcuts import redirect
......@@ -70,7 +69,7 @@ from astakos.im.util import redirect_back
from astakos.im.views.util import render_response, _create_object, \
_update_object, _resources_catalog, ExceptionHandler
from astakos.im.views.decorators import cookie_fix, signed_terms_required,\
valid_astakos_user_required
valid_astakos_user_required, login_required
logger = logging.getLogger(__name__)
......
......@@ -39,7 +39,6 @@ from django.utils.translation import ugettext as _
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_http_methods
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required
import django.contrib.auth.views as django_auth_views
......@@ -51,7 +50,7 @@ from astakos.im import settings
import astakos.im.messages as astakos_messages
from astakos.im import auth_providers as auth
from astakos.im.views.decorators import cookie_fix, requires_anonymous, \
signed_terms_required, requires_auth_provider
signed_terms_required, requires_auth_provider, login_required
from ratelimit.decorators import ratelimit
......
......@@ -84,8 +84,6 @@ AUTHENTICATION_BACKENDS = (
'astakos.im.auth_backends.EmailBackend',
'astakos.im.auth_backends.TokenBackend')
LOGIN_URL = '/ui/'
CUSTOM_USER_MODEL = 'astakos.im.AstakosUser'
#SOUTH_TESTS_MIGRATE = False
......
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