ascott

Entity - AccountsRepository

Jul 4th, 2012
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.60 KB | None | 0 0
  1. <?php
  2.  
  3. namespace RedK\Core\IndexBundle\Entity;
  4.  
  5. use Symfony\Component\Security\Core\User\UserInterface;
  6. use Symfony\Component\Security\Core\User\UserProviderInterface;
  7. use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  8. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  9. use Doctrine\ORM\EntityRepository;
  10. use Doctrine\ORM\NoResultException;
  11.  
  12. class AccountsRepository extends EntityRepository implements UserProviderInterface
  13. {
  14.     public function loadUserByUsername($username)
  15.     {
  16.         $q = $this
  17.            ->createQueryBuilder('u')
  18.            ->select('u, g')
  19.            ->leftJoin('u.groups', 'g')
  20.            ->where('u.email = :email')
  21.            ->setParameter('email', $username)
  22.            ->getQuery();
  23.  
  24.         try
  25.         {
  26.             $user = $q->getSingleResult();
  27.         }
  28.         catch (NoResultException $e)
  29.         {
  30.             throw new UsernameNotFoundException(
  31.                 sprintf('Unable to find an active account identified by "%s".', $username),
  32.                 null, 0, $e);
  33.         }
  34.  
  35.         return $user;
  36.     }
  37.  
  38.     public function refreshUser(UserInterface $user)
  39.     {
  40.         $class = get_class($user);
  41.         if (!$this->supportsClass($class)) {
  42.             throw new UnsupportedUserException(
  43.                 sprintf('Instances of "%s" are not supported.', $class));
  44.         }
  45.  
  46.         return $this->loadUserByUsername($user->getUsername());
  47.     }
  48.  
  49.     public function supportsClass($class)
  50.     {
  51.         return $this->getEntityName() === $class || is_subclass_of($class, $this->getEntityName());
  52.     }
  53. }
Add Comment
Please, Sign In to add comment