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

Skip to content

Commit 3cb9d6c

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 3cb9d6c

File tree

5 files changed

+124
-5
lines changed

5 files changed

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

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