Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Trigger a django user password change...
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Jens Diemer  
View profile  
 More options Aug 2 2007, 5:20 am
From: Jens Diemer <python_gm...@jensdiemer.de>
Date: Thu, 02 Aug 2007 11:20:16 +0200
Local: Thurs, Aug 2 2007 5:20 am
Subject: Trigger a django user password change...

I would like to do something if the django user password has been set or update.
So i trigger signals.post_save with the User class, like this:

=========================================================================== ====

from django.db.models import signals
from django.dispatch import dispatcher

def update(sender, instance, signal, *args, **kwargs):

     user_obj = instance

     ...

     user_obj.message_set.create(message="Updated!")

dispatcher.connect(update, signal=signals.post_save, sender=User)

=========================================================================== ====

But my function 'update' is not only called if the user password changed.
The problem is, in the User model exists e.g. 'last_login'. So the save method
called every time, the user logged in :(

One idea is this:

=========================================================================== ====

old_passwords = {}
def save_old_pass(sender, instance, signal, *args, **kwargs):
     user_obj = instance
     old_pass = user_obj.password
     old_passwords[user_obj] = old_pass

def update(sender, instance, signal, *args, **kwargs):
     user_obj = instance
     new_password = user_obj.password

     if user_obj in old_passwords and old_passwords[user_obj] == new_password:
         # Nothing to change
         return

     ...

     user_obj.message_set.create(message="Updated!")

from django.db.models import signals
from django.dispatch import dispatcher

dispatcher.connect(save_old_pass, signal=signals.post_init, sender=User)
dispatcher.connect(update, signal=signals.post_save, sender=User)

=========================================================================== ====

This works, but save_old_pass() would be often called, if the user is logged in.
So it's not a really good idea.

Any better ideas?

--
Mfg.

Jens Diemer

----
A django powered CMS: http://www.pylucid.org


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
RajeshD  
View profile  
 More options Aug 2 2007, 4:37 pm
From: RajeshD <rajesh.dha...@gmail.com>
Date: Thu, 02 Aug 2007 20:37:02 -0000
Local: Thurs, Aug 2 2007 4:37 pm
Subject: Re: Trigger a django user password change...
<snip>

It's also not a good idea to end up caching all your users' passwords
in the old_passwords dictionary like that.

> Any better ideas?

Create a custom view to allow your users to change just their
password. In that view, you will be able to tell in a straightforward
fashion if the user has changed her password and take action
accordingly.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
RajeshD  
View profile  
 More options Aug 2 2007, 4:43 pm
From: RajeshD <rajesh.dha...@gmail.com>
Date: Thu, 02 Aug 2007 20:43:17 -0000
Local: Thurs, Aug 2 2007 4:43 pm
Subject: Re: Trigger a django user password change...

> Any better ideas?

You could also use the pre_save signal in your first solution (instead
of post_save). That will give you the new password before it's saved
to the DB. So, you can pull up that user's DB User object and compare
the two passwords. Of course, and send a message to the user. Of
course, your message may be misleading if the subsequent save of the
current instance actually fails for any reason.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jens Diemer  
View profile  
 More options Aug 8 2007, 2:46 am
From: Jens Diemer <python_gm...@jensdiemer.de>
Date: Wed, 08 Aug 2007 08:46:15 +0200
Local: Wed, Aug 8 2007 2:46 am
Subject: Re: Trigger a django user password change...
RajeshD schrieb:

> Create a custom view to allow your users to change just their
> password. In that view, you will be able to tell in a straightforward
> fashion if the user has changed her password and take action
> accordingly.

Yes. That would work fine.
Now, i realized that i need the raw_password for my things. With signals i can
only get the hashed password, not the raw plaintext password.

On the other side, wit signals i can catch every changes from every views. So i
must build my own views and the user can used the default django views to change
his password.

Is there is an other way to trigger a django user password change and get the
raw password???

--
Mfg.

Jens Diemer

----
A django powered CMS: http://www.pylucid.org


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jens Diemer  
View profile  
 More options Aug 31 2007, 3:25 am
From: Jens Diemer <python_gm...@jensdiemer.de>
Date: Fri, 31 Aug 2007 09:25:35 +0200
Local: Fri, Aug 31 2007 3:25 am
Subject: Re: Trigger a django user password change...
Jens Diemer schrieb:

I found a simple way to trigger a user password change. I hacked directly into
the django.contrib.auth.models.User.set_password() method.

It looks like this:

=========================================================================== ====

from django.contrib.auth.models import User

# Save the original method
old_set_password = User.set_password

def set_password(user, raw_password):
     if user.id == None:
         # It's a new user. We must save the django user account first.
         user.save()

     #
     # Do something with the user obejct and the given raw_password ;)
     #

     # Use the original method to set the django User password:
     old_set_password(user, raw_password)

# Replace the method
User.set_password = set_password

=========================================================================== ====

So every normal password change (e.g. from the django admin panel) are caught
and i can access to the raw plaintext password.

I added a snippets here: http://www.djangosnippets.org/snippets/397/

--
Mfg.

Jens Diemer

----
A django powered CMS: http://www.pylucid.org


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »