Skip to content

Commit 00631cf

Browse files
committed
[FrameworkBundle][SecurityBundle] Moved security expression providers pass logic to SecurityBundle
1 parent b560883 commit 00631cf

File tree

9 files changed

+161
-5
lines changed

9 files changed

+161
-5
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Allowed configuring taggable cache pools via a new `framework.cache.pools.tags` option (bool|service-id)
88
* Deprecated auto-injection of the container in AbstractController instances, register them as service subscribers instead
9+
* Deprecated the processing of services tagged `security.expression_language_provider` in favor of the new SecurityBundle pass
910

1011
4.1.0
1112
-----

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddExpressionLanguageProvidersPass.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
1313

14+
use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\AddExpressionLanguageProvidersPass as SecurityExpressionLanguageProvidersPass;
1415
use Symfony\Component\DependencyInjection\ContainerBuilder;
1516
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1617
use Symfony\Component\DependencyInjection\Reference;
@@ -22,6 +23,17 @@
2223
*/
2324
class AddExpressionLanguageProvidersPass implements CompilerPassInterface
2425
{
26+
private $handleSecurityLanguageProviders;
27+
28+
public function __construct(bool $handleSecurityLanguageProviders = true)
29+
{
30+
if ($handleSecurityLanguageProviders) {
31+
@trigger_error(sprintf('Registering services tagged with "security.expression_language_provider" with "%s" is deprecated since Symfony 4.2, use the "%s" instead.', __CLASS__, SecurityExpressionLanguageProvidersPass::class), E_USER_DEPRECATED);
32+
}
33+
34+
$this->handleSecurityLanguageProviders = $handleSecurityLanguageProviders;
35+
}
36+
2537
/**
2638
* {@inheritdoc}
2739
*/
@@ -36,7 +48,7 @@ public function process(ContainerBuilder $container)
3648
}
3749

3850
// security
39-
if ($container->has('security.expression_language')) {
51+
if ($this->handleSecurityLanguageProviders && $container->has('security.expression_language')) {
4052
$definition = $container->findDefinition('security.expression_language');
4153
foreach ($container->findTaggedServiceIds('security.expression_language_provider', true) as $id => $attributes) {
4254
$definition->addMethodCall('registerProvider', array(new Reference($id)));

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function build(ContainerBuilder $container)
101101
$this->addCompilerPassIfExists($container, AddConsoleCommandPass::class, PassConfig::TYPE_BEFORE_REMOVING);
102102
$this->addCompilerPassIfExists($container, TranslatorPass::class);
103103
$container->addCompilerPass(new LoggingTranslatorPass());
104-
$container->addCompilerPass(new AddExpressionLanguageProvidersPass());
104+
$container->addCompilerPass(new AddExpressionLanguageProvidersPass(false));
105105
$this->addCompilerPassIfExists($container, TranslationExtractorPass::class);
106106
$this->addCompilerPassIfExists($container, TranslationDumperPass::class);
107107
$container->addCompilerPass(new FragmentRendererPass());

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/AddExpressionLanguageProvidersPassTest.php

+47-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class AddExpressionLanguageProvidersPassTest extends TestCase
2222
public function testProcessForRouter()
2323
{
2424
$container = new ContainerBuilder();
25-
$container->addCompilerPass(new AddExpressionLanguageProvidersPass());
25+
$container->addCompilerPass(new AddExpressionLanguageProvidersPass(false));
2626

2727
$definition = new Definition('\stdClass');
2828
$definition->addTag('routing.expression_language_provider');
@@ -41,7 +41,7 @@ public function testProcessForRouter()
4141
public function testProcessForRouterAlias()
4242
{
4343
$container = new ContainerBuilder();
44-
$container->addCompilerPass(new AddExpressionLanguageProvidersPass());
44+
$container->addCompilerPass(new AddExpressionLanguageProvidersPass(false));
4545

4646
$definition = new Definition('\stdClass');
4747
$definition->addTag('routing.expression_language_provider');
@@ -58,6 +58,9 @@ public function testProcessForRouterAlias()
5858
$this->assertEquals(new Reference('some_routing_provider'), $calls[0][1][0]);
5959
}
6060

61+
/**
62+
* @group legacy
63+
*/
6164
public function testProcessForSecurity()
6265
{
6366
$container = new ContainerBuilder();
@@ -76,6 +79,9 @@ public function testProcessForSecurity()
7679
$this->assertEquals(new Reference('some_security_provider'), $calls[0][1][0]);
7780
}
7881

82+
/**
83+
* @group legacy
84+
*/
7985
public function testProcessForSecurityAlias()
8086
{
8187
$container = new ContainerBuilder();
@@ -94,4 +100,43 @@ public function testProcessForSecurityAlias()
94100
$this->assertEquals('registerProvider', $calls[0][0]);
95101
$this->assertEquals(new Reference('some_security_provider'), $calls[0][1][0]);
96102
}
103+
104+
/**
105+
* @group legacy
106+
*/
107+
public function testProcessIgnoreSecurity()
108+
{
109+
$container = new ContainerBuilder();
110+
$container->addCompilerPass(new AddExpressionLanguageProvidersPass(false));
111+
112+
$definition = new Definition('\stdClass');
113+
$definition->addTag('security.expression_language_provider');
114+
$container->setDefinition('some_security_provider', $definition->setPublic(true));
115+
116+
$container->register('security.expression_language', '\stdClass')->setPublic(true);
117+
$container->compile();
118+
119+
$calls = $container->getDefinition('security.expression_language')->getMethodCalls();
120+
$this->assertCount(0, $calls);
121+
}
122+
123+
/**
124+
* @group legacy
125+
*/
126+
public function testProcessIgnoreSecurityAlias()
127+
{
128+
$container = new ContainerBuilder();
129+
$container->addCompilerPass(new AddExpressionLanguageProvidersPass(false));
130+
131+
$definition = new Definition('\stdClass');
132+
$definition->addTag('security.expression_language_provider');
133+
$container->setDefinition('some_security_provider', $definition->setPublic(true));
134+
135+
$container->register('my_security.expression_language', '\stdClass')->setPublic(true);
136+
$container->setAlias('security.expression_language', 'my_security.expression_language');
137+
$container->compile();
138+
139+
$calls = $container->getDefinition('my_security.expression_language')->getMethodCalls();
140+
$this->assertCount(0, $calls);
141+
}
97142
}

src/Symfony/Bundle/SecurityBundle/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CHANGELOG
99
the token classes is deprecated. To use
1010
custom tokens extend the existing `Symfony\Component\Security\Core\Authentication\Token\AnonymousToken`
1111
or `Symfony\Component\Security\Core\Authentication\Token\RememberMeToken`.
12+
* Added `Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\AddExpressionLanguageProvidersPass`
1213

1314
4.1.0
1415
-----
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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+
namespace Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\ContainerBuilder;
15+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16+
use Symfony\Component\DependencyInjection\Reference;
17+
18+
/**
19+
* Registers the expression language providers.
20+
*
21+
* @author Fabien Potencier <fabien@symfony.com>
22+
*/
23+
class AddExpressionLanguageProvidersPass implements CompilerPassInterface
24+
{
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function process(ContainerBuilder $container)
29+
{
30+
if ($container->has('security.expression_language')) {
31+
$definition = $container->findDefinition('security.expression_language');
32+
foreach ($container->findTaggedServiceIds('security.expression_language_provider', true) as $id => $attributes) {
33+
$definition->addMethodCall('registerProvider', array(new Reference($id)));
34+
}
35+
}
36+
}
37+
}

src/Symfony/Bundle/SecurityBundle/SecurityBundle.php

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\SecurityBundle;
1313

14+
use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\AddExpressionLanguageProvidersPass;
1415
use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\RegisterCsrfTokenClearingLogoutHandlerPass;
1516
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\JsonLoginFactory;
1617
use Symfony\Component\HttpKernel\Bundle\Bundle;
@@ -57,6 +58,7 @@ public function build(ContainerBuilder $container)
5758

5859
$extension->addUserProviderFactory(new InMemoryFactory());
5960
$extension->addUserProviderFactory(new LdapFactory());
61+
$container->addCompilerPass(new AddExpressionLanguageProvidersPass());
6062
$container->addCompilerPass(new AddSecurityVotersPass());
6163
$container->addCompilerPass(new AddSessionDomainConstraintPass(), PassConfig::TYPE_BEFORE_REMOVING);
6264
$container->addCompilerPass(new RegisterCsrfTokenClearingLogoutHandlerPass());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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+
namespace Symfony\Bundle\SecurityBundle\Tests\DependencyInjection\Compiler;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\AddExpressionLanguageProvidersPass;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Definition;
18+
use Symfony\Component\DependencyInjection\Reference;
19+
20+
class AddExpressionLanguageProvidersPassTest extends TestCase
21+
{
22+
public function testProcessForSecurity()
23+
{
24+
$container = new ContainerBuilder();
25+
$container->addCompilerPass(new AddExpressionLanguageProvidersPass());
26+
27+
$definition = new Definition('\stdClass');
28+
$definition->addTag('security.expression_language_provider');
29+
$container->setDefinition('some_security_provider', $definition->setPublic(true));
30+
31+
$container->register('security.expression_language', '\stdClass')->setPublic(true);
32+
$container->compile();
33+
34+
$calls = $container->getDefinition('security.expression_language')->getMethodCalls();
35+
$this->assertCount(1, $calls);
36+
$this->assertEquals('registerProvider', $calls[0][0]);
37+
$this->assertEquals(new Reference('some_security_provider'), $calls[0][1][0]);
38+
}
39+
40+
public function testProcessForSecurityAlias()
41+
{
42+
$container = new ContainerBuilder();
43+
$container->addCompilerPass(new AddExpressionLanguageProvidersPass());
44+
45+
$definition = new Definition('\stdClass');
46+
$definition->addTag('security.expression_language_provider');
47+
$container->setDefinition('some_security_provider', $definition->setPublic(true));
48+
49+
$container->register('my_security.expression_language', '\stdClass')->setPublic(true);
50+
$container->setAlias('security.expression_language', 'my_security.expression_language');
51+
$container->compile();
52+
53+
$calls = $container->getDefinition('my_security.expression_language')->getMethodCalls();
54+
$this->assertCount(1, $calls);
55+
$this->assertEquals('registerProvider', $calls[0][0]);
56+
$this->assertEquals(new Reference('some_security_provider'), $calls[0][1][0]);
57+
}
58+
}

src/Symfony/Bundle/SecurityBundle/composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"conflict": {
4747
"symfony/var-dumper": "<3.4",
4848
"symfony/event-dispatcher": "<3.4",
49-
"symfony/framework-bundle": "<4.1.1",
49+
"symfony/framework-bundle": "<4.2",
5050
"symfony/console": "<3.4"
5151
},
5252
"autoload": {

0 commit comments

Comments
 (0)