"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/user/src/UserAuth.php" (26 Nov 2020, 1945 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "UserAuth.php" see the Fossies "Dox" file reference documentation.

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