Question

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 ?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top