【问题标题】:isGranted returns FALSE with User extending FOSUserBundleisGranted 返回 FALSE,用户扩展 FOSUserBundle
【发布时间】:2015-03-10 18:07:13
【问题描述】:

为了问题的简单性,我没有在我的 security.yml 上使用 role_hierarchy。

我有一个登录用户。如果我在控制器上执行此操作:

dump($this->get('security.token_storage')->getToken()->getUser()->getRoles());

这是我得到的:

array:3 [▼
0 => "ROLE_SUPER_ADMIN" 
1 => "ROLE_ADMIN" 
2 => "ROLE_USER"
]

但是,如果我要求 isGranted,我只会在 'ROLE_USER' 上得到 true,而在所有其余部分都得到 false:

dump($this->get('security.authorization_checker')->isGranted('ROLE_USER'));
dump($this->get('security.authorization_checker')->isGranted('ROLE_ADMIN'));
dump($this->get('security.authorization_checker')->isGranted('ROLE_SUPER_ADMIN'));

给我:

true
false
false

我正在使用 FOSUserBundle,我的用户扩展了他们的 BaseUser(它又实现了 UserInterface),我没有在我的用户实体上触及它的任何方法。

你能发现问题吗?是不是我不能将 security.checker 与 FOSUSerBundle 一起使用?

更新: 如果我转储($this->getUser()) 这就是我得到的:

Usuario {#2290 ▼
#id: 3
#username: "u1"
#usernameCanonical: "u1"
#email: "u1"
#emailCanonical: "u1"
#enabled: true
#salt: "8wqvgv5t24g0ssck44gw4008c04g8sg"
#password:        "zfDmozi78wrglXx3SUaCiz7490o4ZzKYEukcbdlCQ5FAWpA4jgLFQT6BXNbo3tzTyhdPDOCC/h4ZDs32SKlGEw=="
#plainPassword: null
#lastLogin: DateTime {#2288 ▶}
#confirmationToken: null
#passwordRequestedAt: null
#groups: null
#locked: false
#expired: false
#expiresAt: null
#roles: array:2 [▼
0 => "ROLE_SUPER_ADMIN"
1 => "ROLE_ADMIN"
]
#credentialsExpired: false
#credentialsExpireAt: null
}

如您所见,FSOS 只是在 getRoles() 方法中添加“ROLE_USER”,因为它不存在于实际的角色数组中。

【问题讨论】:

  • 那么用户在数据库中分配了三个不同的角色?我刚刚使用您的代码对其进行了测试,它为我正确返回(但我有层次结构)。你能转储$this->getUser() 并把它放在这里吗?
  • 更新了!我真的不知道问题可能出在哪里。感谢@bassplayer7 帮助我。
  • 这很奇怪。我会继续考虑的。你使用 XDebug 吗?你有没有经历过它?
  • stackoverflow.com/questions/25654270/…你注销再登录了吗?

标签: symfony fosuserbundle


【解决方案1】:

退出并重新登录将解决问题。

【讨论】:

  • 谢谢你,这对我有用,我很生气:)
  • 没问题。会话存储角色信息,因此必须清除它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-26
  • 2017-12-24
  • 1970-01-01
  • 1970-01-01
  • 2015-12-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多