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

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.      * @param UserManagerInterface $userManager
  27.      */
  28.     public function __construct(UserManagerInterface $userManager)
  29.     {
  30.         $this->userManager $userManager;
  31.     }
  32.     /**
  33.      * {@inheritdoc}
  34.      */
  35.     public function loadUserByUsername($username)
  36.     {
  37.         $user $this->findUser($username);
  38.         if (!$user) {
  39.             throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.'$username));
  40.         }
  41.         return $user;
  42.     }
  43.     /**
  44.      * {@inheritdoc}
  45.      */
  46.     public function refreshUser(SecurityUserInterface $user)
  47.     {
  48.         if (!$user instanceof UserInterface) {
  49.             throw new UnsupportedUserException(sprintf('Expected an instance of FOS\UserBundle\Model\UserInterface, but got "%s".'get_class($user)));
  50.         }
  51.         if (!$this->supportsClass(get_class($user))) {
  52.             throw new UnsupportedUserException(sprintf('Expected an instance of %s, but got "%s".'$this->userManager->getClass(), get_class($user)));
  53.         }
  54.         if (null === $reloadedUser $this->userManager->findUserBy(array('id' => $user->getId()))) {
  55.             throw new UsernameNotFoundException(sprintf('User with ID "%s" could not be reloaded.'$user->getId()));
  56.         }
  57.         return $reloadedUser;
  58.     }
  59.     /**
  60.      * {@inheritdoc}
  61.      */
  62.     public function supportsClass($class)
  63.     {
  64.         $userClass $this->userManager->getClass();
  65.         return $userClass === $class || is_subclass_of($class$userClass);
  66.     }
  67.     /**
  68.      * Finds a user by username.
  69.      *
  70.      * This method is meant to be an extension point for child classes.
  71.      *
  72.      * @param string $username
  73.      *
  74.      * @return UserInterface|null
  75.      */
  76.     protected function findUser($username)
  77.     {
  78.         return $this->userManager->findUserByUsername($username);
  79.     }
  80. }