Skip to content

Commit b3203cb

Browse files
GromNaNfabpot
authored andcommitted
[SecurityBundle] Move cache of the firewall context into the request parameters
1 parent 40beab4 commit b3203cb

File tree

2 files changed

+95
-5
lines changed

2 files changed

+95
-5
lines changed

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\SecurityBundle\Security;
1313

14+
use Symfony\Bundle\SecurityBundle\Security\FirewallContext;
1415
use Symfony\Component\Security\Http\FirewallMapInterface;
1516
use Symfony\Component\HttpFoundation\Request;
1617
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -26,13 +27,11 @@ class FirewallMap implements FirewallMapInterface
2627
{
2728
protected $container;
2829
protected $map;
29-
private $contexts;
3030

3131
public function __construct(ContainerInterface $container, array $map)
3232
{
3333
$this->container = $container;
3434
$this->map = $map;
35-
$this->contexts = new \SplObjectStorage();
3635
}
3736

3837
/**
@@ -63,15 +62,27 @@ public function getFirewallConfig(Request $request)
6362
return $context->getConfig();
6463
}
6564

65+
/**
66+
* @return FirewallContext
67+
*/
6668
private function getFirewallContext(Request $request)
6769
{
68-
if ($this->contexts->contains($request)) {
69-
return $this->contexts[$request];
70+
if ($request->attributes->has('_firewall_context')) {
71+
$storedContextId = $request->attributes->get('_firewall_context');
72+
foreach ($this->map as $contextId => $requestMatcher) {
73+
if ($contextId === $storedContextId) {
74+
return $this->container->get($contextId);
75+
}
76+
}
77+
78+
$request->attributes->remove('_firewall_context');
7079
}
7180

7281
foreach ($this->map as $contextId => $requestMatcher) {
7382
if (null === $requestMatcher || $requestMatcher->matches($request)) {
74-
return $this->contexts[$request] = $this->container->get($contextId);
83+
$request->attributes->set('_firewall_context', $contextId);
84+
85+
return $this->container->get($contextId);
7586
}
7687
}
7788
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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\Security;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bundle\SecurityBundle\Security\FirewallContext;
16+
use Symfony\Bundle\SecurityBundle\Security\FirewallMap;
17+
use Symfony\Component\DependencyInjection\Container;
18+
use Symfony\Component\HttpFoundation\Request;
19+
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
20+
21+
class FirewallMapTest extends TestCase
22+
{
23+
const ATTRIBUTE_FIREWALL_CONTEXT = '_firewall_context';
24+
25+
public function testGetListenersWithEmptyMap()
26+
{
27+
$request = new Request();
28+
29+
$map = array();
30+
$container = $this->getMockBuilder(Container::class)->getMock();
31+
$container->expects($this->never())->method('get');
32+
33+
$firewallMap = new FirewallMap($container, $map);
34+
35+
$this->assertEquals(array(array(), null), $firewallMap->getListeners($request));
36+
$this->assertNull($firewallMap->getFirewallConfig($request));
37+
$this->assertFalse($request->attributes->has(self::ATTRIBUTE_FIREWALL_CONTEXT));
38+
}
39+
40+
public function testGetListenersWithInvalidParameter()
41+
{
42+
$request = new Request();
43+
$request->attributes->set(self::ATTRIBUTE_FIREWALL_CONTEXT, 'foo');
44+
45+
$map = array();
46+
$container = $this->getMockBuilder(Container::class)->getMock();
47+
$container->expects($this->never())->method('get');
48+
49+
$firewallMap = new FirewallMap($container, $map);
50+
51+
$this->assertEquals(array(array(), null), $firewallMap->getListeners($request));
52+
$this->assertNull($firewallMap->getFirewallConfig($request));
53+
$this->assertFalse($request->attributes->has(self::ATTRIBUTE_FIREWALL_CONTEXT));
54+
}
55+
56+
public function testGetListeners()
57+
{
58+
$request = new Request();
59+
60+
$firewallContext = $this->getMockBuilder(FirewallContext::class)->disableOriginalConstructor()->getMock();
61+
$firewallContext->expects($this->once())->method('getConfig')->willReturn('CONFIG');
62+
$firewallContext->expects($this->once())->method('getContext')->willReturn(array('LISTENERS', 'EXCEPTION LISTENER'));
63+
64+
$matcher = $this->getMockBuilder(RequestMatcherInterface::class)->getMock();
65+
$matcher->expects($this->once())
66+
->method('matches')
67+
->with($request)
68+
->willReturn(true);
69+
70+
$container = $this->getMockBuilder(Container::class)->getMock();
71+
$container->expects($this->exactly(2))->method('get')->willReturn($firewallContext);
72+
73+
$firewallMap = new FirewallMap($container, array('security.firewall.map.context.foo' => $matcher));
74+
75+
$this->assertEquals(array('LISTENERS', 'EXCEPTION LISTENER'), $firewallMap->getListeners($request));
76+
$this->assertEquals('CONFIG', $firewallMap->getFirewallConfig($request));
77+
$this->assertEquals('security.firewall.map.context.foo', $request->attributes->get(self::ATTRIBUTE_FIREWALL_CONTEXT));
78+
}
79+
}

0 commit comments

Comments
 (0)