将对象传递给自定义投票者? [英] Passing object to custom voter?

查看:111
本文介绍了将对象传递给自定义投票者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此页面,可以将对象传递给securitycontext的isGranted方法,该方法已在我自己的控制器中完成:

I've been reading up about creating custom voters in Symfony 2. According to this page, it is possible to pass an object to the isGranted method of the securitycontext, which I have done in my own controller:

$page = new Page();

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

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

It looks like the vote method should be accepting it, however, when I call get_class on the $object parameter, instead of getting my Page entity, I get:

Symfony \ Component \ HttpFoundation \ Request

Symfony\Component\HttpFoundation\Request

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

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

My voter is defined as a service in my services.yml file:

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

我要去哪里错了?

任何建议表示赞赏.

谢谢

推荐答案

当某些呼叫被授予isgranted时,将调用每个注册的选民.

Every registered Voters is called when something calls isGranted.

事实是框架本身(或捆绑包)在请求中被授予.

The fact is that the framework itself (or a bundle f.e) calls isGranted on the request.

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

You have to use supportsClass, supportsAttribute, ... in order to check if the object is the one you're waiting for, and if not return a VoterInterface::ABSTAIN value.

看看现有的实现(在框架本身(例如RoleVoter)中)或在这里:

Take a look at existing implementations (in the framework itself (like RoleVoter) or here: https://github.com/KnpLabs/KnpRadBundle/blob/develop/Security/Voter/IsOwnerVoter.php#L35-L45

这篇关于将对象传递给自定义投票者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆