Domanda

I have a very strange problem in one of my Symfony2's service. I want to get the current user in the constructor of my service but the SecurityContext->getToken return false!

That is my service constructor :

public function __construct(Registry $doctrine, SecurityContext $context){
    $this->doctrine = $doctrine;
    $this->context = $context;
    if(!$this->context->getToken()){
        echo "Il y a une merde au niveau du token !";
    }
    $this->my_auth = $this->getMyAuth();
}

And here the service declaration in my service.yml

security.autorisation:
    class: Nsi\SecurityBundle\Service\Autorisation
    arguments: [@doctrine,@security.context]

But when I go on my admin page, the message "Il y a une merde au niveau du Token" appear. the most surprising is that my bottom symfony toolbar show me my login !

My project is a Symfony 2.3.11 project

Thinks for your help

È stato utile?

Soluzione

Don't call it from service constructor, because service initialization called earlier than authentication. That's why you have no token set.

Altri suggerimenti

Try:

public function __construct(Registry $doctrine, SecurityContext $context){
    $this->doctrine = $doctrine;
    $this->context = $context;
    if(!$this->context->isGranted('ROLE_USER'))){
       echo "Il y a une merde au niveau du token !";
    }
    $this->my_auth = $this->getMyAuth();
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top