summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDries2013-06-20 19:02:06 (GMT)
committerDries2013-06-20 19:02:06 (GMT)
commit98b6331e1e61608cf00c760d0fa10c90a93ecc71 (patch)
treed3574f682a6648d74aa45b0edf45b641ff5d16a1
parentab87217af435644ed3cfdd5cab66132fa327e035 (diff)
Issue #1984610 by Albert Volkman, dawehner, tim.plunkett: Convert user_pass() to a new-style Form object.
-rw-r--r--core/modules/user/lib/Drupal/user/Form/UserPasswordForm.php145
-rw-r--r--core/modules/user/lib/Drupal/user/Tests/UserAccountLinksTests.php4
-rw-r--r--core/modules/user/user.module5
-rw-r--r--core/modules/user/user.pages.inc77
-rw-r--r--core/modules/user/user.routing.yml7
5 files changed, 153 insertions, 85 deletions
diff --git a/core/modules/user/lib/Drupal/user/Form/UserPasswordForm.php b/core/modules/user/lib/Drupal/user/Form/UserPasswordForm.php
new file mode 100644
index 0000000..47fc23b
--- /dev/null
+++ b/core/modules/user/lib/Drupal/user/Form/UserPasswordForm.php
@@ -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';
+ }
+
+}
diff --git a/core/modules/user/lib/Drupal/user/Tests/UserAccountLinksTests.php b/core/modules/user/lib/Drupal/user/Tests/UserAccountLinksTests.php
index deec20e..9d714a0 100644
--- a/core/modules/user/lib/Drupal/user/Tests/UserAccountLinksTests.php
+++ b/core/modules/user/lib/Drupal/user/Tests/UserAccountLinksTests.php
@@ -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.
diff --git a/core/modules/user/user.module b/core/modules/user/user.module
index bf0b052..e7ee37a 100644
--- a/core/modules/user/user.module
+++ b/core/modules/user/user.module
@@ -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',
diff --git a/core/modules/user/user.pages.inc b/core/modules/user/user.pages.inc
index a8559d0..5025c17 100644
--- a/core/modules/user/user.pages.inc
+++ b/core/modules/user/user.pages.inc
@@ -5,8 +5,6 @@
* 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;
@@ -14,81 +12,6 @@ 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.
*/
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 a4c5983..3d52545 100644
--- a/core/modules/user/user.routing.yml
+++ b/core/modules/user/user.routing.yml
@@ -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'