Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/Symfony/Bundle/SecurityBundle/Security/FirewallMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\SecurityBundle\Security;

use Symfony\Bundle\SecurityBundle\Security\FirewallContext;
use Symfony\Component\Security\Http\FirewallMapInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerInterface;
Expand All @@ -26,13 +27,11 @@ class FirewallMap implements FirewallMapInterface
{
protected $container;
protected $map;
private $contexts;

public function __construct(ContainerInterface $container, array $map)
{
$this->container = $container;
$this->map = $map;
$this->contexts = new \SplObjectStorage();
}

/**
Expand Down Expand Up @@ -63,15 +62,27 @@ public function getFirewallConfig(Request $request)
return $context->getConfig();
}

/**
* @return FirewallContext
*/
private function getFirewallContext(Request $request)
{
if ($this->contexts->contains($request)) {
return $this->contexts[$request];
if ($request->attributes->has('_firewall_context')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should validate that $request->attributes->get('_firewall_context') is a valid key of $this->map

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. And added test assertions on the value of this attribute.

$storedContextId = $request->attributes->get('_firewall_context');
foreach ($this->map as $contextId => $requestMatcher) {
if ($contextId === $storedContextId) {
return $this->container->get($contextId);
}
}

$request->attributes->remove('_firewall_context');
}

foreach ($this->map as $contextId => $requestMatcher) {
if (null === $requestMatcher || $requestMatcher->matches($request)) {
return $this->contexts[$request] = $this->container->get($contextId);
$request->attributes->set('_firewall_context', $contextId);

return $this->container->get($contextId);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\SecurityBundle\Tests\Security;

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\SecurityBundle\Security\FirewallContext;
use Symfony\Bundle\SecurityBundle\Security\FirewallMap;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestMatcherInterface;

class FirewallMapTest extends TestCase
{
const ATTRIBUTE_FIREWALL_CONTEXT = '_firewall_context';

public function testGetListenersWithEmptyMap()
{
$request = new Request();

$map = array();
$container = $this->getMockBuilder(Container::class)->getMock();
$container->expects($this->never())->method('get');

$firewallMap = new FirewallMap($container, $map);

$this->assertEquals(array(array(), null), $firewallMap->getListeners($request));
$this->assertNull($firewallMap->getFirewallConfig($request));
$this->assertFalse($request->attributes->has(self::ATTRIBUTE_FIREWALL_CONTEXT));
}

public function testGetListenersWithInvalidParameter()
{
$request = new Request();
$request->attributes->set(self::ATTRIBUTE_FIREWALL_CONTEXT, 'foo');

$map = array();
$container = $this->getMockBuilder(Container::class)->getMock();
$container->expects($this->never())->method('get');

$firewallMap = new FirewallMap($container, $map);

$this->assertEquals(array(array(), null), $firewallMap->getListeners($request));
$this->assertNull($firewallMap->getFirewallConfig($request));
$this->assertFalse($request->attributes->has(self::ATTRIBUTE_FIREWALL_CONTEXT));
}

public function testGetListeners()
{
$request = new Request();

$firewallContext = $this->getMockBuilder(FirewallContext::class)->disableOriginalConstructor()->getMock();
$firewallContext->expects($this->once())->method('getConfig')->willReturn('CONFIG');
$firewallContext->expects($this->once())->method('getContext')->willReturn(array('LISTENERS', 'EXCEPTION LISTENER'));
Copy link
Member Author

@GromNaN GromNaN Jun 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After merging into 3.3, getContext is replaced by getListeners and getExceptionListener, update this line to:

$firewallContext->expects($this->once())->method('getListeners')->willReturn('LISTENERS');
$firewallContext->expects($this->once())->method('getExceptionListener')->willReturn('EXCEPTION LISTENER');

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks


$matcher = $this->getMockBuilder(RequestMatcherInterface::class)->getMock();
$matcher->expects($this->once())
->method('matches')
->with($request)
->willReturn(true);

$container = $this->getMockBuilder(Container::class)->getMock();
$container->expects($this->exactly(2))->method('get')->willReturn($firewallContext);

$firewallMap = new FirewallMap($container, array('security.firewall.map.context.foo' => $matcher));

$this->assertEquals(array('LISTENERS', 'EXCEPTION LISTENER'), $firewallMap->getListeners($request));
$this->assertEquals('CONFIG', $firewallMap->getFirewallConfig($request));
$this->assertEquals('security.firewall.map.context.foo', $request->attributes->get(self::ATTRIBUTE_FIREWALL_CONTEXT));
}
}