【问题标题】:Passing object to custom voter?将对象传递给自定义选民?
【发布时间】:2013-07-16 09:33:11
【问题描述】:

我一直在阅读有关在 Symfony 2 中创建自定义选民的信息。根据 this page,可以将对象传递给 securitycontext 的 isGranted 方法,我在自己的控制器中完成了这一点:

$page = new Page();

if ( ! $securityContext->isGranted('CONTENT_CREATE', $page)) {
    throw new AccessDeniedException('Fail');
}

看起来投票方法应该接受它,但是,当我在 $object 参数上调用 get_class 时,我得到的不是我的 Page 实体,而是:

Symfony\Component\HttpFoundation\Request

public function vote(TokenInterface $token, $object, array $attributes)
{   
    print_r(get_class($object)); die();
    return VoterInterface::ACCESS_ABSTAIN;
}

我的选民在我的 services.yml 文件中被定义为一个服务:

content_security.access.my_voter:
        class:      My\Bundle\Security\Authorization\Voter\MyVoter
        arguments:  ["@service_container"]
        public:     false
        tags:
            - { name: security.voter }

我哪里错了?

任何建议表示赞赏。

谢谢

【问题讨论】:

  • 嗯,我记得几天前有一个类似的问题......试图找到它。
  • 找到了 ...stackoverflow.com/questions/17530062/… ... Pierre 收到了选民类别,而不是 get_class() 的对象类别。这可能是相关的。您是否尝试转储对象?真的是请求还是 get_class 只是返回了错误的 FQCN。
  • Pierre 几分钟后在这里发布了第二个问题(几乎相同):stackoverflow.com/questions/17528191/…

标签: symfony symfony-security


【解决方案1】:

当调用 isGranted 时,每个注册的选民都会被调用。

事实上,框架本身(或包 f.e)在请求上调用 isGranted。

您必须使用 supportsClass、supportsAttribute... 来检查对象是否是您正在等待的对象,如果不是则返回 VoterInterface::ABSTAIN 值。

查看现有实现(在框架本身(如 RoleVoter)或此处:https://github.com/KnpLabs/KnpRadBundle/blob/develop/Security/Voter/IsOwnerVoter.php#L35-L45

【讨论】:

  • +1 我没想到选民也被要求请求页面...... facepalm - 这是正确的解释,感谢弗洛里安的启发:D
猜你喜欢
  • 2017-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多