本文介绍了Symfony-选民总是收到相同的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了以下选民

服务定义

security.access.company_voter:
    class:      Application\...\CompanyVoter
    public:     false
    tags:
       - { name: security.voter }

投票 Application/.../CompanyVoter.php

#...
public function vote(TokenInterface $token, $object, array $attributes) 
{
    if ( !($this->supportsClass(get_class($object))) ) { # <- Problem here
        return VoterInterface::ACCESS_ABSTAIN;
    }

    foreach ($attributes as $attribute) {
        if ( !$this->supportsAttribute($attribute) ) {
            return VoterInterface::ACCESS_ABSTAIN;
        }
    }

    $user = $token->getUser();
    if ( !($user instanceof UserInterface) ) {
        return VoterInterface::ACCESS_DENIED;
    }

    if ( $user->getCompany() == $object->getCompany() ) {
        return VoterInterface::ACCESS_GRANTED;
    }

    return VoterInterface::ACCESS_ABSTAIN;
}
#...

但是,每次给选民打电话(除了第一个Symfony\Component\HttpFoundation\Request以外)都将Application\...\CompanyVoter的实例作为$object(vote()的第二个参数).

But every little call to the voter (except the first Symfony\Component\HttpFoundation\Request) is giving an instance of Application\...\CompanyVoter as $object (2nd argument of vote()).

可能是什么原因?

推荐答案

我注意到实际上收到的对象总是NULL.
get_class(NULL)返回当前类.

I noticed that the object received is in fact always NULL.
get_class(NULL) returns the current class.

经过几天的搜索,我终于找到了那是哪里来的.

And after days of search, I finally found where did that come from.

这链接到SonataAdmin RoleHandler isGranted()实现.是的,我在使用SonataAdmin捆绑包中的选民.

This is linked to the SonataAdmin RoleHandler isGranted() implementation. Yeah I was using my voters from a SonataAdmin bundle.

这篇关于Symfony-选民总是收到相同的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 16:12