2013-10-26 55 views
5

我正在爲我的User實體創建自定義實體提供程序。但是,當我登錄表單中輸入用戶名和密碼,我得到這個異常:Symfony2安全性 - 用戶「Ibw UserBundle Entity User」沒有用戶提供程序

There is no user provider for user "Ibw\UserBundle\Entity\User". 

當我切換回默認主義實體提供商,一切順利。

首先這裏是我的security.yml

security: 
    role_hierarchy: 
     ROLE_ADMIN:  ROLE_USER 
     ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH] 

    firewalls: 
     dev: 
      pattern: ^/(_(profiler|wdt)|css|images|js)/ 
      security: false 
     secured_area: 
      pattern: ^/ 
      anonymous: ~ 
      form_login: 
       login_path: /login 
       check_path: /login_check 
       default_target_path: ibw_job_index 
      logout: 
       path: /logout 
       target:/

    access_control: 
     - { path: ^/admin, roles: ROLE_ADMIN } 

    providers: 
     default_provider: 
      entity: { class: Ibw\UserBundle\Entity\User } 

    encoders: 
     Ibw\UserBundle\Entity\User: sha512 

這裏的Ibw\UserBundle\Entity\User類:

<?php 
namespace Ibw\UserBundle\Entity; 

use Doctrine\Bundle\DoctrineBundle\Registry; 
use Symfony\Component\DependencyInjection\ContainerInterface; 
use Symfony\Component\Security\Core\Role\Role; 
use Symfony\Component\Security\Core\User\UserInterface; 
use Doctrine\ORM\Mapping as ORM; 

/** 
* Class User 
* @ORM\Table(name="users") 
* @ORM\Entity(repositoryClass="Ibw\UserBundle\Repository\UserRepository") 
*/ 
class User implements UserInterface, \Serializable 
{ 
    /** 
    * @ORM\Column(type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** 
    * @ORM\Column(type="string", length=25, unique=true) 
    */ 
    protected $username; 

    /** 
    * @ORM\Column(type="string", length=40) 
    */ 
    protected $salt; 

    /** 
    * @ORM\Column(type="string", length=128) 
    */ 
    protected $password; 

    public function __construct(ContainerInterface $container) 
    { 
     $this->isActive = true; 
     $this->salt = md5(uniqid(null, true)); 
     $this->container = $container; 
    } 

    public function setUsername($username) 
    { 
     $this->username = $username; 
     return $this; 
    } 

    public function setPassword($password) 
    { 
     $encoder = $this->container->get('security.encoder_factory')->getEncoder($this); 
     $this->password = $encoder->encodePassword($password, $this->getSalt()); 
     return $this; 
    } 

    /** 
    * @return Role[] The user roles 
    */ 
    public function getRoles() 
    { 
     return array('ROLE_ADMIN'); 
    } 

    public function getPassword() 
    { 
     return $this->password; 
    } 

    public function getSalt() 
    { 
     return $this->salt; 
    } 

    public function getUsername() 
    { 
     return $this->username; 
    } 

    public function eraseCredentials() 
    { 
     // TODO: Implement eraseCredentials() method. 
    } 

    public function serialize() 
    { 
     return serialize(array($this->id)); 
    } 

    public function unserialize($serialized) 
    { 
     list($this->id) = unserialize($serialized); 
    } 


    public function getId() 
    { 
     return $this->id; 
    } 

    public function setSalt($salt) 
    { 
     $this->salt = $salt; 

     return $this; 
    } 
} 

最後,這裏是我的Ibw\UserBundle\Repository\UserRepository類:

<?php 

namespace Ibw\UserBundle\Repository; 

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

class UserRepository extends EntityRepository implements UserProviderInterface 
{ 

    public function loadUserByUsername($username) 
    { 
     $q = $this->createQueryBuilder('u') 
        ->select('u') 
        ->where('u.username = :username') 
        ->setParameter('username', $username) 
        ->getQuery(); 

     try 
     { 
      $user = $q->getSingleResult(); 
     } 
     catch (NoResultException $e) 
     { 
      $message = sprintf('Unable to find active admin identified by %s', $username); 
      throw new UsernameNotFoundException($message, 0, $e); 
     } 
     return $user; 
    } 


    public function refreshUser(UserInterface $user) 
    { 
     $class = get_class($user); 
     if(!$this->supportsClass($user)) 
     { 
      $message = sprintf('Unsupported class type : %s', $class); 
      throw new UnsupportedUserException($message); 
     } 

     return $this->find($user->getId()); 
    } 


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

編輯

我試圖把它定義爲一個服務也,在JobeetBundle/Resources/config/services.yml

parameters: 
    ibw.user.provider.class: Ibw\UserBundle\Repository\UserRepository 
    ibw.user.provider.entity.class: Ibw\UserBundle\Entity\User 

services: 
    ibw.user.provider: 
     class: %ibw.user.provider.class% 
     factory_service: doctrine 
     factory_method: getRepository 
     arguments: 
      - %ibw.user.provider.entity.class% 

而且在jobeet/app/config/security.yml

providers: 
    default_provider: 
     id: ibw.user.provider 
encoders: 
    Ibw\UserBundle\Entity\User: sha512 

但不幸的是它給了我之後的確切同樣的錯誤我張貼登錄表單。

回答

6

我跟蹤了ContextListener類中的問題,結果發現它是一個錯字!

在這裏:

public function refreshUser(UserInterface $user) 
{ 
    $class = get_class($user); 
    if (!$this->supportsClass($user)) { //This should be $class not $user 
     $message = sprintf('Unsupported class type : %s', $class); 
     throw new UnsupportedUserException($message); 
    } 

    return $this->find($user->getId()); 
} 

變化是if到:

if (!$this->supportsClass($class)) {} 
0

您需要使用IbwUserBundle:User符號而不是entity: { class: Ibw\UserBundle\Entity\User }來定義您的用戶提供者,或者只是將您的用戶提供者定義爲服務並使用其名稱。

+0

嗯,但什麼」你的鏈接,這'HTTP的區別:// goo.gl/DPHcJ4',因爲我甚至不會將它作爲服務添加,而且它也用作存儲庫。 –

+0

對不起,我不注意。在檢查代碼後,我解決了我的問題。 –

+0

我嘗試了兩種,定義爲服務或使用它沒有服務,但仍然有同樣的錯誤。 我將用我的服務編輯問題。 –

相關問題