Symfony - Voter always the same object received

本秂侑毒 提交于 2019-12-02 09:34:43

问题


I have implemented the following Voter

Service definition

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

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;
}
#...

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()).

What can be the reason ?


回答1:


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.

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



来源:https://stackoverflow.com/questions/17528191/symfony-voter-always-the-same-object-received

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!