Opened 17 years ago

Closed 14 years ago

Last modified 13 years ago

#5612 closed (fixed)

Signals for login / logout

Reported by: Peter van Kampen Owned by: Chris Beaven
Component: Core (Other) Version: dev
Severity: Keywords:
Cc: pterk@…, bthomas@…, siddhartag@…, egmanoj@…, mmitar@…, piquadrat@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

A small patch that adds signals for login / logout events. The signal passes on the request. I use it to clean up session-data.

Attachments (5)

login_logout_signals.diff (1.4 KB ) - added by Peter van Kampen 17 years ago.
login_logout_signals-2.diff (1.4 KB ) - added by Peter van Kampen 17 years ago.
Self contained (moved signals to contrib.auth.signals)
login_logout_signals_r10823.diff (1.6 KB ) - added by JohnDoe 16 years ago.
5612.diff (7.1 KB ) - added by Chris Beaven 15 years ago.
5612.2.diff (7.2 KB ) - added by Chris Beaven 14 years ago.

Download all attachments as: .zip

Change History (32)

by Peter van Kampen, 17 years ago

Attachment: login_logout_signals.diff added

comment:1 by anonymous, 17 years ago

Patch needs improvement: set

I believe the contrib modules are supposed to be self-contained, so should the signal objects should probably be within contrib.auth rather than core.

by Peter van Kampen, 17 years ago

Attachment: login_logout_signals-2.diff added

Self contained (moved signals to contrib.auth.signals)

comment:2 by anonymous, 17 years ago

Cc: pterk@… added

comment:3 by Brian Rosner, 17 years ago

Resolution: wontfix
Status: newclosed

Marking as wont fix. Signal dispatching is slow. This should be accomplished using the daily_cleanup.py found in django/bin. If that doesn't suit you then rewrite parts of django.contrib.auth or maybe through a custom auth backend. Please reopen with better use cases. I just don't see signals being a solution to really any use case for something like this.

comment:4 by anonymous, 17 years ago

Signals are slow, but login and logout events would occur so rarely that signal performance shouldn't be a consideration here.

comment:5 by Peter van Kampen, 17 years ago

Another use case could be to log all login/outs in a single place (iso /admin/, /accounts/registration and possibly other locations or rewriting bits of django core). To alleviate the worries about speed, it could be made entirely optional and off by default.

comment:6 by Brian Rosner, 17 years ago

Resolution: wontfix
Status: closedreopened
Triage Stage: UnreviewedDesign decision needed

Alright, I am going to re-open this ticket to get some exposure by the core devs. There are a few use cases for this and having an optional on and off switch would be nice to alleviate any performance issues.

comment:7 by Pete Crosier, 17 years ago

Has patch: set

comment:8 by Bob Thomas, 16 years ago

Cc: bthomas@… added

by JohnDoe, 16 years ago

comment:9 by JohnDoe, 16 years ago

Updated patch to work with newer version, changed behavior to trigger login signal before updating last_login because i need to log time between logins.

comment:10 by siddhi, 16 years ago

Cc: siddhartag@… added

comment:11 by anonymous, 15 years ago

Cc: egmanoj@… added

comment:12 by imbaczek@…, 15 years ago

another use case: demo users - profiles, passwords and other data needs to be reset on login/logout.

comment:13 by coleifer, 15 years ago

I'd love to see this one - we'd like to hook into the login process and are considering wrapping the default views. What's the status on this one?

comment:14 by Chris Beaven, 15 years ago

Owner: changed from nobody to Chris Beaven
Patch needs improvement: unset
Status: reopenednew

New patch with docs & tests. Also moved the update for last_login to a signal which allows for it to be disconnected if someone wanted to.

by Chris Beaven, 15 years ago

Attachment: 5612.diff added

comment:15 by Chris Beaven, 15 years ago

Summary: [patch] Signals for login / logoutSignals for login / logout

comment:16 by Mitar, 15 years ago

Cc: mmitar@… added

Another use case: with signal you could add a message to the user informing her that she was successfully logged in or out and redirect her to the page she wanted to go in the first place and not to the default login page. It is a nice information to users that they know what is happening behind the scenes.

comment:17 by drizzo4shizzo, 14 years ago

I'd love to see this as in my app I'm currently wrapping the default login view to store login attempts. So preferably a hook for a successful login and another for a failed attempt.

I tried using a custom authentication back end to handle failed login attempts but those only get username/password - need the http request to dig out IP...

comment:18 by Chris Beaven, 14 years ago

mitar: using signals doesn't just make that possible. In fact, it confuses the issue more: what if you have two conflicting responses from signal listeners?

drizzo4shizzo: that's an interesting idea - I don't think it's part of this primary ticket though so perhaps you should open a new ticket for that (and crosslink it to here)

comment:19 by Mitar, 14 years ago

I do not understand why it would not be possible. I am quite happily using such views:

def login(request, *args, **kwargs):
  res = login_view(request, *args, **kwargs)
  if request.user.is_authenticated():
    signals.user_login.send(sender=login, request=request, user=request.user)
  return res
def logout(request, *args, **kwargs):
  user = request.user
  res = logout_view(request, *args, **kwargs)
  signals.user_logout.send(sender=logout_redirect, request=request, user=user)
  return res

comment:20 by Mitar, 14 years ago

And then:

def user_login_message(request, user, **kwargs):
  messages.success(request, _("You have successfully logged in."), fail_silently=True)
user_login.connect(user_login_message)

def user_logout_message(request, user, **kwargs):
  messages.success(request, _("You have successfully logged out."), fail_silently=True)
user_logout.connect(user_logout_message)

comment:21 by Chris Beaven, 14 years ago

Right, I get that adding a message would be a useful use of the signal.

It's was the "redirect her to the page she wanted to go in the first place and not the default login page" statement that I was referring to. It has nothing to do with signal listeners, which is what this ticket was about ;)

comment:22 by Mitar, 14 years ago

Ah, yes. This is something else. I pass next parameter to login view so that user is redirected back to where it started the login process. So I do not use signals for redirecting. But once you do redirecting it is nice to tell user that login was successful, because otherwise just some small part of the page changes (like that username is written somewhere instead of "login" link). So messages are useful to tell the user this. And having signals then makes this really easy to do.

comment:23 by Russell Keith-Magee, 14 years ago

Triage Stage: Design decision neededAccepted

Speed isn't a concern for signals any more, and it's an obvious place to be raising a signal. Accepting.

by Chris Beaven, 14 years ago

Attachment: 5612.2.diff added

comment:24 by anonymous, 14 years ago

Cc: piquadrat@… added

comment:25 by Jannis Leidel, 14 years ago

milestone: 1.3
Triage Stage: AcceptedReady for checkin

comment:26 by Jannis Leidel, 14 years ago

Resolution: fixed
Status: newclosed

(In [14710]) Fixed #5612 -- Added login and logout signals to contrib auth app. Thanks SmileyChris and pterk.

comment:27 by Jacob, 13 years ago

milestone: 1.3

Milestone 1.3 deleted

Note: See TracTickets for help on using tickets.
Back to Top