Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\user;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Entity\EntityManagerInterface;
|
Chris@0
|
6 use Drupal\Core\Password\PasswordInterface;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Validates user authentication credentials.
|
Chris@0
|
10 */
|
Chris@0
|
11 class UserAuth implements UserAuthInterface {
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * The entity manager.
|
Chris@0
|
15 *
|
Chris@0
|
16 * @var \Drupal\Core\Entity\EntityManagerInterface
|
Chris@0
|
17 */
|
Chris@0
|
18 protected $entityManager;
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * The password hashing service.
|
Chris@0
|
22 *
|
Chris@0
|
23 * @var \Drupal\Core\Password\PasswordInterface
|
Chris@0
|
24 */
|
Chris@0
|
25 protected $passwordChecker;
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * Constructs a UserAuth object.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
|
Chris@0
|
31 * The entity manager.
|
Chris@0
|
32 * @param \Drupal\Core\Password\PasswordInterface $password_checker
|
Chris@0
|
33 * The password service.
|
Chris@0
|
34 */
|
Chris@0
|
35 public function __construct(EntityManagerInterface $entity_manager, PasswordInterface $password_checker) {
|
Chris@0
|
36 $this->entityManager = $entity_manager;
|
Chris@0
|
37 $this->passwordChecker = $password_checker;
|
Chris@0
|
38 }
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * {@inheritdoc}
|
Chris@0
|
42 */
|
Chris@0
|
43 public function authenticate($username, $password) {
|
Chris@0
|
44 $uid = FALSE;
|
Chris@0
|
45
|
Chris@0
|
46 if (!empty($username) && strlen($password) > 0) {
|
Chris@0
|
47 $account_search = $this->entityManager->getStorage('user')->loadByProperties(['name' => $username]);
|
Chris@0
|
48
|
Chris@0
|
49 if ($account = reset($account_search)) {
|
Chris@0
|
50 if ($this->passwordChecker->check($password, $account->getPassword())) {
|
Chris@0
|
51 // Successful authentication.
|
Chris@0
|
52 $uid = $account->id();
|
Chris@0
|
53
|
Chris@0
|
54 // Update user to new password scheme if needed.
|
Chris@0
|
55 if ($this->passwordChecker->needsRehash($account->getPassword())) {
|
Chris@0
|
56 $account->setPassword($password);
|
Chris@0
|
57 $account->save();
|
Chris@0
|
58 }
|
Chris@0
|
59 }
|
Chris@0
|
60 }
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 return $uid;
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 }
|