Guest User

ShawnMilo

a guest
Dec 16th, 2009
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. from django.http import HttpResponseRedirect
  2. from django.conf import settings
  3.  
  4. class LoginChecksMiddleware(object):
  5.    
  6.     """
  7.    A user has to be logged in to access
  8.    any screen other than the login screen
  9.    and password reset screen.
  10.    
  11.    A logged-in user must have changed their
  12.    password within the required period.
  13.    """
  14.    
  15.     def process_request(self, request):
  16.        
  17.         #don't restrict CSS & images
  18.         #don't break Django user tests (/remote_user/ URL)
  19.         if request.META['PATH_INFO'].startswith(settings.MEDIA_URL) or request.META['PATH_INFO'] == '/remote_user/':
  20.             return None
  21.        
  22.         if request.user.is_anonymous():
  23.             #if they're trying to log in, let them
  24.             if request.META['PATH_INFO'].startswith('/login/') or \
  25.                 request.META['PATH_INFO'].startswith('/password_reset') or \
  26.                 request.META['PATH_INFO'].startswith('/register') or \
  27.                 request.META['PATH_INFO'].startswith('/reset/'):
  28.                 return None
  29.                
  30.             #redirect to login
  31.             return HttpResponseRedirect('/login/')
  32.         else:
  33.             #if they're trying to go to the password_change
  34.             #page, let them in any case
  35.             if request.META['PATH_INFO'] == '/password_change/':
  36.                 return None
  37.  
  38.             #if password is expired, redirect
  39.             if request.user.get_profile().password_is_expired():
  40.                 return HttpResponseRedirect('/password_change/')
  41.        
  42.  
  43.         #if nothing was triggered,
  44.         #just return None
  45.         return None
Advertisement
Add Comment
Please, Sign In to add comment