aboutsummaryrefslogtreecommitdiff
path: root/app/dispatch/middleware.py
diff options
context:
space:
mode:
Diffstat (limited to 'app/dispatch/middleware.py')
-rw-r--r--app/dispatch/middleware.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/app/dispatch/middleware.py b/app/dispatch/middleware.py
new file mode 100644
index 0000000..2b3e349
--- /dev/null
+++ b/app/dispatch/middleware.py
@@ -0,0 +1,31 @@
+from django.http import HttpResponseRedirect
+from django.conf import settings
+from re import compile
+from django.core.urlresolvers import reverse
+
+EXEMPT_URLS = [reverse('login'),reverse('logout')]
+
+if hasattr(settings, 'LOGIN_EXEMPT_URLS'):
+ EXEMPT_URLS += settings.LOGIN_EXEMPT_URLS
+
+"""
+Middleware that requires a user to be authenticated to view any page other
+than LOGIN_URL. Exemptions to this requirement can optionally be specified
+in settings via a list of regular expressions in LOGIN_EXEMPT_URLS (which
+you can copy from your urls.py).
+
+Requires authentication middleware and template context processors to be
+loaded. You'll get an error if they aren't.
+"""
+class LoginRequiredMiddleware(object):
+ def __init__(self, get_response):
+ self.get_response = get_response
+
+ def __call__(self, request):
+ if not request.user or not request.user.is_authenticated():
+ path = request.path_info
+ if path not in EXEMPT_URLS:
+ login_uri = '%s?next=%s' % (settings.LOGIN_URL, request.path_info)
+ return HttpResponseRedirect(login_uri)
+
+ return self.get_response(request) \ No newline at end of file