簡體   English   中英

Symfony用戶角色未讀

[英]Symfony user role not read

我嘗試:

在控制台中:

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應用程序/控制台fos:user:promote grek admin

然后檢查-我沒有作用($ 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

UPDATE

您可以運行來解決您的問題

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');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM