【问题标题】:Just checking: abstain vote from Symfony AbstractVoter isGranted只是检查:Symfony AbstractVoter isGranted 的弃权票
【发布时间】:2015-01-30 09:50:14
【问题描述】:

我正在开发一个 Symfony2 应用程序,我们想在其中引入 Security Voters。 DX 倡议(杰伊!)为我们带来了这种机制的the simpler version,所以我想使用它。关于\Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter 的使用,唯一让我有点困扰的是,当我仅仅实现抽象的isGranted 方法时,我不能再投弃权票了。我想为我们的应用程序更改它,但非常感谢您从安全角度对此事提出意见。

明确地说,在我们的应用程序中,我们将使用 unanimous 访问决策管理器策略 - 简而言之:您至少需要一个 ACCESS_GRANTED 而不需要 ACCESS_DENIED - 因为我们稍后可能想要引入选民来禁止IP 地址或其他恶作剧。

如下面相关代码所示,只要AbstractVoter 的实现支持属性,投票将默认为Denied

public function vote(TokenInterface $token, $object, array $attributes)
{
    if (!$object || !$this->supportsClass(get_class($object))) {
        return self::ACCESS_ABSTAIN;
    }

    // abstain vote by default in case none of the attributes are supported
    $vote = self::ACCESS_ABSTAIN;

    foreach ($attributes as $attribute) {
        if (!$this->supportsAttribute($attribute)) {
            continue;
        }

        // as soon as at least one attribute is supported, default is to deny access
        $vote = self::ACCESS_DENIED;

        if ($this->isGranted($attribute, $object, $token->getUser())) {
            // grant access as soon as at least one voter returns a positive response
            return self::ACCESS_GRANTED;
        }
    }

    return $vote;
}

我想做的是用下面的代码覆盖它

public function vote(TokenInterface $token, $object, array $attributes)
{
    if (!$object || !$this->supportsClass(get_class($object))) {
        return self::ACCESS_ABSTAIN;
    }

    $vote = self::ACCESS_ABSTAIN;

    foreach ($attributes as $attribute) {
        if (!$this->supportsAttribute($attribute)) {
            continue;
        }

        // This is where we differ from SymfonyAbstractVoter. Only if there is an outspoken yes or no, a vote is cast
        // When returning null, it will still abstain
        $grant = $this->isGranted($attribute, $object, $token->getUser());
        if ($grant === true) {
            return self::ACCESS_GRANTED;
        }
        if($grant === false) {
            return self::ACCESS_DENIED;
        }

    }

    return $vote;
}

你怎么看?当从 isGranted 返回 null 时返回 ACCESS_ABSTAIN 是否安全,并且只有在选民返回 true 或 false 时才投 ACCESS_GRANTEDACCESS_DENIED


我为什么要这样做?仍然使用AbstractVoter(因为,是的,我是一个懒惰的开发人员)但我的关注点分开。

假设我为帖子设置了一个投票者PostVoter,这将允许我编辑自己的帖子。然后它将投射ACCESS_GRANTED。但只要我不拥有这个职位,这个选民真的不在乎:它应该只是投一个ACCESS_ABSTAIN。 稍后它可能会遇到AdminVoter,它仍然会投射ACCESS_GRANTED。 然而,当使用unanimous 访问决策管理器策略时,如果这个PostVoter 已经投了一个ACCESS_DENIED,它将永远不会到达任何其他选民。

如果不覆盖 vote 方法,我现在无法在 AbstractVoter 中投射 ACCESS_ABSTAIN,而我认为更简单的选民的全部意义在于,让它更简单:)

【问题讨论】:

    标签: php security symfony


    【解决方案1】:

    AbstractVoter 类是大多数用例的简单引导实现,它模拟了internal voters behave 的方式,因此一旦您觉得您的解决方案与基本案例不同,您应该放心用你自己的方法覆盖。

    【讨论】:

      猜你喜欢
      • 2019-03-07
      • 1970-01-01
      • 2015-10-04
      • 1970-01-01
      • 2018-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-06
      相关资源
      最近更新 更多