2013-10-29 19 views
1

我嘗試配置防火牆來驗證用戶。這一步很好,因爲當我在登錄表單中放置了一個錯誤的用戶時,應用程序返回消息「Bad credentials。」,但是當用戶和密碼正確時,應用程序嘗試獲取用戶角色失敗。檢查登錄無法在Symfony2中工作

我的實體用戶被稱爲「Usuario」,我的實體Rol被稱爲「Perfil」。在我的用戶實體有這樣的代碼:

/** 
* @ORM\ManyToMany(targetEntity="AppsManantiales\CommonBundle\Entity\Perfil") 
* @ORM\JoinTable(name="usuarios_perfiles", 
*  joinColumns={@ORM\JoinColumn(name="idUsuario", referencedColumnName="idusuario")}, 
*  inverseJoinColumns={@ORM\JoinColumn(name="idPerfil", referencedColumnName="idperfil")} 
*) 
*/ 
protected $perfiles; 

/** 
* Get perfiles (getter auto generated) 
* 
* @return \Doctrine\Common\Collections\Collection 
*/ 
public function getPerfiles() 
{ 
    return $this->perfiles; 
} 

/** 
* Hook method 
*/ 
public function getRoles() { 
    return $this->getPerfiles()->toArray(); 
} 

登錄的過程與此消息失敗:

Error: Call to a member function getRole() on a non-object

在這個文件中:

/vendor/symfony/symfony/src/Symfony/Bundle/SecurityBundle/DataCollector/SecurityDataCollector.php line 60

所以,檢查引線:

array_map(function ($role){ return $role->getRole();}, $token->getRoles()),

然後,用va進行調試r_dump:

var_dump($token->isAuthenticated()); 
var_dump($token->getUsername()); 

最後幾行,返回正確的數據,但在嘗試這個辦法:

var_dump($token->getRoles()); 

的var_dump顯示有錯誤的數據數組:

array (size=2) 0 => string 'ccraig' (length=6) 1 => int 3

'ccraig' 字符串值,實際的用戶實體實例,但其他值不知道。 很明顯,這個值不理解我的Perfil(Rol)實體的「getRole」方法。

那麼,爲什麼不能獲得真正的角色數組呢? 我在測試其他控制器的方法getRoles(具有相同的用戶)的正常工作:

$doctrine = $this->getDoctrine(); 
$users = $doctrine->getRepository('CommonBundle:Usuario'); 
$user = $users->find(8); 
$perfiles = $user->getPerfiles(); 
$out = $perfiles->getValues(); 
var_dump($out); 

的var_dump顯示:

array (size=2) 
    0 => 
    object(AppsManantiales\CommonBundle\Entity\Perfil)[405] 
     protected 'idperfil' => int 3 
     protected 'nombre' => string 'ROLE_DOCENTE' (length=12) 
     protected 'administrador' => boolean true 
     protected 'paginaDefault' => null 
    1 => 
    object(AppsManantiales\CommonBundle\Entity\Perfil)[406] 
     protected 'idperfil' => int 1 
     protected 'nombre' => string 'ROLE_ADMIN' (length=10) 
     protected 'administrador' => boolean true 
     protected 'paginaDefault' => null 

任何想法?我最新的Symfony。 謝謝!

回答

2

解決方案很簡單。我不應該返回一個數組或角色對象。我應該返回一個角色名稱(字符串)數組。