Dan*_*Dan 2 symfony symfony-security
我一直在阅读有关在Symfony 2中创建自定义选民的信息.根据这个页面,可以将一个对象传递给securitycontext的isGranted方法,我在自己的控制器中完成了这个方法:
$page = new Page();
if ( ! $securityContext->isGranted('CONTENT_CREATE', $page)) {
throw new AccessDeniedException('Fail');
}
Run Code Online (Sandbox Code Playgroud)
看起来像投票方法应该接受它,但是,当我在$ object参数上调用get_class而不是获取我的Page实体时,我得到:
Symfony的\分量\ HttpFoundation \请求
public function vote(TokenInterface $token, $object, array $attributes)
{
print_r(get_class($object)); die();
return VoterInterface::ACCESS_ABSTAIN;
}
Run Code Online (Sandbox Code Playgroud)
我的选民被定义为我的services.yml文件中的服务:
content_security.access.my_voter:
class: My\Bundle\Security\Authorization\Voter\MyVoter
arguments: ["@service_container"]
public: false
tags:
- { name: security.voter }
Run Code Online (Sandbox Code Playgroud)
我哪里错了?
任何建议表示赞赏
谢谢
每次注册的选民都会在调用isGranted时被调用.
事实是框架本身(或捆绑fe)调用isGranted请求.
您必须使用supportsClass,supportsAttribute,...以检查对象是否是您正在等待的对象,如果没有返回VoterInterface :: ABSTAIN值.
看一下现有的实现(在框架本身(如RoleVoter)或这里:https://github.com/KnpLabs/KnpRadBundle/blob/develop/Security/Voter/IsOwnerVoter.php#L35-L45