8000 Add Firewall object & service · symfony/symfony@83fc3a4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 83fc3a4

Browse files
committed
Add Firewall object & service
Create one service per firewall (security.firewall.map.[name]) Depreciate security.firewall.map.context.[name] service Access FirewallContext via Firewall::getContext() (i.e. container->get('security.firewall.map.foo')->getContext()) Pass [firewall id] => RequestMatcher as FirewallMap::$map Use Firewal::getContext() rather than deprecated context service in FirewallMap::getListeners() Add some shortcut methods for main firewall keys (anonymous, stateless, ..) to Firewall class
1 parent 031b850 commit 83fc3a4

File tree

5 files changed

+122
-5
lines changed

5 files changed

+122
-5
lines changed

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

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

1414
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SecurityFactoryInterface;
1515
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\UserProvider\UserProviderFactoryInterface;
16+
use Symfony\Bundle\SecurityBundle\Security\Firewall;
1617
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1718
use Symfony\Component\DependencyInjection\DefinitionDecorator;
1819
use Symfony\Component\DependencyInjection\Alias;
@@ -110,6 +111,7 @@ public function load(array $configs, ContainerBuilder $container)
110111
'Symfony\Component\Security\Core\Authorization\AccessDecisionManager',
111112
'Symfony\Component\Security\Core\Authorization\AuthorizationChecker',
112113
'Symfony\Component\Security\Core\Authorization\Voter\VoterInterface',
114+
'Symfony\Bundle\SecurityBundle\Security\Firewall',
113115
'Symfony\Bundle\SecurityBundle\Security\FirewallMap',
114116
'Symfony\Bundle\SecurityBundle\Security\FirewallContext',
115117
'Symfony\Component\HttpFoundation\RequestMatcher',
@@ -235,16 +237,25 @@ private function createFirewalls($config, ContainerBuilder $container)
235237
// load firewall map
236238
$mapDef = $container->getDefinition('security.firewall.map');
237239
$map = $authenticationProviders = array();
238-
foreach ($firewalls as $name => $firewall) {
239-
list($matcher, $listeners, $exceptionListener) = $this->createFirewall($container, $name, $firewall, $authenticationProviders, $providerIds);
240+
foreach ($firewalls as $name => $config) {
241+
list($matcher, $listeners, $exceptionListener) = $this->createFirewall($container, $name, $config, $authenticationProviders, $providerIds);
240242

243+
// Deprecated in 3.2, to be removed in 4.0
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)
246249
;
247-
$map[$contextId] = $matcher;
250+
251+
$firewallId = 'security.firewall.map.'.$name;
252+
$firewall = $container->setDefinition($firewallId, new DefinitionDecorator('security.firewall.map.firewall'));
253+
$firewall
254+
->replaceArgument(0, new Reference($contextId))
255+
->replaceArgument(1, $config)
256+
;
257+
258+
$map[$firewallId] = $matcher;
248259
}
249260
$mapDef->replaceArgument(1, $map);
250261

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
<argument type="collection" />
109109
</service>
110110

111+
<!-- Deprecated in 3.2, removed in 4.0 -->
111112
<service id="security.firewall.context" class=< 8000 span class="pl-pds">"Symfony\Bundle\SecurityBundle\Security\FirewallContext" abstract="true">
112113
<argument type="collection" />
113114
<argument type="service" id="security.exception_listener" />
@@ -119,6 +120,10 @@
119120
<argument type="service" id="security.token_storage" on-invalid="null" />
120121
</service>
121122

123+
<service id="security.firewall.map.firewall" class="Symfony\Bundle\SecurityBundle\Security\Firewall" abstract="true">
124+
<argument /> <!-- FirewallContext -->
125+
<argument type="collection" />
126+
</service>
122127

123128
<!-- Provisioning -->
124129
<service id="security.user.provider.in_memory" class="Symfony\Component\Security\Core\User\InMemoryUserProvider" abstract="true" public="false" />
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
* Object representation of a firewall.
16+
*
17+
* @author Robin Chalas <robin.chalas@gmail.com>
18+
*/
19+
class Firewall
20+
{
21+
private $context;
22+
private $options;
23+
24+
/**
25+
* @param FirewallContext $context
26+
* @param array $options
27+
*/
28+
public function __construct(FirewallContext $context, array $options)
29+
{
30+
$this->context = $context;
31+
$this->options = $options;
32+
}
33+
34+
/**
35+
* Gets the firewall context.
36+
*
37+
* @return FirewallContext
38+
*/
39+
public function getContext()
40+
{
41+
return $this->context;
42+
}
43+
44+
/**
45+
* @return bool The "stateless" option value
46+
*/
47+
public function isStateless()
48+
{
49+
return (bool) $this->getOption('stateless');
50+
}
51+
52+
/**
53+
* @return bool True if the "rembember_me" option is set.
54+
*/
55+
public function supportsRememberMe()
56+
{
57+
return (bool) $this->getOption('remember_me');
58+
}
59+
60+
/**
61+
* @return bool True if the "anonymous" option is set.
62+
*/
63+
public function supportsAnonymous()
64+
{
65+
return (bool) $this->getOption('anonymous');
66+
}
67+
68+
/**
69+
* Returns the whole firewall configuration.
70+
*
71+
* @return array
72+
*/
73+
public function getOptions()
74+
{
75+
return $this->options;
76+
}
77+
78+
/**
79+
* Returns the value of a firewall mapping option by key.
80+
*
81+
* @param string|null $key The key
82+
*
83+
* @return string|null The value, null if the key doesn't exist
84+
*/
85+
private function getOption($key)
86+
{
87+
return array_key_exists($key, $this->options) ? $this->options[$key] : null;
88+
}
89+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,20 @@ public function __construct(array $listeners, ExceptionListener $exceptionListen
3030
$this->exceptionListener = $exceptionListener;
3131
}
3232

33+
public function getListeners()
34+
{
35+
return array($this->listeners, $this->exceptionListener);
36+
}
37+
38+
/**
39+
* @deprecated since 3.2. Use {@see getListeners()} instead.
40+
*
41+
* @return array
42+
*/
3343
public function getContext()
3444
{
45+
@trigger_error(sprintf('Method %s() is deprecated since version 3.2 and will be removed in 4.0. Use %s::getListeners() instead.', __METHOD__, __CLASS__), E_USER_DEPRECATED);
46+
3547
return array($this->listeners, $this->exceptionListener);
3648
}
3749
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public function __construct(ContainerInterface $container, array $map)
3535

3636
public function getListeners(Request $request)
3737
{
38-
foreach ($this->map as $contextId => $requestMatcher) {
38+
foreach ($this->map as $firewall => $requestMatcher) {
3939
if (null === $requestMatcher || $requestMatcher->matches($request)) {
40-
return $this->container->get($contextId)->getContext();
40+
return $this->container->get($firewall)->getContext()->getListeners();
4141
}
4242
}
4343

0 commit comments

Comments
 (0)
0