2012-08-09 162 views

回答

5

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 */ } 
} 
+0

谢谢,它工作。然而,在你的例子中,你有一个错字,我错过了两个小时:你的第一个setToken()使用$ previousToken作为参数,而不是$ token:D – 2012-08-10 10:30:19

+0

顺便说一句,这对我来说很合理,因为user-> getRoles()不是真正的角色。我真的认为它已经坏了,但这就是应用程序的工作原理,我不能说“嘿,让我们改变安全防火墙!”。尤其是我实习的最后一天:p – 2012-08-10 10:33:41

+1

这个命题的问题在于它不依赖security.yml中定义的角色层次结构。查找用户是否具有角色A与查找用户是否被授予角色(将角色层次结构考虑在内)不一样。后者更困难。我正在寻找这样的解决方案... – 2013-04-12 13:09:14

3

我有同样的要求,而前。所以我自己实现了。由于您需要容器中的层次结构信息,因此它不是 可能 建议使用此功能扩展用户实体。

// 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; 
     } 
} 
+0

注意:您可以使用@ security.role_hierarchy:getReachableRoles服务方法来解析继承角色 – delf 2016-11-07 13:15:31

1

我认为最好的方法是手动调用AccessDecisionManager - 像$securityContext->isGranted()确实很好,但当前登录的用户。如果您使用Symfony选民来确定访问权限,这也很好。

$token = new UsernamePasswordToken($userObject, 'none', 'main', $userObject->getRoles()); 
$hasAccess = $this->get('security.access.decision_manager')->decide($token, array('voter'), $optionalObjectToCheckAccessTo);