4

我正在使用 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;
    }
}

您可以看到有一个公共函数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应该在哪里依赖 Voter 实现。所以我没有看到循环引用在哪里。

还有其他方法可以从 VoterB 呼叫 VoterA 吗?

4

1 回答 1

2

要从中引用VoterOneVoterTwo您可以注入 inAuthorizationCheckerInterface然后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 的选民)。然后可以通过附加条件来扩展。

于 2019-01-09T15:59:38.417 回答