8000 Introduce a FirewallConfig class · symfony/symfony@ecc5e12 · GitHub
[go: up one dir, main page]

Skip to content

Commit ecc5e12

Browse files
committed
Introduce a FirewallConfig class
Add a FirewallConfig object, pass it to the FirewallContext Add FirewallContextTest & FirewallConfigTest Populate FirewallConfig definition from SecurityExtension Add missing anonymous listener in FirewallConfig::listenerConfigs Add a functional test Fabbot fixes Fix security option value Add ContextAwareFirewallMapInterface Remove bool casts from getters CS/Spelling Fixes Remove FirewallConfig::listenerConfigs in favor of FirewallConfig::listeners; Add FirewallConfig::allowAnonymous() Add allowAnonymous()/isSecurityEnabled, update comments Fabbot fixes Fix deprecation message
1 parent da3ff6c commit ecc5e12

File tree

9 files changed

+455
-10
lines changed

9 files changed

+455
-10
lines changed

src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public function load(array $configs, ContainerBuilder $container)
110110
'Symfony\Component\Security\Core\Authorization\AccessDecisionManager',
111111
'Symfony\Component\Security\Core\Authorization\AuthorizationChecker',
112112
'Symfony\Component\Security\Core\Authorization\Voter\VoterInterface',
113+
'Symfony\Bundle\SecurityBundle\Security\FirewallConfig',
113114
'Symfony\Bundle\SecurityBundle\Security\FirewallMap',
114115
'Symfony\Bundle\SecurityBundle\Security\FirewallContext',
115116
'Symfony\Component\HttpFoundation\RequestMatcher',
@@ -236,14 +237,18 @@ private function createFirewalls($config, ContainerBuilder $container)
236237
$mapDef = $container->getDefinition('security.firewall.map');
237238
$map = $authenticationProviders = array();
238239
foreach ($firewalls as $name => $firewall) {
239-
list($matcher, $listeners, $exceptionListener) = $this->createFirewall($container, $name, $firewall, $authenticationProviders, $providerIds);
240+
$configId = 'security.firewall.map.config.'.$name;
241+
242+
list($matcher, $listeners, $exceptionListener) = $this->createFirewall($container, $name, $firewall, $authenticationProviders, $providerIds, $configId);
240243

241244
$contextId = 'security.firewall.map.context.'.$name;
242245
$context = $container->setDefinition($contextId, new DefinitionDecorator('security.firewall.context'));
243246
$context
244247
->replaceArgument(0, $listeners)
245248
->replaceArgument(1, $exceptionListener)
249+
->replaceArgument(2, new Reference($configId))
246250
;
251+
247252
$map[$contextId] = $matcher;
248253
}
249254
$mapDef->replaceArgument(1, $map);
@@ -258,8 +263,11 @@ private function createFirewalls($config, ContainerBuilder $container)
258263
;
259264
}
260265

261-
private function createFirewall(ContainerBuilder $container, $id, $firewall, &$authenticationProviders, $providerIds)
266+
private function createFirewall(ContainerBuilder $container, $id, $firewall, &$authenticationProviders, $providerIds, $configId)
262267
{
268+
$config = $container->setDefinition($configId, new DefinitionDecorator('security.firewall.config'));
269+
$config->replaceArgument(0, $id);
270+
263271
// Matcher
264272
$matcher = null;
265273
if (isset($firewall['request_matcher'])) {
@@ -271,20 +279,28 @@ private function createFirewall(ContainerBuilder $container, $id, $firewall, &$a
271279
$matcher = $this->createRequestMatcher($container, $pattern, $host, $methods);
272280
}
273281

282+
$config->replaceArgument(1, (string) $matcher);
283+
$config->replaceArgument(2, $firewall['security']);
284+
274285
// Security disabled?
275286
if (false === $firewall['security']) {
276287
return array($matcher, array(), null);
277288
}
278289

290+
$config->replaceArgument(3, $firewall['stateless']);
291+
279292
// Provider id (take the first registered provider if none defined)
280293
if (isset($firewall['provider'])) {
281294
$defaultProvider = $this->getUserProviderId($firewall['provider']);
282295
} else {
283296
$defaultProvider = reset($providerIds);
284297
}
285298

299+
$config->replaceArgument(4, $defaultProvider);
300+
286301
// Register listeners
287302
$listeners = array();
303+
$listenerKeys = array();
288304

289305
// Channel listener
290306
$listeners[] = new Reference('security.channel_listener');
@@ -296,11 +312,14 @@ private function createFirewall(ContainerBuilder $container, $id, $firewall, &$a
296312
$contextKey = $firewall['context'];
297313
}
298314

315+
$config->replaceArgument(5, $contextKey);
316+
299317
$listeners[] = new Reference($this->createContextListener($container, $contextKey));
300318
}
301319

302320
// Logout listener
303321
if (isset($firewall['logout'])) {
322+
$listenerKeys[] = 'logout';
304323
$listenerId = 'security.logout_listener.'.$id;
305324
$listener = $container->setDefinition($listenerId, new DefinitionDecorator('security.logout_listener'));
306325
$listener->replaceArgument(3, array(
@@ -363,10 +382,13 @@ private function createFirewall(ContainerBuilder $container, $id, $firewall, &$a
363382
// Authentication listeners
364383
list($authListeners, $defaultEntryPoint) = $this->createAuthenticationListeners($container, $id, $firewall, $authenticationProviders, $defaultProvider, $configuredEntryPoint);
365384

385+
$config->replaceArgument(6, $configuredEntryPoint ?: $defaultEntryPoint);
386+
366387
$listeners = array_merge($listeners, $authListeners);
367388

368389
// Switch user listener
369390
if (isset($firewall['switch_user'])) {
391+
$listenerKeys[] = 'switch_user';
370392
$listeners[] = new Reference($this->createSwitchUserListener($container, $id, $firewall['switch_user'], $defaultProvider));
371393
}
372394

@@ -376,7 +398,30 @@ private function createFirewall(ContainerBuilder $container, $id, $firewall, &$a
376398
// Exception listener
377399
$exceptionListener = new Reference($this->createExceptionListener($container, $firewall, $id, $configuredEntryPoint ?: $defaultEntryPoint, $firewall['stateless']));
378400

401+
if (isset($firewall['access_denied_handler'])) {
402+
$config->replaceArgument(7, $firewall['access_denied_handler']);
403+
}
404+
if (isset($firewall['access_denied_url'])) {
405+
$config->replaceArgument(8, $firewall['access_denied_url']);
406+
}
407+
379408
$container->setAlias(new Alias('security.user_checker.'.$id, false), $firewall['user_checker']);
409+
$config->replaceArgument(9, $firewall['user_checker']);
410+
411+
foreach ($this->factories as $position) {
412+
foreach ($position as $factory) {
413+
$key = str_replace('-', '_', $factory->getKey());
414+
if (array_key_exists($key, $firewall)) {
415+
$listenerKeys[] = $key;
416+
}
417+
}
418+
}
419+
420+
if (isset($firewall['anonymous'])) {
421+
$listenerKeys[] = 'anonymous';
422+
}
423+
424+
$config->replaceArgument(10, $listenerKeys);
380425

381426
return array($matcher, $listeners, $exceptionListener);
382427
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,21 @@
111111
<service id="security.firewall.context" class="Symfony\Bundle\SecurityBundle\Security\FirewallContext" abstract="true">
112112
<argument type="collection" />
113113
<argument type="service" id="security.exception_listener" />
114+
<argument /> <!-- FirewallConfig -->
115+
</service>
116+
117+
<service id="security.firewall.config" class="Symfony\Bundle\SecurityBundle\Security\FirewallConfig" abstract="true" public="false">
118+
<argument /> <!-- name -->
119+
<argument /> <!-- request_matcher -->
120+
<argument /> <!-- security enabled -->
121+
<argument /> <!-- stateless -->
122+
<argument /> <!-- provider -->
123+
<argument /> <!-- context -->
124+
<argument /> <!-- entry_point -->
125+
<argument /> <!-- user_checker -->
126+
<argument /> <!-- access_denied_handler -->
127+
<argument /> <!-- access_denied_url -->
128+
<argument type="collection" /> <!-- listeners -->
114129
</service>
115130

116131
<service id="security.logout_url_generator" class="Symfony\Component\Security\Http\Logout\LogoutUrlGenerator" public="false">
@@ -119,7 +134,6 @@
119134
<argument type="service" id="security.token_storage" on-invalid="null" />
120135
</service>
121136

122-
123137
<!-- Provisioning -->
124138
<service id="security.user.provider.in_memory" class="Symfony\Component\Security\Core\User\InMemoryUserProvider" abstract="true" public="false" />
125139
<service id="security.user.provider.in_memory.user" class="Symfony\Component\Security\Core\User\User" abstract="true" public="false" />
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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\Security;
13+
14+
use Symfony\Component\Security\Http\FirewallMapInterface;
15+
use Symfony\Component\HttpFoundation\Request;
16+
17+
/**
18+
* This interface must be implemented by firewall maps that are able to return a
19+
* context.
20+
*
21+
* @author Robin Chalas <robin.chalas@gmail.com>
22+
*/
23+
interface ContextAwareFirewallMapInterface extends FirewallMapInterface
24+
{
25+
/**
26+
* Returns the firewall context for a given request.
27+
*
28+
* @param Request $request
29+
*
30+
* @return FirewallContext
31+
*/
32+
public function getContext(Request $request);
33+
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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\Security;
13+
14+
/**
15+
* @author Robin Chalas <robin.chalas@gmail.com>
16+
*/
17+
class FirewallConfig
18+
{
19+
private $name;
20+
private $requestMatcher;
21+
private $securityEnabled;
22+
private $stateless;
23+
private $provider;
24+
private $context;
25+
private $entryPoint;
26+
private $accessDeniedHandler;
27+
private $accessDeniedUrl;
28+
private $userChecker;
29+
private $listeners;
30+
31+
/**
32+
* @param string $name
33+
* @param string $requestMatcher
34+
* @param bool $securityEnabled
35+
* @param bool $stateless
36+
* @param string $provider
37+
* @param string $context
38+
* @param string $entryPoint
39+
* @param string $accessDeniedHandler
40+
* @param string $accessDeniedUrl
41+
* @param string $userChecker
42+
* @param array $listeners
43+
*/
44+
public function __construct($name, $requestMatcher, $securityEnabled = true, $stateless = false, $provider = null, $context = null, $entryPoint = null, $accessDeniedHandler = null, $accessDeniedUrl = null, $userChecker = null, $listeners = array())
45+
{
46+
$this->name = $name;
47+
$this->requestMatcher = $requestMatcher;
48+
$this->securityEnabled = $securityEnabled;
49+
$this->stateless = $stateless;
50+
$this->provider = $provider;
51+
$this->context = $context;
52+
$this->entryPoint = $entryPoint;
53+
$this->accessDeniedHandler = $accessDeniedHandler;
54+
$this->accessDeniedUrl = $accessDeniedUrl;
55+
$this->userChecker = $userChecker;
56+
$this->listeners = $listeners;
57+
}
58+
59+
public function getName()
60+
{
61+
return $this->name;
62+
}
63+
64+
/**
65+
* @return string The request matcher service id
66+
*/
67+
public function getRequestMatcher()
68+
{
69+
return $this->requestMatcher;
70+
}
71+
72+
/**
73+
* @return bool
74+
*/
75+
public function isSecurityEnabled()
76+
{
77+
return $this->securityEnabled;
78+
}
79+
80+
/**
81+
* @return bool
82+
*/
83+
public function allowsAnonymous()
84+
{
85+
return in_array('anonymous', $this->listeners, true);
86+
}
87+
88+
/**
89+
* @return bool
90+
*/
91+
public function isStateless()
92+
{
93+
return $this->stateless;
94+
}
95+
96+
/**
97+
* @return string The provider service id
98+
*/
99+
public function getProvider()
100+
{
101+
return $this->provider;
102+
}
103+
104+
/**
105+
* @return string
106+
*/
107+
public function getContext()
108+
{
109+
return $this->context;
110+
}
111+
112+
/**
113+
* @return string The entry_point service id
114+
*/
115+
public function getEntryPoint()
116+
{
117+
return $this->entryPoint;
118+
}
119+
120+
/**
121+
* @return string The user_checker service id
122+
*/
123+
public function getUserChecker()
124+
{
125+
return $this->userChecker;
126+
}
127+
128+
/**
129+
* @return string The access_denied_handler service id
130+
*/
131+
public function getAccessDeniedHandler()
132+
{
133+
return $this->accessDeniedHandler;
134+
}
135+
136+
/**
137+
* @return string
138+
*/
139+
public function getAccessDeniedUrl()
140+
{
141+
return $this->accessDeniedUrl;
142+
}
143+
144+
/**
145+
* @return array An array of listener keys
146+
*/
147+
public function getListeners()
148+
{
149+
return $this->listeners;
150+
}
151+
}

src/Symfony/Bundle/SecurityBundle/Security/FirewallContext.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,37 @@ class FirewallContext
2323
{
2424
private $listeners;
2525
private $exceptionListener;
26+
private $config;
2627

27-
public function __construct(array $listeners, ExceptionListener $exceptionListener = null)
28+
public function __construct(array $listeners, ExceptionListener $exceptionListener = null, FirewallConfig $config = null)
2829
{
2930
$this->listeners = $listeners;
3031
$this->exceptionListener = $exceptionListener;
32+
33+
if (null === $config) {
34+
@trigger_error(sprintf('"%s()" expects an instance of "%s" as third argument since version 3.2 and will trigger an error in 4.0 if not provided.', __METHOD__, FirewallConfig::class), E_USER_DEPRECATED);
35+
36+
return;
37+
}
38+
39+
$this->config = $config;
40+
}
41+
42+
/**
43+
* Returns the firewall configuration.
44+
*
45+
* @return FirewallConfig
46+
*/
47+
public function getConfig()
48+
{
49+
return $this->config;
3150
}
3251

52+
/**
53+
* Returns the listeners of this context.
54+
*
55+
* @return array
56+
*/
3357
public function getContext()
3458
{
3559
return array($this->listeners, $this->exceptionListener);

0 commit comments

Comments
 (0)
0