Code

Ticket #5612: login_logout_signals_r10823.diff

File login_logout_signals_r10823.diff, 1.6 KB (added by JohnDoe, 5 years ago)
Line 
1Index: django/contrib/auth/__init__.py
2===================================================================
3--- django/contrib/auth/__init__.py     (revision 10821)
4+++ django/contrib/auth/__init__.py     (working copy)
5@@ -1,6 +1,7 @@
6 import datetime
7 from django.core.exceptions import ImproperlyConfigured
8 from django.utils.importlib import import_module
9+from django.contrib.auth import signals
10 
11 SESSION_KEY = '_auth_user_id'
12 BACKEND_SESSION_KEY = '_auth_user_backend'
13@@ -52,6 +53,7 @@
14     if user is None:
15         user = request.user
16     # TODO: It would be nice to support different login methods, like signed cookies.
17+    signals.user_logged_in.send(sender=None, request=request, user=user)
18     user.last_login = datetime.datetime.now()
19     user.save()
20 
21@@ -75,6 +77,9 @@
22     """
23     request.session.flush()
24     if hasattr(request, 'user'):
25+        # Dispatch the signal before it is reset to AnonymousUser so
26+        # the receivers have a chance to find out *who* logged out
27+        signals.user_logged_out.send(sender=None, request=request)
28         from django.contrib.auth.models import AnonymousUser
29         request.user = AnonymousUser()
30 
31Index: django/contrib/auth/signals.py
32===================================================================
33--- django/contrib/auth/signals.py      (revision 0)
34+++ django/contrib/auth/signals.py      (revision 0)
35@@ -0,0 +1,4 @@
36+import django.dispatch
37+
38+user_logged_in = django.dispatch.Signal(providing_args=['request', 'user'])
39+user_logged_out = django.dispatch.Signal(providing_args=['request',])
40\ No newline at end of file