vendor/symfony/src/Symfony/Bridge/Doctrine/Security/User/EntityUserProvider.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Bridge\Doctrine\Security\User;
       
    13 
       
    14 use Doctrine\ORM\EntityManager;
       
    15 use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
       
    16 use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
       
    17 use Symfony\Component\Security\Core\User\UserProviderInterface;
       
    18 use Symfony\Component\Security\Core\User\UserInterface;
       
    19 
       
    20 /**
       
    21  * Wrapper around a Doctrine EntityManager.
       
    22  *
       
    23  * Provides easy to use provisioning for Doctrine entity users.
       
    24  *
       
    25  * @author Fabien Potencier <fabien@symfony.com>
       
    26  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
       
    27  */
       
    28 class EntityUserProvider implements UserProviderInterface
       
    29 {
       
    30     private $class;
       
    31     private $repository;
       
    32     private $property;
       
    33 
       
    34     public function __construct(EntityManager $em, $class, $property = null)
       
    35     {
       
    36         $this->class = $class;
       
    37 
       
    38         if (false !== strpos($this->class, ':')) {
       
    39             $this->class = $em->getClassMetadata($class)->name;
       
    40         }
       
    41 
       
    42         $this->repository = $em->getRepository($class);
       
    43         $this->property = $property;
       
    44     }
       
    45 
       
    46     /**
       
    47      * {@inheritdoc}
       
    48      */
       
    49     public function loadUserByUsername($username)
       
    50     {
       
    51         if (null !== $this->property) {
       
    52             $user = $this->repository->findOneBy(array($this->property => $username));
       
    53         } else {
       
    54             if (!$this->repository instanceof UserProviderInterface) {
       
    55                 throw new \InvalidArgumentException(sprintf('The Doctrine repository "%s" must implement UserProviderInterface.', get_class($this->repository)));
       
    56             }
       
    57 
       
    58             $user = $this->repository->loadUserByUsername($username);
       
    59         }
       
    60 
       
    61         if (null === $user) {
       
    62             throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
       
    63         }
       
    64 
       
    65         return $user;
       
    66     }
       
    67 
       
    68     /**
       
    69      * {@inheritDoc}
       
    70      */
       
    71     public function refreshUser(UserInterface $user)
       
    72     {
       
    73         if (!$user instanceof $this->class) {
       
    74             throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
       
    75         }
       
    76 
       
    77         return $this->loadUserByUsername($user->getUsername());
       
    78     }
       
    79 
       
    80     /**
       
    81      * {@inheritDoc}
       
    82      */
       
    83     public function supportsClass($class)
       
    84     {
       
    85         return $class === $this->class;
       
    86     }
       
    87 }