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