vendor/friendsofsymfony/user-bundle/Security/UserProvider.php line 53

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the FOSUserBundle package.
  4.  *
  5.  * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace FOS\UserBundle\Security;
  11. use FOS\UserBundle\Model\UserInterface;
  12. use FOS\UserBundle\Model\UserManagerInterface;
  13. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  14. use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  15. use Symfony\Component\Security\Core\User\UserInterface as SecurityUserInterface;
  16. use Symfony\Component\Security\Core\User\UserProviderInterface;
  17. class UserProvider implements UserProviderInterface
  18. {
  19.     /**
  20.      * @var UserManagerInterface
  21.      */
  22.     protected $userManager;
  23.     /**
  24.      * Constructor.
  25.      */
  26.     public function __construct(UserManagerInterface $userManager)
  27.     {
  28.         $this->userManager $userManager;
  29.     }
  30.     /**
  31.      * {@inheritdoc}
  32.      */
  33.     public function loadUserByUsername($username)
  34.     {
  35.         $user $this->findUser($username);
  36.         if (!$user) {
  37.             throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.'$username));
  38.         }
  39.         return $user;
  40.     }
  41.     /**
  42.      * {@inheritdoc}
  43.      */
  44.     public function refreshUser(SecurityUserInterface $user)
  45.     {
  46.         if (!$user instanceof UserInterface) {
  47.             throw new UnsupportedUserException(sprintf('Expected an instance of FOS\UserBundle\Model\UserInterface, but got "%s".'get_class($user)));
  48.         }
  49.         if (!$this->supportsClass(get_class($user))) {
  50.             throw new UnsupportedUserException(sprintf('Expected an instance of %s, but got "%s".'$this->userManager->getClass(), get_class($user)));
  51.         }
  52.         if (null === $reloadedUser $this->userManager->findUserBy(['id' => $user->getId()])) {
  53.             throw new UsernameNotFoundException(sprintf('User with ID "%s" could not be reloaded.'$user->getId()));
  54.         }
  55.         return $reloadedUser;
  56.     }
  57.     /**
  58.      * {@inheritdoc}
  59.      */
  60.     public function supportsClass($class)
  61.     {
  62.         $userClass $this->userManager->getClass();
  63.         return $userClass === $class || is_subclass_of($class$userClass);
  64.     }
  65.     /**
  66.      * Finds a user by username.
  67.      *
  68.      * This method is meant to be an extension point for child classes.
  69.      *
  70.      * @param string $username
  71.      *
  72.      * @return UserInterface|null
  73.      */
  74.     protected function findUser($username)
  75.     {
  76.         return $this->userManager->findUserByUsername($username);
  77.     }
  78. }