src/Security/UserApiProvider.php line 29

Open in your IDE?
  1. <?php
  2. // src/Security/UserProviderrovider.php
  3. namespace App\Security;
  4. use App\Entity\User;
  5. use App\Exception\Api\UserNotEnabledException;
  6. use App\Exception\Api\UserNotValidatedException;
  7. use App\Repository\UserRepository;
  8. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  9. use Symfony\Component\Security\Core\Exception\UserNotFoundException;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. /**
  12.  * UserProvider.
  13.  */
  14. class UserApiProvider extends UserProvider
  15. {
  16.     private UserRepository $userRepository;
  17.     public function __construct(UserRepository $userRepository)
  18.     {
  19.         $this->userRepository $userRepository;
  20.     }
  21.     public function loadUserByIdentifier(string $identifier): UserInterface
  22.     {
  23.         $user $this->userRepository->loadUserByIdentifier($identifier);
  24.         if (null === $user) {
  25.             throw new UserNotFoundException();
  26.         }
  27.         if (!$user->getEnabled()) {
  28.             throw new UserNotEnabledException();
  29.         }
  30.         if (!$user->isValidated()) {
  31.             throw new UserNotValidatedException();
  32.         }
  33.         return $user;
  34.     }
  35.     /**
  36.      * Refreshes the user after being reloaded from the session.
  37.      *
  38.      * When a user is logged in, at the beginning of each request, the
  39.      * User object is loaded from the session and then this method is
  40.      * called. Your job is to make sure the user's data is still fresh by,
  41.      * for example, re-querying for fresh User data.
  42.      *
  43.      * If your firewall is "stateless: true" (for a pure API), this
  44.      * method is not called.
  45.      *
  46.      * @return UserInterface
  47.      */
  48.     public function refreshUser(UserInterface $user)
  49.     {
  50.         if (!$user instanceof User) {
  51.             throw new UnsupportedUserException(sprintf('Invalid user class "%s".'get_class($user)));
  52.         }
  53.         return $this->userRepository->refreshUser($user);
  54.     }
  55.     /**
  56.      * Tells Symfony to use this provider for this User class.
  57.      */
  58.     public function supportsClass(string $class)
  59.     {
  60.         return User::class === $class || is_subclass_of($classUser::class);
  61.     }
  62.     /**
  63.      * Upgrades the encoded password of a user, typically for using a better hash algorithm.
  64.      */
  65.     public function upgradePassword(UserInterface $userstring $newEncodedPassword): void
  66.     {
  67.         $this->userRepository->upgradePassword($user$newEncodedPassword);
  68.     }
  69.     /** @deprecated since 5.3 use loadUserByIdentifier */
  70.     public function loadUserByUsername(string $username)
  71.     {
  72.         return $this->loadUserByIdentifier($username);
  73.     }
  74. }