8000 Populate FirewallConfig definition from SecurityExtension · symfony/symfony@0dc120b · GitHub
[go: up one dir, main page]

Skip to content

Commit 0dc120b

Browse files
committed
Populate FirewallConfig definition from SecurityExtension
Add PHPDoc to FirewallConfig
1 parent 6ea2a15 commit 0dc120b

File tree

4 files changed

+115
-89
lines changed

4 files changed

+115
-89
lines changed

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

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -237,24 +237,9 @@ private function createFirewalls($config, ContainerBuilder $container)
237237
$mapDef = $container->getDefinition('security.firewall.map');
238238
$map = $authenticationProviders = array();
239239
foreach ($firewalls as $name => $firewall) {
240-
list($matcher, $listeners, $exceptionListener) = $this->createFirewall($container, $name, $firewall, $authenticationProviders, $providerIds);
241-
242-
$listenerKeys = array();
243-
foreach ($this->factories as $position) {
244-
foreach ($position as $factory) {
245-
$key = str_replace('-', '_', $factory->getKey());
246-
if (array_key_exists($key, $firewall)) {
247-
$listenerKeys[] = $key;
248-
}
249-
}
250-
}
251-
252240
$configId = 'security.firewall.map.config.'.$name;
253-
$firewallDef = $container->setDefinition($configId, new DefinitionDecorator('security.firewall.config'));
254-
$firewallDef
255-
->replaceArgument(0, $firewall)
256-
->replaceArgument(1, $listenerKeys)
257-
;
241+
242+
list($matcher, $listeners, $exceptionListener) = $this->createFirewall($container, $name, $firewall, $authenticationProviders, $providerIds, $configId);
258243

259244
$contextId = 'security.firewall.map.context.'.$name;
260245
$context = $container->setDefinition($contextId, new DefinitionDecorator('security.firewall.context'));
@@ -278,8 +263,11 @@ private function createFirewalls($config, ContainerBuilder $container)
278263
;
279264
}
280265

281-
private function createFirewall(ContainerBuilder $container, $id, $firewall, &$authenticationProviders, $providerIds)
266+
private function createFirewall(ContainerBuilder $container, $id, $firewall, &$authenticationProviders, $providerIds, $configId)
282267
{
268+
// FirewallConfig
269+
$config = $container->setDefinition($configId, new DefinitionDecorator('security.firewall.config'));
270+
283271
// Matcher
284272
$matcher = null;
285273
if (isset($firewall['request_matcher'])) {
@@ -291,18 +279,26 @@ private function createFirewall(ContainerBuilder $container, $id, $firewall, &$a
291279
$matcher = $this->createRequestMatcher($container, $pattern, $host, $methods);
292280
}
293281

282+
$config->replaceArgument(0, (string) $matcher);
283+
294284
// Security disabled?
295285
if (false === $firewall['security']) {
296-
return array($matcher, array(), null);
286+
$config->replaceArgument(1, false);
287+
288+
return array($matcher, array(), null, $configId);
297289
}
298290

291+
$config->replaceArgument(2, $firewall['stateless']);
292+
299293
// Provider id (take the first registered provider if none defined)
300294
if (isset($firewall['provider'])) {
301295
$defaultProvider = $this->getUserProviderId($firewall['provider']);
302296
} else {
303297
$defaultProvider = reset($providerIds);
304298
}
305299

300+
$config->replaceArgument(3, $defaultProvider);
301+
306302
// Register listeners
307303
$listeners = array();
308304

@@ -316,6 +312,8 @@ private function createFirewall(ContainerBuilder $container, $id, $firewall, &$a
316312
$contextKey = $firewall['context'];
317313
}
318314

315+
$config->replaceArgument(4, $contextKey);
316+
319317
$listeners[] = new Reference($this->createContextListener($container, $contextKey));
320318
}
321319

@@ -383,6 +381,8 @@ private function createFirewall(ContainerBuilder $container, $id, $firewall, &$a
383381
// Authentication listeners
384382
list($authListeners, $defaultEntryPoint) = $this->createAuthenticationListeners($container, $id, $firewall, $authenticationProviders, $defaultProvider, $configuredEntryPoint);
385383

384+
$config->replaceArgument(5, $defaultEntryPoint);
385+
386386
$listeners = array_merge($listeners, $authListeners);
387387

388388
// Switch user listener
@@ -397,6 +397,20 @@ private function createFirewall(ContainerBuilder $container, $id, $firewall, &$a
397397
$exceptionListener = new Reference($this->createExceptionListener($container, $firewall, $id, $configuredEntryPoint ?: $defaultEntryPoint, $firewall['stateless']));
398398

399399
$container->setAlias(new Alias('security.user_checker.'.$id, false), $firewall['user_checker']);
400+
$config->replaceArgument(6, $firewall['user_checker']);
401+
402+
$listenerConfigs = array();
403+
// TODO Add missing listeners (logout, remember_me, ...)
404+
foreach ($this->factories as $position) {
405+
foreach ($position as $factory) {
406+
$key = str_replace('-', '_', $factory->getKey());
407+
if (array_key_exists($key, $firewall)) {
408+
$listenerConfigs[$key] = $firewall[$key];
409+
}
410+
}
411+
}
412+
413+
$config->replaceArgument(7, $listenerConfigs);
400414

401415
return array($matcher, $listeners, $exceptionListener);
402416
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,16 @@
115115
</service>
116116

117117
<service id="security.firewall.config" class="Symfony\Bundle\SecurityBundle\Security\FirewallConfig" abstract="true" public="false">
118-
<argument /> <!-- options -->
119-
<argument /> <!-- listener keys -->
118+
<argument /> <!-- security -->
119+
<argument /> <!-- request_matcher -->
120+
<argument /> <!-- access_denied_url -->
121+
<argument /> <!-- access_denied_handler -->
122+
<argument /> <!-- entry_point -->
123+
<argument /> <!-- provider -->
124+
<argument /> <!-- stateless -->
125+
<argument /> <!-- context -->
126+
<argument /> <!-- user_checker -->
127+
<argument type="collection" /> <!-- listener configs -->
120128
</service>
121129

122130
<service id="security.logout_url_generator" class="Symfony\Component\Security\Http\Logout\LogoutUrlGenerator" public="false">

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

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -18,88 +18,94 @@
1818
*/
1919
class FirewallConfig
2020
{
21-
private $pattern;
22-
private $security;
2321
private $requestMatcher;
22+
private $security;
23+
private $provider;
2424
private $accessDeniedUrl;
2525
private $accessDeniedHandler;
2626
private $entryPoint;
27-
private $provider;
2827
private $stateless;
2928
private $context;
3029
private $userChecker;
3130
private $listenerConfigs;
3231

33-
/**
34-
* @param array $options The whole firewall configuration
35-
* @param array $listenerKeys An array of keys corresponding to each configured listener
36-
* of the firewall
37-
*/
38-
public function __construct(array $options, array $listenerKeys = array())
39-
{
40-
$this->set($this->pattern, 'pattern', $options);
41-
$this->set($this->security, 'security', $options);
42-
$this->set($this->requestMatcher, 'request_matcher', $options);
43-
$this->set($this->accessDeniedUrl, 'access_denied_url', $options);
44-
$this->set($this->accessDeniedHandler, 'access_denied_handler', $options);
45-
$this->set($this->entryPoint, 'entry_point', $options);
46-
$this->set($this->provider, 'provider', $options);
47-
$this->set($this->stateless, 'stateless', $options);
48-
$this->set($this->context, 'context', $options);
49-
$this->set($this->userChecker, 'user_checker', $options);
50-
51-
foreach ($options as $key => $value) {
52-
if (in_array($key, $listenerKeys)) {
53-
$this->listenerConfigs[$key] = $value;
54-
}
55-
}
56-
}
57-
58-
public function getPattern()
59-
{
60-
return $this->pattern;
61-
}
62-
63-
public function hasSecurity()
32+
public function __construct($requestMatcher, $security, $stateless = false, $provider = null, $context = null, $entryPoint = null, $userChecker = null, $listenerConfigs = array())
6433
{
65-
return (bool) $this->security;
34+
$this->requestMatcher = $requestMatcher;
35+
$this->security = $security;
36+
$this->stateless = $stateless;
37+
$this->provider = $provider;
38+
$this->context = $context;
39+
$this->entryPoint = $entryPoint;
40+
$this->userChecker = $userChecker;
41+
$this->listenerConfigs = $listenerConfigs;
6642
}
6743

44+
/**
45+
* Returns the request matcher service id.
46+
*
47+
* @return string
48+
*/
6849
public function getRequestMatcher()
6950
{
7051
return $this->requestMatcher;
7152
}
7253

73-
public function getAccessDeniedUrl()
74-
{
75-
return $this->a 10000 ccessDeniedUrl;
76-
}
77-
78-
public function getAccessDeniedHandler()
54+
/**
55+
* Returns either the security is disabled or not.
56+
*
57+
* @return bool
58+
*/
59+
public function hasSecurity()
7960
{
80-
return $this->accessDeniedHandler;
61+
return (bool) $this->security;
8162
}
8263

83-
public function getEntryPoint()
64+
/**
65+
* Returns either the firewall is stateless or not.
66+
*
67+
* @return bool
68+
*/
69+
public function isStateless()
8470
{
85-
return $this->entryPoint;
71+
return (bool) $this->stateless;
8672
}
8773

74+
/**
75+
* Returns the provider service id.
76+
*
77+
* @return string
78+
*/
8879
public function getProvider()
8980
{
9081
return $this->provider;
9182
}
9283

93-
public function isStateless()
84+
/**
85+
* Returns the context service id.
86+
*
87+
* @return string
88+
*/
89+
public function getContext()
9490
{
95-
return (bool) $this->stateless;
91+
return $this->context;
9692
}
9793

98-
public function getContext()
94+
/**
95+
* Returns the entry_point service id.
96+
*
97+
* @return string
98+
*/
99+
public function getEntryPoint()
99100
{
100-
return $this->context;
101+
return $this->entryPoint;
101102
}
102103

104+
/**
105+
* Returns the user_checker service id.
106+
*
107+
* @return string
108+
*/
103109
public function getUserChecker()
104110
{
105111
return $this->userChecker;
@@ -115,11 +121,4 @@ public function getListenerConfigs()
115121
{
116122
return $this->listenerConfigs;
117123
}
118-
119-
private function set(&$property, $key, $config)
120-
{
121-
if (array_key_exists($key, $config)) {
122-
$property = $config[$key];
123-
}
124-
}
125124
}

src/Symfony/Bundle/SecurityBundle/Tests/Security/FirewallConfigTest.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public function testGetters()
1919
{
2020
$listenerKeys = array('logout', 'remember_me', 'anonymous');
2121
$options = array(
22-
'pattern' => 'foo_pattern',
2322
'security' => false,
2423
'request_matcher' => 'foo_request_matcher',
2524
'access_denied_url' => 'foo_access_denied_url',
@@ -34,26 +33,32 @@ public function testGetters()
3433
'anonymous' => array('secret' => 'bar'),
3534
);
3635

37-
$config = new FirewallConfig($options, $listenerKeys);
36+
$listenerConfigs = array(
37+
'logout' => $options['logout'],
38+
'remember_me' => $options['remember_me'],
39+
'anonymous' => $options['anonymous'],
40+
);
41+
42+
$config = new FirewallConfig(
43+
$options['request_matcher'],
44+
$options['security'],
45+
$options['stateless'],
46+
$options['provider'],
47+
$options['context'],
48+
$options['entry_point'],
49+
$options['user_checker'],
50+
$listenerConfigs
51+
);
3852

3953
// Basic config options
40-
$this->assertSame($options['pattern'], $config->getPattern());
4154
$this->assertSame($options['security'], $config->hasSecurity());
55+
$this->assertSame($options['context'], $config->getContext());
4256
$this->assertSame($options['request_matcher'], $config->getRequestMatcher());
43-
$this->assertSame($options['access_denied_url'], $config->getAccessDeniedUrl());
44-
$this->assertSame($options['access_denied_handler'], $config->getAccessDeniedHandler());
4557
$this->assertSame($options['entry_point'], $config->getEntryPoint());
4658
$this->assertSame($options['provider'], $config->getProvider());
4759
$this->assertSame($options['stateless'], $config->isStateless());
4860
$this->assertSame($options['user_checker'], $config->getUserChecker());
4961

50-
// Listener configs
51-
$expectedConfigs = array(
52-
'logout' => $options['logout'],
53-
'remember_me' => $options['remember_me'],
54-
'anonymous' => $options['anonymous'],
55-
);
56-
57-
$this->assertSame($expectedConfigs, $config->getListenerConfigs());
62+
$this->assertSame($listenerConfigs, $config->getListenerConfigs());
5863
}
5964
}

0 commit comments

Comments
 (0)
0