~ubuntu-branches/ubuntu/quantal/python-django/quantal

« back to all changes in this revision

Viewing changes to django/contrib/auth/__init__.py

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant, Eddy Mulyono
  • Date: 2008-09-16 12:18:47 UTC
  • mfrom: (1.1.5 upstream) (4.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080916121847-mg225rg5mnsdqzr0
Tags: 1.0-1ubuntu1
* Merge from Debian (LP: #264191), remaining changes:
  - Run test suite on build.

[Eddy Mulyono]
* Update patch to workaround network test case failures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import datetime
1
2
from django.core.exceptions import ImproperlyConfigured
2
3
 
3
4
SESSION_KEY = '_auth_user_id'
4
5
BACKEND_SESSION_KEY = '_auth_user_backend'
5
 
LOGIN_URL = '/accounts/login/'
6
6
REDIRECT_FIELD_NAME = 'next'
7
7
 
8
8
def load_backend(path):
12
12
        mod = __import__(module, {}, {}, [attr])
13
13
    except ImportError, e:
14
14
        raise ImproperlyConfigured, 'Error importing authentication backend %s: "%s"' % (module, e)
 
15
    except ValueError, e:
 
16
        raise ImproperlyConfigured, 'Error importing authentication backends. Is AUTHENTICATION_BACKENDS a correctly defined list or tuple?'
15
17
    try:
16
18
        cls = getattr(mod, attr)
17
19
    except AttributeError:
49
51
    if user is None:
50
52
        user = request.user
51
53
    # TODO: It would be nice to support different login methods, like signed cookies.
 
54
    user.last_login = datetime.datetime.now()
 
55
    user.save()
 
56
 
 
57
    if SESSION_KEY in request.session:
 
58
        if request.session[SESSION_KEY] != user.id:
 
59
            # To avoid reusing another user's session, create a new, empty
 
60
            # session if the existing session corresponds to a different
 
61
            # authenticated user.
 
62
            request.session.flush()
 
63
    else:
 
64
        request.session.cycle_key()
52
65
    request.session[SESSION_KEY] = user.id
53
66
    request.session[BACKEND_SESSION_KEY] = user.backend
 
67
    if hasattr(request, 'user'):
 
68
        request.user = user
54
69
 
55
70
def logout(request):
56
71
    """
57
 
    Remove the authenticated user's ID from the request.
 
72
    Removes the authenticated user's ID from the request and flushes their
 
73
    session data.
58
74
    """
59
 
    try:
60
 
        del request.session[SESSION_KEY]
61
 
    except KeyError:
62
 
        pass
63
 
    try:
64
 
        del request.session[BACKEND_SESSION_KEY]
65
 
    except KeyError:
66
 
        pass
 
75
    request.session.flush()
 
76
    if hasattr(request, 'user'):
 
77
        from django.contrib.auth.models import AnonymousUser
 
78
        request.user = AnonymousUser()
67
79
 
68
80
def get_user(request):
69
81
    from django.contrib.auth.models import AnonymousUser