Skip to content

Commit

Permalink
Use new IS_* attributes in the expression language functions
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterj committed Feb 25, 2020
1 parent ff9b8da commit 3f0c599
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
Expand Up @@ -25,21 +25,21 @@ public function getFunctions()
{
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') {
Expand All @@ -49,9 +49,9 @@ public function getFunctions()
}),

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');
}),
];
}
Expand Down
Expand Up @@ -21,6 +21,7 @@
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;

Expand All @@ -35,11 +36,10 @@ public function testIsAuthenticated($token, $expression, $result)
$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;

Expand Down

0 comments on commit 3f0c599

Please sign in to comment.