Symfony2:从另一个选民呼叫选民

Symfony2: Call Voter from another Voter

我正在使用 Voters 来限制对 REST API 中实体的访问。

第 1 步

考虑这个限制用户访问博客帖子的选民:

class BlogPostVoter extends Voter
{
    public function __construct(AccessDecisionManagerInterface $decisionManager)
    {
        $this->decisionManager = $decisionManager;
    }

    /**
     * Determines if the attribute and subject are supported by this voter.
     *
     * @param string $attribute An attribute
     * @param int $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
     *
     * @return bool True if the attribute and subject are supported, false otherwise
     */
    protected function supports($attribute, $subject)
    {
        if (!in_array($attribute, $this->allowedAttributes)) {
            return false;
        }
        if (!$subject instanceof BlogPost) {
            return false;
        }

        return true;
    }

    /**
     * Perform a single access check operation on a given attribute, subject and token.
     *
     * @param string $attribute
     * @param mixed $subject
     * @param TokenInterface $token
     * @return bool
     * @throws \Exception
     */
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
    {
        return $this->canUserAccess($attribute, $subject, $token);
    }

    public function canUserAccess($attribute, $subject, TokenInterface $token) {
        if ($this->decisionManager->decide($token, array('ROLE_SUPPORT', 'ROLE_ADMIN'))) {
            return true;
        } 

        //other logic here omitted ...

        return false;
    }
}

您可以看到有一个 public 函数 canUserAccess 来确定是否允许用户查看 BlogPost。这一切都很好。

第 2 步

现在我有另一个选民检查其他东西,但也需要为 BlogPost 检查同样的逻辑。我的想法是:

所以我想我会像这样将 BlogPostVoter 注入我的另一个选民:

class SomeOtherVoter extends Voter
{
    public function __construct(BlogPostVoter $blogPostVoter)
    {
        $this->decisionManager = $decisionManager;
    }

    ...

    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
    {
        //other logic

        if ($this->blogPostVoter->canUserAccess($attribute, $subject, $token))    {
           return true;
        }

        return false;
    }
}

问题

当我这样做时,我得到以下错误,同时使用 setter 和构造函数注入:

检测到服务 "security.access.decision_manager" 的循环引用,路径:"security.access.decision_manager"

我看不出 security.access.decision_manager 应该取决于选民实施的地方。所以我没有看到循环引用在哪里。

还有其他方法可以从 VoterB 调用 VoterA 吗?

要从 VoterTwo 引用 VoterOne,您可以将 AuthorizationCheckerInterface 注入 VoterTwo,然后调用 ->isGranted('ONE')。其中 ONE 是 VoterOne.

支持的属性

喜欢:

class VoterTwo extends Voter
{
    private $authorizationChecker;

    public function __construct(AuthorizationCheckerInterface $authorizationChecker)
    {
        $this->authorizationChecker = $authorizationChecker;
    }

    protected function supports($attribute, $subject)
    {
        return in_array($attribute, ['TWO']);
    }

    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
    {
        return $this->authorizationChecker->isGranted('ONE', $subject);
    }
}

在此示例中,VoterTwo 只是将请求重定向到 VoterOne(或支持属性 ONE 的选民)。然后可以通过附加条件对其进行扩展。