문제

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

도움이 되었습니까?

해결책

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

다른 팁

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();
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top