Issue #1984610 by Albert Volkman, dawehner, tim.plunkett: Convert user_pass() to a new-style Form object.

8.0.x
Dries 2013-06-20 15:02:06 -04:00
parent ab87217af4
commit 98b6331e1e
5 changed files with 153 additions and 85 deletions

View File

@ -0,0 +1,145 @@
<?php
/**
* @file
* Contains \Drupal\user\Form\UserPasswordForm.
*/
namespace Drupal\user\Form;
use Drupal\Core\Controller\ControllerInterface;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageManager;
use Drupal\user\UserStorageControllerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a user password reset form.
*/
class UserPasswordForm implements FormInterface, ControllerInterface {
/**
* The user storage controller.
*
* @var \Drupal\user\UserStorageControllerInterface
*/
protected $userStorageController;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManager
*/
protected $languageManager;
/**
* Constructs a UserPasswordForm object.
*
* @param \Drupal\user\UserStorageControllerInterface $user_storage_controller
* The user storage controller.
* @param \Drupal\Core\Language\LanguageManager $language_manager
* The language manager.
*/
public function __construct(UserStorageControllerInterface $user_storage_controller, LanguageManager $language_manager) {
$this->userStorageController = $user_storage_controller;
$this->languageManager = $language_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('plugin.manager.entity')->getStorageController('user'),
$container->get('language_manager')
);
}
/**
* {@inheritdoc}
*/
public function getFormID() {
return 'user_pass';
}
/**
* {@inheritdoc}
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request object.
*/
public function buildForm(array $form, array &$form_state, Request $request = NULL) {
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,
'#attributes' => array(
'autocorrect' => 'off',
'autocapitalize' => 'off',
'spellcheck' => 'false',
'autofocus' => 'autofocus',
),
);
// 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' => '<p>',
'#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' => '</p>',
);
}
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;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {
$name = trim($form_state['values']['name']);
// Try to load by email.
$users = $this->userStorageController->loadByProperties(array('mail' => $name, 'status' => '1'));
if (empty($users)) {
// No success, try to load by name.
$users = $this->userStorageController->loadByProperties(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)));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
$langcode = $this->languageManager->getLanguage(Language::TYPE_INTERFACE)->langcode;
$account = $form_state['values']['account'];
// Mail one time login URL and instructions using current language.
$mail = _user_mail_notify('password_reset', $account->getBCEntity(), $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';
}
}

View File

@ -117,10 +117,6 @@ class UserAccountLinksTests extends WebTestBase {
$this->drupalGet('user/password');
$this->assertTitle('Request new password' . $title_suffix, "Page title of /user/register is 'Request new password' for anonymous users.");
// Tests the default fallback title.
$this->drupalGet('user/password/' . $this->randomName());
$this->assertTitle('User account' . $title_suffix, "Fallback page title for user pages is 'User account' for anonymous users.");
// Check the page title for registered users is "My Account" in menus.
$this->drupalLogin($this->drupalCreateUser());
// After login, the client is redirected to /user.

View File

@ -881,11 +881,8 @@ function user_menu() {
$items['user/password'] = array(
'title' => 'Request new password',
'page callback' => 'drupal_get_form',
'page arguments' => array('user_pass'),
'access callback' => TRUE,
'route_name' => 'user_pass',
'type' => MENU_LOCAL_TASK,
'file' => 'user.pages.inc',
);
$items['user/reset/%/%/%'] = array(
'title' => 'Reset password',

View File

@ -5,89 +5,12 @@
* User page callback file for the user module.
*/
use Drupal\Core\Language\Language;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\HttpKernelInterface;
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,
'#attributes' => array(
'autocorrect' => 'off',
'autocapitalize' => 'off',
'spellcheck' => 'false',
'autofocus' => 'autofocus',
),
);
// 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' => '<p>',
'#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' => '</p>',
);
}
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->getBCEntity(), $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.
*/

View File

@ -67,3 +67,10 @@ user_role_delete:
_entity_form: user_role.delete
requirements:
_entity_access: user_role.delete
user_pass:
pattern: '/user/password'
defaults:
_form: '\Drupal\user\Form\UserPasswordForm'
requirements:
_access: 'TRUE'