Login

Auto Logout Middleware

Author:
LuckiDog
Posted:
October 19, 2007
Language:
Python
Version:
.96
Tags:
middleware auth logout
Score:
5 (after 5 ratings)

This Middleware is to log users out after a certain amount of time has passed. You'll want to add AUTO_LOGOUT_DELAY to your settings.py, set to a number of minutes after which a user should be logged out.

It adds the key 'last_touch' to the session, you'll want to change that if you happen to be using that already.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
from django.conf import settings
from django.contrib import auth
from datetime import datetime, timedelta

class AutoLogout:
  def process_request(self, request):
    if not request.user.is_authenticated() :
      #Can't log out if not logged in
      return

    try:
      if datetime.now() - request.session['last_touch'] > timedelta( 0, settings.AUTO_LOGOUT_DELAY * 60, 0):
        auth.logout(request)
        del request.session['last_touch']
        return
    except KeyError:
      pass

    request.session['last_touch'] = datetime.now()

More like this

Comments

david_bgk (on October 22, 2007):
<p>Thanks for this snippet, an import is missing:</p> <pre>from django.contrib import auth </pre>

#

LuckiDog (on October 24, 2007):
<p>Thanks, I've updated the code.</p>

#

kmajbar (on November 25, 2007):
<p>Hello, </p> <p>I get the following error when I try to use your middleware :</p> <h1>----------------------------------------------------------</h1> <pre>Traceback (most recent call last): File "/var/lib/python-support/python2.5/django/core/servers/basehttp.py", line 272, in run self.result = application(self.environ, self.start_response) File "/var/lib/python-support/python2.5/django/core/servers/basehttp.py", line 614, in __call__ return self.application(environ, start_response) File "/var/lib/python-support/python2.5/django/core/handlers/wsgi.py", line 184, in __call__ self.load_middleware() File "/var/lib/python-support/python2.5/django/core/handlers/base.py", line 35, in load_middleware raise exceptions.ImproperlyConfigured, 'Middleware module "%s" does not define a "%s" class' % (mw_module, mw_classname) ImproperlyConfigured: Middleware module "WebGui" does not define a "AutoLogout" class </pre> <h1>----------------------------------------------------------</h1> <p>WebGui is my project and in it's settings.py I added 'WebGui.AutoLogout', in the middlware conf like so :</p> <pre>MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.middleware.doc.XViewMiddleware', 'WebGui.AutoLogout', ) </pre> <p>and I also added:</p> <pre>AUTO_LOGOUT_DELAY=1 </pre> <p>to settings.py like you said. Did I do something wrong?</p> <p>Thank you, Kenza</p>

#

LuckiDog (on February 22, 2010):
<p>Three years later, but you'll need to put this class in a file called middleware.py in your application</p>

#

LuckiDog (on February 22, 2010):
<p>@kmajbar To use this as a project level middleware, you would save this code as WebGui/middleware.py</p> <p>Then add 'WebGui.middleware.AutoLogout' to your MIDDLEWARE_CLASSES</p>

#

Please login first before commenting.