gpt4 book ai didi

symfony - 将对象传递给自定义选民?

转载 作者:行者123 更新时间:2023-12-02 07:02:40 26 4
gpt4 key购买 nike

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

$page = new Page();

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

看起来 vote 方法应该接受它,但是,当我在 $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 }

我哪里错了?

感谢任何建议。

谢谢

最佳答案

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

事实是框架本身(或 bundle f.e)根据请求调用 isGranted。

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

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

关于symfony - 将对象传递给自定义选民?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17706428/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com