Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\user\Controller;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Utility\Crypt;
|
Chris@0
|
6 use Drupal\Component\Utility\Xss;
|
Chris@0
|
7 use Drupal\Core\Controller\ControllerBase;
|
Chris@0
|
8 use Drupal\Core\Datetime\DateFormatterInterface;
|
Chris@0
|
9 use Drupal\user\Form\UserPasswordResetForm;
|
Chris@0
|
10 use Drupal\user\UserDataInterface;
|
Chris@0
|
11 use Drupal\user\UserInterface;
|
Chris@0
|
12 use Drupal\user\UserStorageInterface;
|
Chris@0
|
13 use Psr\Log\LoggerInterface;
|
Chris@0
|
14 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
15 use Symfony\Component\HttpFoundation\Request;
|
Chris@0
|
16 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * Controller routines for user routes.
|
Chris@0
|
20 */
|
Chris@0
|
21 class UserController extends ControllerBase {
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * The date formatter service.
|
Chris@0
|
25 *
|
Chris@0
|
26 * @var \Drupal\Core\Datetime\DateFormatterInterface
|
Chris@0
|
27 */
|
Chris@0
|
28 protected $dateFormatter;
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * The user storage.
|
Chris@0
|
32 *
|
Chris@0
|
33 * @var \Drupal\user\UserStorageInterface
|
Chris@0
|
34 */
|
Chris@0
|
35 protected $userStorage;
|
Chris@0
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * The user data service.
|
Chris@0
|
39 *
|
Chris@0
|
40 * @var \Drupal\user\UserDataInterface
|
Chris@0
|
41 */
|
Chris@0
|
42 protected $userData;
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@0
|
45 * A logger instance.
|
Chris@0
|
46 *
|
Chris@0
|
47 * @var \Psr\Log\LoggerInterface
|
Chris@0
|
48 */
|
Chris@0
|
49 protected $logger;
|
Chris@0
|
50
|
Chris@0
|
51 /**
|
Chris@0
|
52 * Constructs a UserController object.
|
Chris@0
|
53 *
|
Chris@0
|
54 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
|
Chris@0
|
55 * The date formatter service.
|
Chris@0
|
56 * @param \Drupal\user\UserStorageInterface $user_storage
|
Chris@0
|
57 * The user storage.
|
Chris@0
|
58 * @param \Drupal\user\UserDataInterface $user_data
|
Chris@0
|
59 * The user data service.
|
Chris@0
|
60 * @param \Psr\Log\LoggerInterface $logger
|
Chris@0
|
61 * A logger instance.
|
Chris@0
|
62 */
|
Chris@0
|
63 public function __construct(DateFormatterInterface $date_formatter, UserStorageInterface $user_storage, UserDataInterface $user_data, LoggerInterface $logger) {
|
Chris@0
|
64 $this->dateFormatter = $date_formatter;
|
Chris@0
|
65 $this->userStorage = $user_storage;
|
Chris@0
|
66 $this->userData = $user_data;
|
Chris@0
|
67 $this->logger = $logger;
|
Chris@0
|
68 }
|
Chris@0
|
69
|
Chris@0
|
70 /**
|
Chris@0
|
71 * {@inheritdoc}
|
Chris@0
|
72 */
|
Chris@0
|
73 public static function create(ContainerInterface $container) {
|
Chris@0
|
74 return new static(
|
Chris@0
|
75 $container->get('date.formatter'),
|
Chris@0
|
76 $container->get('entity.manager')->getStorage('user'),
|
Chris@0
|
77 $container->get('user.data'),
|
Chris@0
|
78 $container->get('logger.factory')->get('user')
|
Chris@0
|
79 );
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * Redirects to the user password reset form.
|
Chris@0
|
84 *
|
Chris@0
|
85 * In order to never disclose a reset link via a referrer header this
|
Chris@0
|
86 * controller must always return a redirect response.
|
Chris@0
|
87 *
|
Chris@0
|
88 * @param \Symfony\Component\HttpFoundation\Request $request
|
Chris@0
|
89 * The request.
|
Chris@0
|
90 * @param int $uid
|
Chris@0
|
91 * User ID of the user requesting reset.
|
Chris@0
|
92 * @param int $timestamp
|
Chris@0
|
93 * The current timestamp.
|
Chris@0
|
94 * @param string $hash
|
Chris@0
|
95 * Login link hash.
|
Chris@0
|
96 *
|
Chris@0
|
97 * @return \Symfony\Component\HttpFoundation\RedirectResponse
|
Chris@0
|
98 * The redirect response.
|
Chris@0
|
99 */
|
Chris@0
|
100 public function resetPass(Request $request, $uid, $timestamp, $hash) {
|
Chris@0
|
101 $account = $this->currentUser();
|
Chris@0
|
102 // When processing the one-time login link, we have to make sure that a user
|
Chris@0
|
103 // isn't already logged in.
|
Chris@0
|
104 if ($account->isAuthenticated()) {
|
Chris@0
|
105 // The current user is already logged in.
|
Chris@0
|
106 if ($account->id() == $uid) {
|
Chris@0
|
107 user_logout();
|
Chris@0
|
108 // We need to begin the redirect process again because logging out will
|
Chris@0
|
109 // destroy the session.
|
Chris@0
|
110 return $this->redirect(
|
Chris@0
|
111 'user.reset',
|
Chris@0
|
112 [
|
Chris@0
|
113 'uid' => $uid,
|
Chris@0
|
114 'timestamp' => $timestamp,
|
Chris@0
|
115 'hash' => $hash,
|
Chris@0
|
116 ]
|
Chris@0
|
117 );
|
Chris@0
|
118 }
|
Chris@0
|
119 // A different user is already logged in on the computer.
|
Chris@0
|
120 else {
|
Chris@0
|
121 /** @var \Drupal\user\UserInterface $reset_link_user */
|
Chris@0
|
122 if ($reset_link_user = $this->userStorage->load($uid)) {
|
Chris@0
|
123 drupal_set_message($this->t('Another user (%other_user) is already logged into the site on this computer, but you tried to use a one-time link for user %resetting_user. Please <a href=":logout">log out</a> and try using the link again.',
|
Chris@0
|
124 ['%other_user' => $account->getUsername(), '%resetting_user' => $reset_link_user->getUsername(), ':logout' => $this->url('user.logout')]), 'warning');
|
Chris@0
|
125 }
|
Chris@0
|
126 else {
|
Chris@0
|
127 // Invalid one-time link specifies an unknown user.
|
Chris@0
|
128 drupal_set_message($this->t('The one-time login link you clicked is invalid.'), 'error');
|
Chris@0
|
129 }
|
Chris@0
|
130 return $this->redirect('<front>');
|
Chris@0
|
131 }
|
Chris@0
|
132 }
|
Chris@0
|
133
|
Chris@0
|
134 $session = $request->getSession();
|
Chris@0
|
135 $session->set('pass_reset_hash', $hash);
|
Chris@0
|
136 $session->set('pass_reset_timeout', $timestamp);
|
Chris@0
|
137 return $this->redirect(
|
Chris@0
|
138 'user.reset.form',
|
Chris@0
|
139 ['uid' => $uid]
|
Chris@0
|
140 );
|
Chris@0
|
141 }
|
Chris@0
|
142
|
Chris@0
|
143 /**
|
Chris@0
|
144 * Returns the user password reset form.
|
Chris@0
|
145 *
|
Chris@0
|
146 * @param \Symfony\Component\HttpFoundation\Request $request
|
Chris@0
|
147 * The request.
|
Chris@0
|
148 * @param int $uid
|
Chris@0
|
149 * User ID of the user requesting reset.
|
Chris@0
|
150 *
|
Chris@0
|
151 * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
|
Chris@0
|
152 * The form structure or a redirect response.
|
Chris@0
|
153 *
|
Chris@0
|
154 * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
|
Chris@0
|
155 * If the pass_reset_timeout or pass_reset_hash are not available in the
|
Chris@0
|
156 * session. Or if $uid is for a blocked user or invalid user ID.
|
Chris@0
|
157 */
|
Chris@0
|
158 public function getResetPassForm(Request $request, $uid) {
|
Chris@0
|
159 $session = $request->getSession();
|
Chris@0
|
160 $timestamp = $session->get('pass_reset_timeout');
|
Chris@0
|
161 $hash = $session->get('pass_reset_hash');
|
Chris@0
|
162 // As soon as the session variables are used they are removed to prevent the
|
Chris@0
|
163 // hash and timestamp from being leaked unexpectedly. This could occur if
|
Chris@0
|
164 // the user does not click on the log in button on the form.
|
Chris@0
|
165 $session->remove('pass_reset_timeout');
|
Chris@0
|
166 $session->remove('pass_reset_hash');
|
Chris@0
|
167 if (!$hash || !$timestamp) {
|
Chris@0
|
168 throw new AccessDeniedHttpException();
|
Chris@0
|
169 }
|
Chris@0
|
170
|
Chris@0
|
171 /** @var \Drupal\user\UserInterface $user */
|
Chris@0
|
172 $user = $this->userStorage->load($uid);
|
Chris@0
|
173 if ($user === NULL || !$user->isActive()) {
|
Chris@0
|
174 // Blocked or invalid user ID, so deny access. The parameters will be in
|
Chris@0
|
175 // the watchdog's URL for the administrator to check.
|
Chris@0
|
176 throw new AccessDeniedHttpException();
|
Chris@0
|
177 }
|
Chris@0
|
178
|
Chris@0
|
179 // Time out, in seconds, until login URL expires.
|
Chris@0
|
180 $timeout = $this->config('user.settings')->get('password_reset_timeout');
|
Chris@0
|
181
|
Chris@0
|
182 $expiration_date = $user->getLastLoginTime() ? $this->dateFormatter->format($timestamp + $timeout) : NULL;
|
Chris@0
|
183 return $this->formBuilder()->getForm(UserPasswordResetForm::class, $user, $expiration_date, $timestamp, $hash);
|
Chris@0
|
184 }
|
Chris@0
|
185
|
Chris@0
|
186 /**
|
Chris@0
|
187 * Validates user, hash, and timestamp; logs the user in if correct.
|
Chris@0
|
188 *
|
Chris@0
|
189 * @param int $uid
|
Chris@0
|
190 * User ID of the user requesting reset.
|
Chris@0
|
191 * @param int $timestamp
|
Chris@0
|
192 * The current timestamp.
|
Chris@0
|
193 * @param string $hash
|
Chris@0
|
194 * Login link hash.
|
Chris@0
|
195 *
|
Chris@0
|
196 * @return \Symfony\Component\HttpFoundation\RedirectResponse
|
Chris@0
|
197 * Returns a redirect to the user edit form if the information is correct.
|
Chris@0
|
198 * If the information is incorrect redirects to 'user.pass' route with a
|
Chris@0
|
199 * message for the user.
|
Chris@0
|
200 *
|
Chris@0
|
201 * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
|
Chris@0
|
202 * If $uid is for a blocked user or invalid user ID.
|
Chris@0
|
203 */
|
Chris@0
|
204 public function resetPassLogin($uid, $timestamp, $hash) {
|
Chris@0
|
205 // The current user is not logged in, so check the parameters.
|
Chris@0
|
206 $current = REQUEST_TIME;
|
Chris@0
|
207 /** @var \Drupal\user\UserInterface $user */
|
Chris@0
|
208 $user = $this->userStorage->load($uid);
|
Chris@0
|
209
|
Chris@0
|
210 // Verify that the user exists and is active.
|
Chris@0
|
211 if ($user === NULL || !$user->isActive()) {
|
Chris@0
|
212 // Blocked or invalid user ID, so deny access. The parameters will be in
|
Chris@0
|
213 // the watchdog's URL for the administrator to check.
|
Chris@0
|
214 throw new AccessDeniedHttpException();
|
Chris@0
|
215 }
|
Chris@0
|
216
|
Chris@0
|
217 // Time out, in seconds, until login URL expires.
|
Chris@0
|
218 $timeout = $this->config('user.settings')->get('password_reset_timeout');
|
Chris@0
|
219 // No time out for first time login.
|
Chris@0
|
220 if ($user->getLastLoginTime() && $current - $timestamp > $timeout) {
|
Chris@0
|
221 drupal_set_message($this->t('You have tried to use a one-time login link that has expired. Please request a new one using the form below.'), 'error');
|
Chris@0
|
222 return $this->redirect('user.pass');
|
Chris@0
|
223 }
|
Chris@0
|
224 elseif ($user->isAuthenticated() && ($timestamp >= $user->getLastLoginTime()) && ($timestamp <= $current) && Crypt::hashEquals($hash, user_pass_rehash($user, $timestamp))) {
|
Chris@0
|
225 user_login_finalize($user);
|
Chris@0
|
226 $this->logger->notice('User %name used one-time login link at time %timestamp.', ['%name' => $user->getDisplayName(), '%timestamp' => $timestamp]);
|
Chris@0
|
227 drupal_set_message($this->t('You have just used your one-time login link. It is no longer necessary to use this link to log in. Please change your password.'));
|
Chris@0
|
228 // Let the user's password be changed without the current password
|
Chris@0
|
229 // check.
|
Chris@0
|
230 $token = Crypt::randomBytesBase64(55);
|
Chris@0
|
231 $_SESSION['pass_reset_' . $user->id()] = $token;
|
Chris@0
|
232 return $this->redirect(
|
Chris@0
|
233 'entity.user.edit_form',
|
Chris@0
|
234 ['user' => $user->id()],
|
Chris@0
|
235 [
|
Chris@0
|
236 'query' => ['pass-reset-token' => $token],
|
Chris@0
|
237 'absolute' => TRUE,
|
Chris@0
|
238 ]
|
Chris@0
|
239 );
|
Chris@0
|
240 }
|
Chris@0
|
241
|
Chris@0
|
242 drupal_set_message($this->t('You have tried to use a one-time login link that has either been used or is no longer valid. Please request a new one using the form below.'), 'error');
|
Chris@0
|
243 return $this->redirect('user.pass');
|
Chris@0
|
244 }
|
Chris@0
|
245
|
Chris@0
|
246 /**
|
Chris@0
|
247 * Redirects users to their profile page.
|
Chris@0
|
248 *
|
Chris@0
|
249 * This controller assumes that it is only invoked for authenticated users.
|
Chris@0
|
250 * This is enforced for the 'user.page' route with the '_user_is_logged_in'
|
Chris@0
|
251 * requirement.
|
Chris@0
|
252 *
|
Chris@0
|
253 * @return \Symfony\Component\HttpFoundation\RedirectResponse
|
Chris@0
|
254 * Returns a redirect to the profile of the currently logged in user.
|
Chris@0
|
255 */
|
Chris@0
|
256 public function userPage() {
|
Chris@0
|
257 return $this->redirect('entity.user.canonical', ['user' => $this->currentUser()->id()]);
|
Chris@0
|
258 }
|
Chris@0
|
259
|
Chris@0
|
260 /**
|
Chris@0
|
261 * Route title callback.
|
Chris@0
|
262 *
|
Chris@0
|
263 * @param \Drupal\user\UserInterface $user
|
Chris@0
|
264 * The user account.
|
Chris@0
|
265 *
|
Chris@0
|
266 * @return string|array
|
Chris@0
|
267 * The user account name as a render array or an empty string if $user is
|
Chris@0
|
268 * NULL.
|
Chris@0
|
269 */
|
Chris@0
|
270 public function userTitle(UserInterface $user = NULL) {
|
Chris@0
|
271 return $user ? ['#markup' => $user->getUsername(), '#allowed_tags' => Xss::getHtmlTagList()] : '';
|
Chris@0
|
272 }
|
Chris@0
|
273
|
Chris@0
|
274 /**
|
Chris@0
|
275 * Logs the current user out.
|
Chris@0
|
276 *
|
Chris@0
|
277 * @return \Symfony\Component\HttpFoundation\RedirectResponse
|
Chris@0
|
278 * A redirection to home page.
|
Chris@0
|
279 */
|
Chris@0
|
280 public function logout() {
|
Chris@0
|
281 user_logout();
|
Chris@0
|
282 return $this->redirect('<front>');
|
Chris@0
|
283 }
|
Chris@0
|
284
|
Chris@0
|
285 /**
|
Chris@0
|
286 * Confirms cancelling a user account via an email link.
|
Chris@0
|
287 *
|
Chris@0
|
288 * @param \Drupal\user\UserInterface $user
|
Chris@0
|
289 * The user account.
|
Chris@0
|
290 * @param int $timestamp
|
Chris@0
|
291 * The timestamp.
|
Chris@0
|
292 * @param string $hashed_pass
|
Chris@0
|
293 * The hashed password.
|
Chris@0
|
294 *
|
Chris@0
|
295 * @return \Symfony\Component\HttpFoundation\RedirectResponse
|
Chris@0
|
296 * A redirect response.
|
Chris@0
|
297 */
|
Chris@0
|
298 public function confirmCancel(UserInterface $user, $timestamp = 0, $hashed_pass = '') {
|
Chris@0
|
299 // Time out in seconds until cancel URL expires; 24 hours = 86400 seconds.
|
Chris@0
|
300 $timeout = 86400;
|
Chris@0
|
301 $current = REQUEST_TIME;
|
Chris@0
|
302
|
Chris@0
|
303 // Basic validation of arguments.
|
Chris@0
|
304 $account_data = $this->userData->get('user', $user->id());
|
Chris@0
|
305 if (isset($account_data['cancel_method']) && !empty($timestamp) && !empty($hashed_pass)) {
|
Chris@0
|
306 // Validate expiration and hashed password/login.
|
Chris@0
|
307 if ($timestamp <= $current && $current - $timestamp < $timeout && $user->id() && $timestamp >= $user->getLastLoginTime() && Crypt::hashEquals($hashed_pass, user_pass_rehash($user, $timestamp))) {
|
Chris@0
|
308 $edit = [
|
Chris@0
|
309 'user_cancel_notify' => isset($account_data['cancel_notify']) ? $account_data['cancel_notify'] : $this->config('user.settings')->get('notify.status_canceled'),
|
Chris@0
|
310 ];
|
Chris@0
|
311 user_cancel($edit, $user->id(), $account_data['cancel_method']);
|
Chris@0
|
312 // Since user_cancel() is not invoked via Form API, batch processing
|
Chris@0
|
313 // needs to be invoked manually and should redirect to the front page
|
Chris@0
|
314 // after completion.
|
Chris@16
|
315 return batch_process('<front>');
|
Chris@0
|
316 }
|
Chris@0
|
317 else {
|
Chris@0
|
318 drupal_set_message(t('You have tried to use an account cancellation link that has expired. Please request a new one using the form below.'), 'error');
|
Chris@0
|
319 return $this->redirect('entity.user.cancel_form', ['user' => $user->id()], ['absolute' => TRUE]);
|
Chris@0
|
320 }
|
Chris@0
|
321 }
|
Chris@0
|
322 throw new AccessDeniedHttpException();
|
Chris@0
|
323 }
|
Chris@0
|
324
|
Chris@0
|
325 }
|