2014-02-25 126 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 
{ 
//....... 
} 
相關問題