Add COMMON_ALLOW_ANONYMOUS_ACCESS configuration option to allow access to non authenticated users

This commit is contained in:
Roberto Rosario
2012-01-08 04:54:15 -04:00
parent 467be05baa
commit c2fcbfe299
4 changed files with 53 additions and 15 deletions

View File

@@ -1,8 +1,12 @@
from __future__ import absolute_import
import re
from django.http import HttpResponseRedirect
from django.conf import settings
from ..conf.settings import ALLOW_ANONYMOUS_ACCESS
EXEMPT_URLS = [re.compile(settings.LOGIN_URL.lstrip('/'))]
if hasattr(settings, 'LOGIN_EXEMPT_URLS'):
EXEMPT_URLS += [re.compile(expr) for expr in settings.LOGIN_EXEMPT_URLS]
@@ -20,13 +24,14 @@ class LoginRequiredMiddleware:
"""
def process_request(self, request):
assert hasattr(request, 'user'), "The Login Required middleware\
requires authentication middleware to be installed. Edit your\
MIDDLEWARE_CLASSES setting to insert\
'django.contrib.auth.middlware.AuthenticationMiddleware'. If that doesn't\
work, ensure your TEMPLATE_CONTEXT_PROCESSORS setting includes\
'django.core.context_processors.auth'."
if not request.user.is_authenticated():
path = request.path_info.lstrip('/')
if not any(m.match(path) for m in EXEMPT_URLS):
return HttpResponseRedirect(settings.LOGIN_URL)
if not ALLOW_ANONYMOUS_ACCESS:
assert hasattr(request, 'user'), "The Login Required middleware\
requires authentication middleware to be installed. Edit your\
MIDDLEWARE_CLASSES setting to insert\
'django.contrib.auth.middlware.AuthenticationMiddleware'. If that doesn't\
work, ensure your TEMPLATE_CONTEXT_PROCESSORS setting includes\
'django.core.context_processors.auth'."
if not request.user.is_authenticated():
path = request.path_info.lstrip('/')
if not any(m.match(path) for m in EXEMPT_URLS):
return HttpResponseRedirect(settings.LOGIN_URL)