annotate core/modules/user/src/UserAuth.php @ 19:fa3358dc1485 tip

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