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 ya 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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.