Symfony用户角色未读取

时间:2014-04-11 16:12:27

标签: php symfony

我试试:

在控制台中:

php app/console fos:user:promote grek admin
Role "admin" has been added to user "grek".

if ($this->get('security.context')->isGranted('ROLE_ADMIN')) die('ok');

这不是为什么?

我注销,登录和相同的情况

var_dump($this->get('security.context')->getToken()->getUser());

i>protected</i> 'roles' <font color='#888a85'>=&gt;</font> 
    <b>array</b> <i>(size=4)</i>
      0 ADMIN'</font> <i>(length=5)</i>
      1 OWNER'</font> <i>(length=5)</i>
      2 TESTER'</font> <i>(length=6)</i>


 $user = $this->get('security.context')->getToken()->getUser();
  var_dump($user->getRoles());

array (size=1)
  0 => string 'ROLE_USER' (length=9)

为什么$ user-&gt; getRoles()只提供一个?

当我添加 php app / console fos:user:提升grek ADMIN php app / console fos:user:提升grek ROLE_ADMIN php app / console fos:user:promote grek admin

然后检查 - 我没有角色if($ this-&gt; get(&#39; security.context&#39;) - &gt; isGranted

还有一些想法:

php app/console fos:user:promote grek test
Role "test" has been added to user "grek".
php app/console fos:user:demote grek test
User "grek" didn't have "test" role.
这是什么?

1 个答案:

答案 0 :(得分:1)

您已为用户授予ADMIN角色ROLE_ADMIN

<强>更新

您可以解决运行问题

php app/console fos:user:promote grek role_admin

或改变

if ($this->get('security.context')->isGranted('ROLE_ADMIN')) die('ok');

if ($this->get('security.context')->isGranted('ADMIN')) die('ok');