Created
September 12, 2012 16:44
-
-
Save 0x46616c6b/3708008 to your computer and use it in GitHub Desktop.
Maybe a bug in EntityRepository?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Vita\UserBundle\Entity; | |
use Symfony\Component\Security\Core\User\UserProviderInterface; | |
use Symfony\Component\Security\Core\User\UserInterface; | |
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; | |
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; | |
use Doctrine\ORM\EntityRepository; | |
use Doctrine\ORM\NoResultException; | |
use Vita\UserBundle\Entity\User; | |
/** | |
* Vita\UserBundle\Entity\UserProvider | |
*/ | |
class UserRepository extends EntityRepository implements UserProviderInterface { | |
public function loadUserByUsername($username) { | |
/* | |
* The QueryBuilder from EntityRepository does not work properly. The email field throws an error "not defined". | |
* But in the Model it still defined. Without the email in the Query the authentication still fails. | |
*/ | |
/*$query = $this->createQueryBuilder('u') | |
->where('u.username = :username OR u.email = :email') | |
->setParameter('username', $username) | |
->setParameter('email', $username) | |
->getQuery();*/ | |
$query = $this->getEntityManager()->getRepository('VitaUserBundle:User')->createQueryBuilder('u') | |
->where('u.username = :username OR u.email = :email') | |
->setParameter('username', $username) | |
->setParameter('email', $username) | |
->getQuery(); | |
try { | |
$user = $query->getSingleResult(); | |
} catch (NoResultException $e) { | |
throw new UsernameNotFoundException(sprintf('Unable to find an active admin VitaUserBundle:User object identified by "%s".', $username), null, 0, $e); | |
} | |
return $user; | |
} | |
public function refreshUser(UserInterface $user) { | |
$class = get_class($user); | |
if (!$this->supportsClass($class)) { | |
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', $class)); | |
} | |
return $this->loadUserByUsername($user->getUsername()); | |
} | |
public function supportsClass($class) { | |
return $this->getEntityName() === $class || is_subclass_of($class, $this->getEntityName()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment