2

I don't like the one time login process; sending the users directly to their user edit page is not what I want. I want to force the users to insert a new password.

I have created a module that doesn't work; obviously I'm not affecting the submit action and is not taking the newly inserted username to the user object.

// Let's try to add a form in the user pass reset form for password requests.
function xxx_login_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'user_pass_reset') {
    $form['#account'] = $GLOBALS['user'];
    $form['loginsetup'] = array(
      '#title' => t('Login setup'),
      '#type' => 'fieldset',
      '#collapsible' => FALSE,
      '#collapsed' => FALSE
    );
    $form['loginsetup']['account']['name'] = array(
      '#type' => 'textfield',
      '#size' => 25,
      '#title' => t('Username'),
      '#required' => TRUE
    );  

    $form['loginsetup']['account']['pass'] = array(
      '#type' => 'password_confirm',
      '#size' => 25,
      '#title' => 'Please enter your new password',
      '#required' => TRUE,
    );

    $form['message']['#markup'] = FALSE;
    $form['help']['#markup'] = FALSE;
    $form['actions']['submit']['#value'] = 'Enter';
    //  $form['#validate'][] = 'xxx_login_user_pass_validate';
    $form['#submit'][] = 'xxx_login_user_pass_submit';
  }

  return $form;
}

// We'll check if the user did not leave a username.

// function xxx_login_user_pass_validate($form, &$form_state) {
//   if (empty($form_state['values']['name'])) {
//     form_set_error('name', t('You must select a username.'));
//   }
// }

// We should send the new values to the user object.

 function xxx_login_user_pass_submit($form, &$form_state) {
   // Set up the edit array to pass to user_save().
   $edit = array(
     'pass' => $form_state['values']['pass'],
     'name' => $form_state['values']['name']
   );

   // Save the account with the new password and name
   user_save($form['#account'], $edit);

   // Inform the user.
   drupal_set_message(t('Your login details have been stored. Welcome to example.com!'));
}

The validate function is now commented, but it does not affect the module; with or without it, it still does not work.

2 Answers 2

2

This is a bit of a SNAFU as user_password_reset doesn't have a hook associated with it. Why that is, I fail to see.

However, you can always use this little trick in hook_user_login to determine when users log in with a reset link by checking the emptiness of the $edit parameter as the only time it will be empty will be when the user didn't submit a form to log in = when they were automatically logged in.

function mymodule_user_login(&$edit, $account){
  if(!$edit){ // only for automatic pwd resets
    // do your thing, redirect to a password change form with drupal_goto or whatever you need.
  }
}

The implementation of forcing the user set a new password is another question altogether. We do it with a modal window that shows a simple custom form, but there are plenty of ways to do it. Unfortunately the easiest one is sending the user to their user edit form like the password reset functionality automatically does, but there's absolutely no reason why you couldn't just do whatever at that point as the user is logged in and good to go.

0

You can intercept the password reset with a hook_form_user_pass_reset_alter function.

When the user clicks the button to login, they can be taken to your alternative page.

Something like this works:

e.g:

/**
 * Implementation of hook_form_FORM_ID_alter()
 */
function MY_MODULE_form_user_pass_reset_alter(&$form, &$form_state, $form_id) {

  $uid = $form_state['build_info']['args'][0];
  $timestamp = $form_state['build_info']['args'][1];
  $hashed_pass = $form_state['build_info']['args'][2];

  // Go to our user/[uid]/edit/password page for resetting password
  $url = url("user/reset/$uid/$timestamp/$hashed_pass/login", array(
    'query' => array(
      'destination' => "user/$uid/edit/password"
    )
  ));

  $form['#action'] = $url;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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