Skip to content

Commit e38fd98

Browse files
committed
Use framework expression language service instead of custom one
1 parent bf1cb5e commit e38fd98

File tree

7 files changed

+55
-14
lines changed

7 files changed

+55
-14
lines changed

src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
use Symfony\Component\DependencyInjection\ContainerBuilder;
3939
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
4040
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
41-
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
4241
use Symfony\Component\Finder\Finder;
4342
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
4443
use Symfony\Component\Messenger\MessageBusInterface;
@@ -120,9 +119,6 @@ public function load(array $configs, ContainerBuilder $container)
120119

121120
$bundles = $container->getParameter('kernel.bundles');
122121
if (isset($bundles['SecurityBundle'])) {
123-
if (class_exists(ExpressionLanguage::class)) {
124-
$loader->load('security_expression_language.xml');
125-
}
126122
$loader->load('security.xml');
127123
}
128124

src/Bridge/Symfony/Bundle/Resources/config/security.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
66

77
<services>
8+
<service id="api_platform.security.expression_language" alias="security.expression_language" />
9+
810
<service id="api_platform.security.resource_access_checker" class="ApiPlatform\Core\Security\ResourceAccessChecker" public="false">
911
<argument type="service" id="api_platform.security.expression_language" on-invalid="null" />
1012
<argument type="service" id="security.authentication.trust_resolver" on-invalid="null" />
@@ -21,6 +23,10 @@
2123
<!-- This listener must be executed only when the current object is available -->
2224
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="1" />
2325
</service>
26+
27+
<service id="api_platform.security.expression_language_provider" class="ApiPlatform\Core\Security\Core\Authorization\ExpressionLanguageProvider" public="false">
28+
<tag name="security.expression_language_provider" />
29+
</service>
2430
</services>
2531

2632
</container>

src/Bridge/Symfony/Bundle/Resources/config/security_expression_language.xml

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Security\Core\Authorization;
15+
16+
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
17+
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
18+
19+
/**
20+
* Registers API Platform's Expression Language functions.
21+
*
22+
* @author Yanick Witschi <[email protected]>
23+
*/
24+
final class ExpressionLanguageProvider implements ExpressionFunctionProviderInterface
25+
{
26+
public function getFunctions(): array
27+
{
28+
return [
29+
new ExpressionFunction('is_granted', function ($attributes, $object = 'null') {
30+
return sprintf('$auth_checker->isGranted(%s, %s)', $attributes, $object);
31+
}, function (array $variables, $attributes, $object = null) {
32+
return $variables['auth_checker']->isGranted($attributes, $object);
33+
}),
34+
];
35+
}
36+
}

src/Security/ExpressionLanguage.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace ApiPlatform\Core\Security;
1515

16+
use Psr\Cache\CacheItemPoolInterface;
1617
use Symfony\Component\Security\Core\Authorization\ExpressionLanguage as BaseExpressionLanguage;
1718

1819
/**
@@ -25,6 +26,16 @@
2526
*/
2627
class ExpressionLanguage extends BaseExpressionLanguage
2728
{
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function __construct(CacheItemPoolInterface $cache = null, array $providers = [])
33+
{
34+
@trigger_error('Using the ExpressionLanguage class directly is deprecated since API Platform 2.4 and will not be possible anymore in API Platform 3. Use the "api_platform.security.expression_language" service instead.', E_USER_DEPRECATED);
35+
36+
parent::__construct($cache, $providers);
37+
}
38+
2839
protected function registerFunctions()
2940
{
3041
parent::registerFunctions();

src/Security/ResourceAccessChecker.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace ApiPlatform\Core\Security;
1515

1616
use ApiPlatform\Core\Util\ClassInfoTrait;
17+
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
1718
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
1819
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
1920
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ public function testEnableSecurity()
354354
$containerBuilderProphecy->setDefinition('api_platform.security.resource_access_checker', Argument::type(Definition::class))->shouldBeCalled();
355355
$containerBuilderProphecy->setAlias(ResourceAccessCheckerInterface::class, 'api_platform.security.resource_access_checker')->shouldBeCalled();
356356
$containerBuilderProphecy->setDefinition('api_platform.security.listener.request.deny_access', Argument::type(Definition::class))->shouldBeCalled();
357-
$containerBuilderProphecy->setDefinition('api_platform.security.expression_language', Argument::type(Definition::class))->shouldBeCalled();
357+
$containerBuilderProphecy->setDefinition('api_platform.security.expression_language_provider', Argument::type(Definition::class))->shouldBeCalled();
358358
$containerBuilder = $containerBuilderProphecy->reveal();
359359

360360
$this->extension->load(self::DEFAULT_CONFIG, $containerBuilder);

0 commit comments

Comments
 (0)