Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9aa00ce

Browse files
committed
Move cache of the firewall context into the request parameters
1 parent bf99da5 commit 9aa00ce

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ class FirewallMap implements FirewallMapInterface
2626
{
2727
private $container;
2828
private $map;
29-
private $contexts;
3029

3130
public function __construct(ContainerInterface $container, $map)
3231
{
3332
$this->container = $container;
3433
$this->map = $map;
35-
$this->contexts = new \SplObjectStorage();
3634
}
3735

3836
public function getListeners(Request $request)
@@ -60,15 +58,22 @@ public function getFirewallConfig(Request $request)
6058
return $context->getConfig();
6159
}
6260

61+
/**
62+
* @param Request $request
63+
*
64+
* @return \Symfony\Bundle\SecurityBundle\Security\FirewallContext
65+
*/
6366
private function getFirewallContext(Request $request)
6467
{
65-
if ($this->contexts->contains($request)) {
66-
return $this->contexts[$request];
68+
if ($request->attributes->has('_firewall_context')) {
69+
return $this->container->get($request->attributes->get('_firewall_context'));
6770
}
6871

6972
foreach ($this->map as $contextId => $requestMatcher) {
7073
if (null === $requestMatcher || $requestMatcher->matches($request)) {
71-
return $this->contexts[$request] = $this->container->get($contextId);
74+
$request->attributes->set('_firewall_context', $contextId);
75+
76+
return $this->container->get($contextId);
7277
}
7378
}
7479
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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+
public function testGetListenersWithEmptyMap()
24+
{
25+
$request = new Request();
26+
27+
$map = array();
28+
$container = $this->getMockBuilder(Container::class)->getMock();
29+
$container->expects($this->never())->method('get');
30+
31+
$firewallMap = new FirewallMap($container, $map);
32+
33+
$this->assertEquals(array(array(), null), $firewallMap->getListeners($request));
34+
35+
$this->assertNull($firewallMap->getFirewallConfig($request));
36+
}
37+
38+
public function testGetListeners()
39+
{
40+
$request = new Request();
41+
42+
$firewallContext = $this->getMockBuilder(FirewallContext::class)->disableOriginalConstructor()->getMock();
43+
$firewallContext->expects($this->once())->method('getConfig')->willReturn('CONFIG');
44+
$firewallContext->expects($this->once())->method('getListeners')->willReturn('LISTENERS');
45+
$firewallContext->expects($this->once())->method('getExceptionListener')->willReturn('EXCEPTION LISTENER');
46+
47+
$matcher = $this->getMockBuilder(RequestMatcherInterface::class)->getMock();
48+
$matcher->expects($this->once())
49+
->method('matches')
50+
->with($request)
51+
->willReturn(true);
52+
53+
$container = $this->getMockBuilder(Container::class)->getMock();
54+
$container->expects($this->exactly(2))->method('get')->willReturn($firewallContext);
55+
56+
$firewallMap = new FirewallMap($container, array('security.firewall.map.context.foo' => $matcher));
57+
58+
$this->assertEquals(array('LISTENERS', 'EXCEPTION LISTENER'), $firewallMap->getListeners($request));
59+
$this->assertEquals('CONFIG', $firewallMap->getFirewallConfig($request));
60+
}
61+
}

0 commit comments

Comments
 (0)