Advertisement
Guest User

UserRepository

a guest
Oct 21st, 2012
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.59 KB | None | 0 0
  1. <?php
  2.  
  3. namespace SeerUK\Security\AccountBundle\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 UserRepository extends EntityRepository implements UserProviderInterface
  13. {
  14.     public function loadUserByUsername($username)
  15.     {
  16.         $q = $this
  17.             ->createQueryBuilder('u')
  18.             ->where('u.account_name = :usefdgdfgfrname OR u.account_email = :email')
  19.             ->setParameter('username', $username)
  20.             ->setParameter('email', $username)
  21.             ->getQuery()
  22.         ;
  23.  
  24.         try {
  25.             $user = $q->getSingleResult();
  26.         } catch (NoResultException $e) {
  27.             throw new UsernameNotFoundException(sprintf('Unable to find an active admin AcmeUserBundle:User object identified by "%s".', $username), null, 0, $e);
  28.         }
  29.  
  30.         return $user;
  31.     }
  32.  
  33.     public function refreshUser(UserInterface $user)
  34.     {
  35.         $class = get_class($user);
  36.         if (!$this->supportsClass($class)) {
  37.             throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', $class));
  38.         }
  39.  
  40.         return $this->loadUserByUsername($user->getUsername());
  41.     }
  42.  
  43.     public function supportsClass($class)
  44.     {
  45.         return $this->getEntityName() === $class || is_subclass_of($class, $this->getEntityName());
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement