Auto Logout Middleware

 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

  1. Decorator to logout user based on a test and/or redirect to another url by vemubalu 3 years, 4 months ago
  2. StrictAuthentication - Auto log-out inactive users by yeago 5 years, 6 months ago
  3. Show logged users - keeping track of users login and logout by albertorcf 1 year, 7 months ago
  4. Login + logout script from a nub by alandelevie 5 years, 3 months ago
  5. Log to syslog by fylb 4 years, 2 months ago

Comments

david_bgk (on October 22, 2007):

Thanks for this snippet, an import is missing:

from django.contrib import auth

#

LuckiDog (on October 24, 2007):

Thanks, I've updated the code.

#

kmajbar (on November 25, 2007):

Hello,

I get the following error when I try to use your middleware :

----------------------------------------------------------

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

----------------------------------------------------------

WebGui is my project and in it's settings.py I added 'WebGui.AutoLogout', in the middlware conf like so :

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.middleware.doc.XViewMiddleware',
    'WebGui.AutoLogout',
)

and I also added:

AUTO_LOGOUT_DELAY=1

to settings.py like you said. Did I do something wrong?

Thank you, Kenza

#

LuckiDog (on February 22, 2010):

Three years later, but you'll need to put this class in a file called middleware.py in your application

#

LuckiDog (on February 22, 2010):

@kmajbar To use this as a project level middleware, you would save this code as WebGui/middleware.py

Then add 'WebGui.middleware.AutoLogout' to your MIDDLEWARE_CLASSES

#

(Forgotten your password?)