Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.88 KB | None | 0 0
  1. <?php
  2. // src/WX/ExchangeBundle/Entity/UserRepository.php
  3. namespace WX\ExchangeBundle\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.username = :username OR u.email = :email')
  19.             ->setParameter('username', $username)
  20.             ->setParameter('email', $username)
  21.             ->getQuery();
  22.  
  23.         try {
  24.             // The Query::getSingleResult() method throws an exception
  25.             // if there is no record matching the criteria.
  26.             $user = $q->getSingleResult();
  27.         } catch (NoResultException $e) {
  28.             $message = sprintf(
  29.                 'Unable to find an active User object identified by "%s".',
  30.                 $username
  31.             );
  32.             throw new UsernameNotFoundException($message, 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(
  44.                     'Instances of "%s" are not supported.',
  45.                     $class
  46.                 )
  47.             );
  48.         }
  49.  
  50.         return $this->find($user->getId());
  51.     }
  52.  
  53.     public function supportsClass($class)
  54.     {
  55.         return $this->getEntityName() === $class
  56.             || is_subclass_of($class, $this->getEntityName());
  57.     }
  58. }
  59.  
  60. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement