partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

LegacyUserProvider.php (1523B)


      1 <?php
      2 
      3 namespace PartKeepr\AuthBundle\Security\User;
      4 
      5 use Doctrine\ORM\EntityManager;
      6 use PartKeepr\AuthBundle\Entity\User;
      7 use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
      8 use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
      9 use Symfony\Component\Security\Core\User\UserInterface;
     10 use Symfony\Component\Security\Core\User\UserProviderInterface;
     11 
     12 class LegacyUserProvider implements UserProviderInterface
     13 {
     14     /**
     15      * @var EntityManager
     16      */
     17     private $entityManager;
     18 
     19     public function __construct(EntityManager $entityManager)
     20     {
     21         $this->entityManager = $entityManager;
     22     }
     23 
     24     public function loadUserByUsername($username)
     25     {
     26         $repository = $this->entityManager->getRepository('PartKeepr\\AuthBundle\\Entity\\User');
     27 
     28         $user = $repository->findOneBy(['username' => $username, 'legacy' => true]);
     29 
     30         if ($user !== null) {
     31             return $user;
     32         }
     33 
     34         throw new UsernameNotFoundException(
     35             sprintf('Username "%s" does not exist.', $username)
     36         );
     37     }
     38 
     39     public function refreshUser(UserInterface $user)
     40     {
     41         if (!$user instanceof User) {
     42             throw new UnsupportedUserException(
     43                 sprintf('Instances of "%s" are not supported.', get_class($user))
     44             );
     45         }
     46 
     47         return $this->loadUserByUsername($user->getUsername());
     48     }
     49 
     50     public function supportsClass($class)
     51     {
     52         return $class === 'PartKeepr\\AuthBundle\\Entity\\User';
     53     }
     54 }