Reputation: 4150
I am using a statement from a Symfony2 app in Symfony4:
$securityContext = $this->container->get('security.token_storage');
if($securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED') ){
. . .
}
I get always error:
Attempted to call an undefined method named "isGranted" of class "Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage
what I am missing?
Upvotes: 1
Views: 1709
Reputation: 650
for SF4, according to the docs:
public function hello($name)
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
// ...
}
You have to use security.authorization_checker service. And the code above is the same as:
public function hello($name, AuthorizationCheckerInterface $authChecker)
{
if (false === $authChecker->isGranted('ROLE_ADMIN')) {
throw new AccessDeniedException('Unable to access this page!');
}
// ...
}
check docs here https://symfony.com/doc/4.0/security.html#securing-controllers-and-other-code
Upvotes: 0