8000 Move cache of the firewall context into the request parameters · symfony/symfony@3108023 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3108023

Browse files
committed
Move cache of the firewall context into the request parameters
Validate that _firewall_context attribute is in the map keys
1 parent 40beab4 commit 3108023

File tree

2 files changed

+97
-5
lines changed

2 files changed

+97
-5
lines changed

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

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

1212
namespace Symfony\Bundle\SecurityBundle\Security;
1313

14+
use Psr\Container\ContainerInterface;
15+
use Symfony\Bundle\SecurityBundle\Security\FirewallContext;
1416
use Symfony\Component\Security\Http\FirewallMapInterface;
1517
use Symfony\Component\HttpFoundation\Request;
1618
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -26,13 +28,11 @@ class FirewallMap implements FirewallMapInterface
2628
{
2729
protected $container;
2830
protected $map;
29-
private $contexts;
3031

3132
public function __construct(ContainerInterface $container, array $map)
3233
{
3334
$this->container = $container;
3435
$this->map = $map;
35-
$this->contexts = new \SplObjectStorage();
3636
}
3737

3838
/**
@@ -63,15 +63,27 @@ public function getFirewallConfig(Request $request)
6363
return $context->getConfig();
6464
}
6565

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

7282
foreach ($this->map as $contextId => $requestMatcher) {
7383
if (null === $requestMatcher || $requestMatcher->matches($request)) {
74-
return $this->contexts[$request] = $this->container->get($contextId);
84+
$request->attributes->set('_firewall_context', $contextId);
85+
86+
return $this->container->get($contextId);
7587
}
7688
}
7789
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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('getListeners')->willReturn('LISTENERS');
63+
$firewallContext->expects($this->once())->method('getExceptionListener')->willReturn('EXCEPTION LISTENER');
64+
65+
$matcher = $this->getMockBuilder(RequestMatcherInterface::class)->getMock();
66+
$matcher->expects($this->once())
67+
->method('matches')
68+
->with($request)
69+
->willReturn(true);
70+
71+
$container = $this->getMockBuilder(Container::class)->getMock();
72+
$container->expects($this->exactly(2))->method('get')->willReturn($firewallContext);
73+
74+
$firewallMap = new FirewallMap($container, array('security.firewall.map.context.foo' => $matcher));
75+
76+
$this->assertEquals(array('LISTENERS', 'EXCEPTION LISTENER'), $firewallMap->getListeners($request));
77+
$this->assertEquals('CONFIG', $firewallMap->getFirewallConfig($request));
78+
$this->assertEquals('security.firewall.map.context.foo', $request->attributes->get(self::ATTRIBUTE_FIREWALL_CONTEXT));
79+
}
80+
}

0 commit comments

Comments
 (0)
0