选民不在 Symfony 3.3.4 中工作

Voter not working in Symfony 3.3.4

我有一个在控制器中完美运行的投票器,但是当我尝试在服务中使用它时,它总是 return 正确,尽管它有一个 "return false" 语句。

我看到的唯一区别是我对它的称呼方式。

在控制器中我是这样使用的:

    $this->denyAccessUnlessGranted('ver', $menu);

在服务中我这样称呼它:

    $this->authorizationChecker->isGranted('ver', $menu);

在服务中,我注入了 AuthorizationChecker 并且它有效,但似乎 运行 其他选民(我只有一个)。

在 "security.yml" 我有这个:

    access_decision_manager:
        strategy: unanimous

选民代码:

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

    $usuario = $token->getUser();

    if (!$usuario instanceof Usuarios) {
        return false;
    }

    /** @var Menu $menu */
    $menu = $subject;


    switch ($attribute) {
        case self::VER:
            return false;
        case self::EDITAR:
            return false;
        case self::IMPRIMIR:
            return false;
    }

    throw new \LogicException('This code should not be reached!');
}

有人能帮帮我吗?

    $this->denyAccessUnlessGranted('ver', $menu);

    $this->authorizationChecker->isGranted('ver', $menu);

会做同样的工作。

请检查服务和控制器中$menu的内容。

$this->denyAccessUnlessGranted($attributes, $object = null, $message = 'Access Denied.')$this->container->get('security.authorization_checker')->isGranted($attributes, $object)

的快捷方式

您的自定义选民需要实现 VoterInterface 或扩展选民,这使得创建选民更加容易。你有吗?

检查官方文档以检查您的选民的良好实施。 https://symfony.com/doc/current/security/voters.html

好的,我找到答案了。

这两者的区别...:[=​​12=]

    $this->denyAccessUnlessGranted('ver', $menu);

...还有这个...:[=​​12=]

    $this->authorizationChecker->isGranted('ver', $menu);

... 是他们告知您结果的方式。

第一个语句抛出 DenyAccessException 但第二个语句 returns 一个布尔值(不抛出异常)。

我没有意识到:)

谢谢大家的帮助。