使用Symfony获取未登录用户的安全令牌

时间:2012-08-09 16:00:04

标签: php symfony

如何为任何用户获取安全令牌,而不仅仅是当前登录的用户?

我希望能够对从数据库中提取的用户调用isGranted()

3 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分: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;
        }
}

答案 2 :(得分:1)

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

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