daily pastebin goal
63%
SHARE
TWEET

Untitled

a guest Oct 18th, 2018 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. namespace Acme\UserBundle\Entity;
  3.  
  4. use Symfony\Component\Security\Core\User\UserInterface;
  5. use Symfony\Component\Security\Core\User\UserProviderInterface;
  6. use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  7. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  8. use Doctrine\ORM\EntityRepository;
  9. use Doctrine\ORM\NoResultException;
  10.  
  11. class UserRepository extends EntityRepository implements UserProviderInterface
  12. {
  13.     public function loadUserByUsername($username)
  14.     {
  15.         $q = $this
  16.             ->createQueryBuilder('u')
  17.             ->select('u, g')
  18.             ->leftJoin('u.groups', 'g')
  19.             ->where('u.username = :username OR u.email = :email')
  20.             ->setParameter('username', $username)
  21.             ->setParameter('email', $username)
  22.             ->getQuery()
  23.             ;
  24.         try {
  25.             // The Query::getSingleResult() method throws an exception
  26.             // if there is no record matching the criteria.
  27.             $user = $q->getSingleResult();
  28.         } catch (NoResultException $e) {
  29.             throw new UsernameNotFoundException(sprintf('Unable to find an active admin
  30.                   AcmeUserBundle:User object identified by "%s".', $username), null, 0, $e);
  31.         }
  32.         return $user;
  33.     }
  34.    
  35.     public function refreshUser(UserInterface $user)
  36.     {
  37.         $class = get_class($user);
  38.         if (!$this->supportsClass($class)) {
  39.             throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', $class));
  40.         }
  41.        
  42.         return $this->loadUserByUsername($user->getUsername());
  43.     }
  44.  
  45.     public function supportsClass($class)
  46.     {
  47.         return $this->getEntityName() === $class || is_subclass_of($class,
  48.             $this->getEntityName());
  49.     }
  50. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top