Security update for Core, with self-updated composer
[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   public function buildForm(array $form, FormStateInterface $form_state) {
65     $form['name'] = [
66       '#type' => 'textfield',
67       '#title' => $this->t('Username or email address'),
68       '#size' => 60,
69       '#maxlength' => max(USERNAME_MAX_LENGTH, Email::EMAIL_MAX_LENGTH),
70       '#required' => TRUE,
71       '#attributes' => [
72         'autocorrect' => 'off',
73         'autocapitalize' => 'off',
74         'spellcheck' => 'false',
75         'autofocus' => 'autofocus',
76       ],
77     ];
78     // Allow logged in users to request this also.
79     $user = $this->currentUser();
80     if ($user->isAuthenticated()) {
81       $form['name']['#type'] = 'value';
82       $form['name']['#value'] = $user->getEmail();
83       $form['mail'] = [
84         '#prefix' => '<p>',
85         '#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()]),
86         '#suffix' => '</p>',
87       ];
88     }
89     else {
90       $form['mail'] = [
91         '#prefix' => '<p>',
92         '#markup' => $this->t('Password reset instructions will be sent to your registered email address.'),
93         '#suffix' => '</p>',
94       ];
95       $form['name']['#default_value'] = $this->getRequest()->query->get('name');
96     }
97     $form['actions'] = ['#type' => 'actions'];
98     $form['actions']['submit'] = ['#type' => 'submit', '#value' => $this->t('Submit')];
99     $form['#cache']['contexts'][] = 'url.query_args';
100
101     return $form;
102   }
103
104   /**
105    * {@inheritdoc}
106    */
107   public function validateForm(array &$form, FormStateInterface $form_state) {
108     $name = trim($form_state->getValue('name'));
109     // Try to load by email.
110     $users = $this->userStorage->loadByProperties(['mail' => $name]);
111     if (empty($users)) {
112       // No success, try to load by name.
113       $users = $this->userStorage->loadByProperties(['name' => $name]);
114     }
115     $account = reset($users);
116     if ($account && $account->id()) {
117       // Blocked accounts cannot request a new password.
118       if (!$account->isActive()) {
119         $form_state->setErrorByName('name', $this->t('%name is blocked or has not been activated yet.', ['%name' => $name]));
120       }
121       else {
122         $form_state->setValueForElement(['#parents' => ['account']], $account);
123       }
124     }
125     else {
126       $form_state->setErrorByName('name', $this->t('%name is not recognized as a username or an email address.', ['%name' => $name]));
127     }
128   }
129
130   /**
131    * {@inheritdoc}
132    */
133   public function submitForm(array &$form, FormStateInterface $form_state) {
134     $langcode = $this->languageManager->getCurrentLanguage()->getId();
135
136     $account = $form_state->getValue('account');
137     // Mail one time login URL and instructions using current language.
138     $mail = _user_mail_notify('password_reset', $account, $langcode);
139     if (!empty($mail)) {
140       $this->logger('user')->notice('Password reset instructions mailed to %name at %email.', ['%name' => $account->getUsername(), '%email' => $account->getEmail()]);
141       drupal_set_message($this->t('Further instructions have been sent to your email address.'));
142     }
143
144     $form_state->setRedirect('user.page');
145   }
146
147 }