选民不在Symfony 3.3.4工作

时间:2017-08-01 06:37:50

标签: php symfony symfony-3.3

我有一个在控制器中完美运行的选民但是当我尝试在服务中使用它时,它总是返回true,尽管它有一个"返回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!');
}

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

好的,我找到了答案。

这个......之间的区别:

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

......这......:

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

...是他们告诉你结果的方式。

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

我没有意识到这一点:)

谢谢大家的帮助。

答案 1 :(得分:0)

    $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或扩展Voter,这使得创建选民变得更加容易。你呢?

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