【问题标题】:Symfony user role not readSymfony 用户角色未读取
【发布时间】:2014-04-11 16:12:26
【问题描述】:

我试试:

在控制台中:

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->getRoles() 只给一个?

当我添加 php 应用程序/控制台 fos:user:promote grek ADMIN php 应用程序/控制台 fos:user:promote grek ROLE_ADMIN php app/console fos:user:promote grek admin

然后检查 - 我在 if ($this->get('security.context')->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.

这是什么?

【问题讨论】:

  • 试试php app/console fos:user:promote grek role_admin
  • 这个不改情况也是一样。

标签: php symfony


【解决方案1】:

您已授予用户角色ADMIN,而该角色应该是ROLE_ADMIN

更新

您可以通过运行解决您的问题

php app/console fos:user:promote grek role_admin

或改变

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

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

【讨论】:

  • 它有助于使答案更加明确 - 即参考您的答案适用的特定代码行并提供更正语法的示例。
猜你喜欢
  • 2014-01-23
  • 1970-01-01
  • 2018-10-16
  • 2019-09-24
  • 2013-06-14
  • 1970-01-01
  • 2014-07-05
  • 2013-10-26
  • 2021-09-20
相关资源
最近更新 更多