diff --git a/core/modules/user/lib/Drupal/user/UserPasswordForm.php b/core/modules/user/lib/Drupal/user/UserPasswordForm.php new file mode 100644 index 0000000..25a87a3 --- /dev/null +++ b/core/modules/user/lib/Drupal/user/UserPasswordForm.php @@ -0,0 +1,102 @@ + 'textfield', + '#title' => t('Username or e-mail address'), + '#size' => 60, + '#maxlength' => max(USERNAME_MAX_LENGTH, EMAIL_MAX_LENGTH), + '#required' => TRUE, + ); + // Allow logged in users to request this also. + if ($user->uid > 0) { + $form['name']['#type'] = 'value'; + $form['name']['#value'] = $user->mail; + $form['mail'] = array( + '#prefix' => '

', + '#markup' => t('Password reset instructions will be mailed to %email. You must log out to use the password reset link in the e-mail.', array('%email' => $user->mail)), + '#suffix' => '

', + ); + } + else { + $form['name']['#default_value'] = $request->query->get('name'); + } + $form['actions'] = array('#type' => 'actions'); + $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('E-mail new password')); + + return $form; + } + + /** + * Implements \Drupal\Core\Form\FormInterface::validateForm(). + */ + public function validateForm(array &$form, array &$form_state) { + $name = trim($form_state['values']['name']); + // Try to load by email. + $users = entity_load_multiple_by_properties('user', array('mail' => $name, 'status' => '1')); + $account = reset($users); + if (!$account) { + // No success, try to load by name. + $users = entity_load_multiple_by_properties('user', array('name' => $name, 'status' => '1')); + $account = reset($users); + } + if (isset($account->uid)) { + form_set_value(array('#parents' => array('account')), $account, $form_state); + } + else { + form_set_error('name', t('Sorry, %name is not recognized as a username or an e-mail address.', array('%name' => $name))); + } + } + + /** + * Implements \Drupal\Core\Form\FormInterface::submitForm(). + */ + public function submitForm(array &$form, array &$form_state) { + $language_interface = language(LANGUAGE_TYPE_INTERFACE); + + $account = $form_state['values']['account']; + // Mail one time login URL and instructions using current language. + $mail = _user_mail_notify('password_reset', $account, $language_interface->langcode); + if (!empty($mail)) { + watchdog('user', 'Password reset instructions mailed to %name at %email.', array('%name' => $account->name, '%email' => $account->mail)); + drupal_set_message(t('Further instructions have been sent to your e-mail address.')); + } + + $form_state['redirect'] = 'user'; + } +} diff --git a/core/modules/user/user.module b/core/modules/user/user.module index 3ae6154..627c6c4 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -903,12 +903,10 @@ function user_menu() { $items['user/password'] = array( 'title' => 'Request new password', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('user_pass'), - 'access callback' => TRUE, 'type' => MENU_LOCAL_TASK, - 'file' => 'user.pages.inc', + 'route_name' => 'user_pass', ); + $items['user/reset/%/%/%'] = array( 'title' => 'Reset password', 'page callback' => 'drupal_get_form', diff --git a/core/modules/user/user.pages.inc b/core/modules/user/user.pages.inc index 5f0cfab..f74f9a3 100644 --- a/core/modules/user/user.pages.inc +++ b/core/modules/user/user.pages.inc @@ -13,75 +13,6 @@ use Drupal\Component\Utility\Crypt; /** - * Form builder; Request a password reset. - * - * @ingroup forms - * @see user_pass_validate() - * @see user_pass_submit() - */ -function user_pass() { - global $user; - - $form['name'] = array( - '#type' => 'textfield', - '#title' => t('Username or e-mail address'), - '#size' => 60, - '#maxlength' => max(USERNAME_MAX_LENGTH, EMAIL_MAX_LENGTH), - '#required' => TRUE, - ); - // Allow logged in users to request this also. - if ($user->uid > 0) { - $form['name']['#type'] = 'value'; - $form['name']['#value'] = $user->mail; - $form['mail'] = array( - '#prefix' => '

', - '#markup' => t('Password reset instructions will be mailed to %email. You must log out to use the password reset link in the e-mail.', array('%email' => $user->mail)), - '#suffix' => '

', - ); - } - else { - $form['name']['#default_value'] = Drupal::Request()->query->get('name'); - } - $form['actions'] = array('#type' => 'actions'); - $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('E-mail new password')); - - return $form; -} - -function user_pass_validate($form, &$form_state) { - $name = trim($form_state['values']['name']); - // Try to load by email. - $users = entity_load_multiple_by_properties('user', array('mail' => $name, 'status' => '1')); - $account = reset($users); - if (!$account) { - // No success, try to load by name. - $users = entity_load_multiple_by_properties('user', array('name' => $name, 'status' => '1')); - $account = reset($users); - } - if (isset($account->uid)) { - form_set_value(array('#parents' => array('account')), $account, $form_state); - } - else { - form_set_error('name', t('Sorry, %name is not recognized as a username or an e-mail address.', array('%name' => $name))); - } -} - -function user_pass_submit($form, &$form_state) { - $language_interface = language(LANGUAGE_TYPE_INTERFACE); - - $account = $form_state['values']['account']; - // Mail one time login URL and instructions using current language. - $mail = _user_mail_notify('password_reset', $account, $language_interface->langcode); - if (!empty($mail)) { - watchdog('user', 'Password reset instructions mailed to %name at %email.', array('%name' => $account->name, '%email' => $account->mail)); - drupal_set_message(t('Further instructions have been sent to your e-mail address.')); - } - - $form_state['redirect'] = 'user'; - return; -} - -/** * Menu callback; process one time login link and redirects to the user page on success. */ function user_pass_reset($form, &$form_state, $uid, $timestamp, $hashed_pass, $action = NULL) { diff --git a/core/modules/user/user.routing.yml b/core/modules/user/user.routing.yml index 0798e1d..64fc64e 100644 --- a/core/modules/user/user.routing.yml +++ b/core/modules/user/user.routing.yml @@ -25,3 +25,10 @@ user_account_settings: _form: '\Drupal\user\AccountSettingsForm' requirements: _permission: 'administer users' + +user_pass: + pattern: 'user/password' + defaults: + _form: '\Drupal\user\UserPasswordForm' + requirements: + _access: 'TRUE'