ea224fe439e9859bf6765530e953efebcb83ad44
[yaffs-website] / web / core / modules / user / src / Form / UserPasswordForm.php
1 <?php
2
3 namespace Drupal\user\Form;
4
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Language\LanguageManagerInterface;
8 use Drupal\Core\Render\Element\Email;
9 use Drupal\user\UserStorageInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Provides a user password reset form.
14  */
15 class UserPasswordForm extends FormBase {
16
17   /**
18    * The user storage.
19    *
20    * @var \Drupal\user\UserStorageInterface
21    */
22   protected $userStorage;
23
24   /**
25    * The language manager.
26    *
27    * @var \Drupal\Core\Language\LanguageManagerInterface
28    */
29   protected $languageManager;
30
31   /**
32    * Constructs a UserPasswordForm object.
33    *
34    * @param \Drupal\user\UserStorageInterface $user_storage
35    *   The user storage.
36    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
37    *   The language manager.
38    */
39   public function __construct(UserStorageInterface $user_storage, LanguageManagerInterface $language_manager) {
40     $this->userStorage = $user_storage;
41     $this->languageManager = $language_manager;
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public static function create(ContainerInterface $container) {
48     return new static(
49       $container->get('entity.manager')->getStorage('user'),
50       $container->get('language_manager')
51     );
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public function getFormId() {
58     return 'user_pass';
59   }
60
61   /**
62    * {@inheritdoc}
63    *
64    * @param \Symfony\Component\HttpFoundation\Request $request
65    *   The request object.
66    */
67   public function buildForm(array $form, FormStateInterface $form_state) {
68     $form['name'] = [
69       '#type' => 'textfield',
70       '#title' => $this->t('Username or email address'),
71       '#size' => 60,
72       '#maxlength' => max(USERNAME_MAX_LENGTH, Email::EMAIL_MAX_LENGTH),
73       '#required' => TRUE,
74       '#attributes' => [
75         'autocorrect' => 'off',
76         'autocapitalize' => 'off',
77         'spellcheck' => 'false',
78         'autofocus' => 'autofocus',
79       ],
80     ];
81     // Allow logged in users to request this also.
82     $user = $this->currentUser();
83     if ($user->isAuthenticated()) {
84       $form['name']['#type'] = 'value';
85       $form['name']['#value'] = $user->getEmail();
86       $form['mail'] = [
87         '#prefix' => '<p>',
88         '#markup' => $this->t('Password reset instructions will be mailed to %email. You must log out to use the password reset link in the email.', ['%email' => $user->getEmail()]),
89         '#suffix' => '</p>',
90       ];
91     }
92     else {
93       $form['mail'] = [
94         '#prefix' => '<p>',
95         '#markup' => $this->t('Password reset instructions will be sent to your registered email address.'),
96         '#suffix' => '</p>',
97       ];
98       $form['name']['#default_value'] = $this->getRequest()->query->get('name');
99     }
100     $form['actions'] = ['#type' => 'actions'];
101     $form['actions']['submit'] = ['#type' => 'submit', '#value' => $this->t('Submit')];
102     $form['#cache']['contexts'][] = 'url.query_args';
103
104     return $form;
105   }
106
107   /**
108    * {@inheritdoc}
109    */
110   public function validateForm(array &$form, FormStateInterface $form_state) {
111     $name = trim($form_state->getValue('name'));
112     // Try to load by email.
113     $users = $this->userStorage->loadByProperties(['mail' => $name]);
114     if (empty($users)) {
115       // No success, try to load by name.
116       $users = $this->userStorage->loadByProperties(['name' => $name]);
117     }
118     $account = reset($users);
119     if ($account && $account->id()) {
120       // Blocked accounts cannot request a new password.
121       if (!$account->isActive()) {
122         $form_state->setErrorByName('name', $this->t('%name is blocked or has not been activated yet.', ['%name' => $name]));
123       }
124       else {
125         $form_state->setValueForElement(['#parents' => ['account']], $account);
126       }
127     }
128     else {
129       $form_state->setErrorByName('name', $this->t('%name is not recognized as a username or an email address.', ['%name' => $name]));
130     }
131   }
132
133   /**
134    * {@inheritdoc}
135    */
136   public function submitForm(array &$form, FormStateInterface $form_state) {
137     $langcode = $this->languageManager->getCurrentLanguage()->getId();
138
139     $account = $form_state->getValue('account');
140     // Mail one time login URL and instructions using current language.
141     $mail = _user_mail_notify('password_reset', $account, $langcode);
142     if (!empty($mail)) {
143       $this->logger('user')->notice('Password reset instructions mailed to %name at %email.', ['%name' => $account->getUsername(), '%email' => $account->getEmail()]);
144       drupal_set_message($this->t('Further instructions have been sent to your email address.'));
145     }
146
147     $form_state->setRedirect('user.page');
148   }
149
150 }