Skip to content

Instantly share code, notes, and snippets.

@0x46616c6b
Created September 12, 2012 16:44
Show Gist options
  • Save 0x46616c6b/3708008 to your computer and use it in GitHub Desktop.
Save 0x46616c6b/3708008 to your computer and use it in GitHub Desktop.
Maybe a bug in EntityRepository?
<?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