10

I wanted to send basic notification email to the user, after it has changed the password, to inform him of password change.

I am using Rules modules for that. But I am not finding any option there, to add condition if user has changed the password.

Is there any way I can check if user has changed his password using rules or pragmatically.

-- Many thanks for your time.

7

The easiest would be to do this using Rules. Following the next steps, should do the job.

  1. Ensure you have enabled the "PHP filter" on the modules page. This is needed to be able to use PHP code inside Rules.
  2. Create a rule that reacts on the user event "After saving a new user account".
  3. Add a condition of type "Execute custom PHP code" and use the following snippet:
    return $account_unchanged->pass != $account->pass;
  4. Add your action(s) to send an email notification to the user or anything else that fits your purpose.
  • have you tried it? – Aboodred1 Apr 1 '13 at 15:26
  • Yes, I tested this out just before I wrote the answer. – ovi Apr 1 '13 at 15:28
  • Thanks, simple and efficient solution. Works like charm!! – Kalpesh Patel Apr 3 '13 at 5:30
4

I haven't tried it in Rules before, but it would probably be a matter of comparing values on the $account and $account->original objects.

One way to do it in custom code is to implement hook_user_presave():

function MYMODULE_user_presave(&$edit, $account, $category) {
  // Don't show message for registering users.
  if (isset($account->is_new) && $account->is_new) {
    return;
  }

  if (!empty($edit['pass']) && $account->pass != $edit['pass']) {
    // Password has been changed.
  }
}

You could probably do the same in hook_user_update(), but I haven't tried that before. The above is taken from a live project and works perfectly.

1

Using Rules might be easier since you are already using it, but you need to enable PHP Filter module.

The instructions as follow:

  1. Enable PHP Filter module admin/modules
  2. Create a new Rule admin/config/workflow/rules
  3. Make sure to select After updating an existing user account from React on event dropdow list.
  4. Add a condition and choose Execute custom PHP code
  5. Add the following PHP custom code in the value section

    if(!empty($account->current_pass)) { return true; }

  6. Then add an action and select Send mail

  7. In the value section add [account:mail] and fill subject field and message field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.