isGranted返回FALSE,用户扩展FOSUserBundle

时间:2015-03-10 18:07:14

标签: symfony fosuserbundle

为了简化问题,我没有在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'上变为现实,而在所有其他方面都是假的:

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只需添加' ROLE_USER'在getRoles()方法中,因为它不存在于实际的Roles数组中。

1 个答案:

答案 0 :(得分:1)

重新登录并重新登录将解决此问题。

相关问题