Use new IS_* attributes in the expression language functions

This commit is contained in:
Wouter J 2020-02-25 00:21:47 +01:00 committed by Wouter de Jong
parent ff9b8dae88
commit 3f0c599289
2 changed files with 10 additions and 10 deletions

View File

@ -25,21 +25,21 @@ class ExpressionLanguageProvider implements ExpressionFunctionProviderInterface
{
return [
new ExpressionFunction('is_anonymous', function () {
return '$trust_resolver->isAnonymous($token)';
return '$token && $auth_checker->isGranted("IS_ANONYMOUS")';
}, function (array $variables) {
return $variables['trust_resolver']->isAnonymous($variables['token']);
return $variables['token'] && $variables['auth_checker']->isGranted('IS_ANONYMOUS');
}),
new ExpressionFunction('is_authenticated', function () {
return '$token && !$trust_resolver->isAnonymous($token)';
return '$token && !$auth_checker->isGranted("IS_ANONYMOUS")';
}, function (array $variables) {
return $variables['token'] && !$variables['trust_resolver']->isAnonymous($variables['token']);
return $variables['token'] && !$variables['auth_checker']->isGranted('IS_ANONYMOUS');
}),
new ExpressionFunction('is_fully_authenticated', function () {
return '$trust_resolver->isFullFledged($token)';
return '$token && $auth_checker->isGranted("IS_AUTHENTICATED_FULLY")';
}, function (array $variables) {
return $variables['trust_resolver']->isFullFledged($variables['token']);
return $variables['token'] && $variables['auth_checker']->isGranted('IS_AUTHENTICATED_FULLY');
}),
new ExpressionFunction('is_granted', function ($attributes, $object = 'null') {
@ -49,9 +49,9 @@ class ExpressionLanguageProvider implements ExpressionFunctionProviderInterface
}),
new ExpressionFunction('is_remember_me', function () {
return '$trust_resolver->isRememberMe($token)';
return '$token && $auth_checker->isGranted("IS_REMEMBERED")';
}, function (array $variables) {
return $variables['trust_resolver']->isRememberMe($variables['token']);
return $variables['token'] && $variables['auth_checker']->isGranted('IS_REMEMBERED');
}),
];
}

View File

@ -21,6 +21,7 @@ use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManager;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
use Symfony\Component\Security\Core\Authorization\ExpressionLanguage;
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
use Symfony\Component\Security\Core\Authorization\Voter\RoleVoter;
use Symfony\Component\Security\Core\User\User;
@ -35,11 +36,10 @@ class ExpressionLanguageTest extends TestCase
$trustResolver = new AuthenticationTrustResolver();
$tokenStorage = new TokenStorage();
$tokenStorage->setToken($token);
$accessDecisionManager = new AccessDecisionManager([new RoleVoter()]);
$accessDecisionManager = new AccessDecisionManager([new RoleVoter(), new AuthenticatedVoter($trustResolver)]);
$authChecker = new AuthorizationChecker($tokenStorage, $this->getMockBuilder(AuthenticationManagerInterface::class)->getMock(), $accessDecisionManager);
$context = [];
$context['trust_resolver'] = $trustResolver;
$context['auth_checker'] = $authChecker;
$context['token'] = $token;