2014-02-25 55 views
1

我想用户可以通过用户名或电子邮件登录。根据document我security.yml代码Symfony 2.3。通过用户名或电子邮件登录

providers: 
    entity_members: 
     entity: 
      class: AcmeBundle:Members 

它给人的错误

学说库“学说\ ORM \ EntityRepository”必须实现UserProviderInterface 如果

property: username 
追加实体提供商

然后我只能通过用户名登录不通过电子邮件我的存储库类是

 namespace PropertyMart\UserBundle\Entity; 

     use Symfony\Component\Security\Core\User\UserInterface; 
     use Symfony\Component\Security\Core\User\UserProviderInterface; 
     use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; 
     use Symfony\Component\Security\Core\Exception\UnsupportedUserException; 
     use Doctrine\ORM\EntityRepository; 
     use Doctrine\ORM\NoResultException; 

     class MembersRepository extends EntityRepository implements UserProviderInterface 
     { 
      public function loadUserByUsername($username) 
      { 
       $q = $this 
        ->createQueryBuilder('u') 
        ->where('u.username = :username OR u.email = :email') 
        ->setParameter('username', $username) 
        ->setParameter('email', $username) 
        ->getQuery() 
       ; 

       try { 
        // The Query::getSingleResult() method throws an exception 
        // if there is no record matching the criteria. 
        $user = $q->getSingleResult(); 
       } catch (NoResultException $e) { 
        throw new UsernameNotFoundException(sprintf('Unable to find an active admin UserBundle: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->find($user->getId()); 
      } 

      public function supportsClass($class) 
      { 
       return $this->getEntityName() === $class || is_subclass_of($class, $this->getEntityName()); 
      } 
     } 

无法短路问题..... security.yml没有财产:用户名是在symfony的2.1 *工作

回答

3

你的“会员”实体类必须实现的UserInterface。

例:

use Symfony\Component\Security\Core\User\UserInterface; 

class Members implements UserInterface 
{ 
//....... 
} 
相关问题