Advertisement
Guest User

UserRepository

a guest
Nov 5th, 2013
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.96 KB | None | 0 0
  1. <?php
  2. // src/Acme/AccountBundle/Entity/UserRepository.php
  3. namespace Acme\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.             ->select('u, r')
  19.             ->leftJoin('u.roles', 'r')
  20.             ->where('u.username = :username OR u.email = :email')
  21.             ->setParameter('username', $username)
  22.             ->setParameter('email', $username)
  23.             ->getQuery();
  24.  
  25.         try {
  26.             // The Query::getSingleResult() method throws an exception
  27.             // if there is no record matching the criteria.
  28.             $user = $q->getSingleResult();
  29.         } catch (NoResultException $e) {
  30.             $message = sprintf(
  31.                 'Unable to find an active admin AcmeUserBundle:User object identified by "%s".',
  32.                 $username
  33.             );
  34.             throw new UsernameNotFoundException($message, 0, $e);
  35.         }
  36.  
  37.         return $user;
  38.     }
  39.  
  40.     public function refreshUser(UserInterface $user)
  41.     {
  42.         $class = get_class($user);
  43.         if (!$this->supportsClass($class)) {
  44.             throw new UnsupportedUserException(
  45.                 sprintf(
  46.                     'Instances of "%s" are not supported.',
  47.                     $class
  48.                 )
  49.             );
  50.         }
  51.  
  52.         return $this->find($user->getId());
  53.     }
  54.  
  55.     public function supportsClass($class)
  56.     {
  57.         return $this->getEntityName() === $class
  58.             || is_subclass_of($class, $this->getEntityName());
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement