27 lines
802 B
Python
27 lines
802 B
Python
from django.http import HttpResponseRedirect
|
|
from django.conf import settings
|
|
|
|
import re
|
|
|
|
|
|
class LoginRequiredMiddleware:
|
|
"""
|
|
If user is not accessing the site from an authorized IP, force
|
|
authentification.
|
|
"""
|
|
def __init__(self, get_response):
|
|
"""Init middleware"""
|
|
self.get_response = get_response
|
|
self.whitelist_re = re.compile("^/accounts/.*$")
|
|
|
|
def __call__(self, request):
|
|
"""
|
|
If user is not authenticated and external, redirect to login view
|
|
before calling the view.
|
|
"""
|
|
if not request.user.is_authenticated:
|
|
if not self.whitelist_re.match(request.path_info):
|
|
return HttpResponseRedirect(settings.LOGIN_URL)
|
|
|
|
response = self.get_response(request)
|
|
return response
|