diff --git a/password_policy.module b/password_policy.module index 5525431..69b16b4 100644 --- a/password_policy.module +++ b/password_policy.module @@ -511,6 +511,51 @@ function password_policy_form_alter(&$form, $form_state, $form_id) { } /** + * Implements hook_form_FORM_ID_alter() for user_pass(). + * + * Changes the submit button to automatically log the user out when forcing a + * password change by e-mail. This is so the user can use the password reset + * link without manually logging out. + */ +function password_policy_form_user_pass_alter(&$form, &$form_state) { + global $user; + + if (_password_policy_forcing_change_by_mail() + && _password_policy_is_password_change_forced($user->uid)) { + // Remove sentence about logging out. + $form['mail']['#markup'] = t('Password reset instructions will be mailed to %email.', array('%email' => $user->mail)); + // Add submission handler to log user out. + $form['#submit'][] = 'password_policy_user_pass_submit'; + } +} + +/** + * Form submission handler for user_pass(). + */ +function password_policy_user_pass_submit($form, &$form_state) { + _password_policy_user_pass_logout(); + drupal_set_message(t('Further instructions have been sent to your e-mail address.')); +} + +/** + * Logs the user out from the user_pass() form without redirection. + * + * This function is a duplicate of user_logout() without the drupal_goto(). + * + * @see user_logout() + */ +function _password_policy_user_pass_logout() { + global $user; + + watchdog('user', 'Session closed for %name.', array('%name' => $user->name)); + + module_invoke_all('user_logout', $user); + + // Destroy the current session, and reset $user to the anonymous user. + session_destroy(); +} + +/** * Implements hook_cron(). */ function password_policy_cron() { diff --git a/tests/password_policy.test b/tests/password_policy.test index 9ea91d2..a756b9a 100644 --- a/tests/password_policy.test +++ b/tests/password_policy.test @@ -692,7 +692,6 @@ class PasswordPolicyForcePasswordChangeTestCase extends DrupalWebTestCase { $this->assertRaw(t('Your password has expired. You must change your password to proceed on the site.'), t('User presented with error instructing them to change their password.')); // Press E-mail new password button then log out. $this->drupalPost(NULL, array(), t('E-mail new password')); - $this->drupalLogout(); // Visit reset URL. $reset_url = $this->getPasswordResetURLFromMail();