回答
isGranted()
来自安全服务获取用户调用isGranted(),所以这将是硬/不需要用它来获取角色,不调整状态的会议。
不要误会我的意思,这是绝对有可能...这将工作,例如:
public function strangeAction()
{
// Get your User, however you normally get it
$user = $userRepository->find($id);
// Save the current token so you can put it back later
$previousToken = $this->get("security.context")->getToken();
// Create a new token
$token = new UsernamePasswordToken($user, null, "main", $user->getRoles());
// Update the security context with the new token
$this->get("security.context")->setToken($token);
// Now you have access to isGranted()
if ($this->get("security.context")->isGranted("ROLE_SOMETHING"))
{ /* Do something here */ }
// Don't forget to reset the token!
$this->get("security.context")->setToken($previousToken);
}
...但真的是没有意义的。
实际上,你不需要令牌。这样做的更好的方法是添加一个isGranted()
方法到您的用户实体:
// Namespace\YourBundle\Entity\User.php
class User
{
...
public function isGranted($role)
{
return in_array($role, $this->getRoles());
}
...
}
现在你可以得到这些角色在你的控制器:
public function notSoStrangeAction()
{
// Get your User, however you normally get it
$user = $userRepository->find($id);
// Find out if that User has a Role associated to it
if ($user->isGranted("ROLE_SOMETHING"))
{ /* Do something here */ }
}
谢谢,它工作。然而,在你的例子中,你有一个错字,我错过了两个小时:你的第一个setToken()使用$ previousToken作为参数,而不是$ token:D – 2012-08-10 10:30:19
顺便说一句,这对我来说很合理,因为user-> getRoles()不是真正的角色。我真的认为它已经坏了,但这就是应用程序的工作原理,我不能说“嘿,让我们改变安全防火墙!”。尤其是我实习的最后一天:p – 2012-08-10 10:33:41
这个命题的问题在于它不依赖security.yml中定义的角色层次结构。查找用户是否具有角色A与查找用户是否被授予角色(将角色层次结构考虑在内)不一样。后者更困难。我正在寻找这样的解决方案... – 2013-04-12 13:09:14
我有同样的要求,而前。所以我自己实现了。由于您需要容器中的层次结构信息,因此它不是
可能
建议使用此功能扩展用户实体。
// first check if the role is inside the user roles of the user
// if not then check for each user role if it is a master role of the check role
public function isGranted($user, $checkrole){
$userroles = $user->getRoles();
if (in_array($checkrole, $userroles)){return true;}
foreach ($userroles as $userrole){
if ($this->roleOwnsRole($userrole, $checkrole)){return true;}
}
return false;
}
// recursively loop over the subroles of the master to check if any of them are
// the suggested slave role. If yes then the masterrole is a master and has
// the same grants as the slave.
private function roleOwnsRole($masterRole, $slaveRole, $checkvalidityroles=true, $hierarchy=null)
{
if ($hierarchy===null){$hierarchy = $this->container->getParameter('security.role_hierarchy.roles');}
if ($masterRole === $slaveRole){ return false; }
if($checkvalidityroles && (!array_key_exists($masterRole, $hierarchy) || !array_key_exists($slaveRole, $hierarchy))){ return false; }
$masterroles = $hierarchy[$masterRole];
if(in_array($slaveRole, $masterroles)){
return true;
}else{
foreach($masterroles as $masterrolerec){
if ($this->roleOwnsRole($masterrolerec, $slaveRole, false, $hierarchy)){return true;}
}
return false;
}
}
注意:您可以使用@ security.role_hierarchy:getReachableRoles服务方法来解析继承角色 – delf 2016-11-07 13:15:31
我认为最好的方法是手动调用AccessDecisionManager
- 像$securityContext->isGranted()
确实很好,但当前登录的用户。如果您使用Symfony选民来确定访问权限,这也很好。
$token = new UsernamePasswordToken($userObject, 'none', 'main', $userObject->getRoles());
$hasAccess = $this->get('security.access.decision_manager')->decide($token, array('voter'), $optionalObjectToCheckAccessTo);
- 1. 获取Twitter OAuth令牌和秘密,但未使用户使用Twitter登录
- 2. 获取用户登录访问令牌以使用YouTube API
- 3. LARAVEL - 获取CSRF令牌用我的api登录用户
- 4. 使用Python登录,以安全的网站与令牌
- 5. Symfony 2获取登录用户列表
- 6. 如何获取Windows服务的LogOn用户的安全令牌?
- 7. 当用户未登录时使用表单令牌
- 8. 如何使用UDID或设备令牌进行安全登录?
- 9. 如何获取叽叽喳喳oAuth令牌的登录用户
- 10. 登录用户的firebase用户安全
- 11. 使用sha256登录令牌
- 12. Facebook SDK - 在用户登录时获取访问令牌
- 13. 获取访问令牌,无需用户登录
- 14. React Redux - 登录后获取用户和授权令牌
- 15. Symfony登录用户
- 16. IsInRole获取新的安全令牌
- 17. 获取使用用户令牌与C++
- 18. 如何使用用户令牌获取应用访问令牌
- 19. Google登录使用新的GoogleSignInOptions获取访问令牌
- 20. 获取否。当前登录的用户在春季安全
- 21. 获取已登录用户的Grails弹簧安全性
- 22. 如何使用httpwebrequest登录Facebook并获取用户访问令牌?
- 23. 春季安全用令牌登录到网络
- 24. 用户在令牌上登录
- 25. fosuserbundle和用户在安全令牌
- 26. Facebook用户访问令牌安全
- 27. 安全存储用户凭证令牌
- 28. Identity Server:使用API获取令牌并避免身份登录
- 29. 如何使用外部登录从Web API 2获取令牌
- 30. 使用Android登录获取Google OAuth令牌
您是否将用户的角色存储在数据库中? – richsage 2012-08-09 17:15:20