i try:
控制台中:
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'>=></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 app/console fos:user:proment grek ADMINphp app/console fos:user:proment grek ROLE_ADMINphp应用程序/控制台fos:user:促进grek管理
然后检查-我在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.
这是什么?
您已授予用户角色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');