8000 [SecurityBundle] Allow accessing remember_me services via FirewallMap · symfony/symfony@7c1dbea · GitHub
[go: up one dir, main page]

Skip to content

Commit 7c1dbea

Browse files
committed
[SecurityBundle] Allow accessing remember_me services via FirewallMap
Use assertSame instead of assertEqual
1 parent f1c1e37 commit 7c1dbea

File tree

6 files changed

+103
-6
lines changed

6 files changed

+103
-6
lines changed

src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/RememberMeFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ public function create(ContainerBuilder $container, $id, $config, $userProvider,
4343
// remember me services
4444
if (isset($config['token_provider'])) {
4545
$templateId = 'security.authentication.rememberme.services.persistent';
46-
$rememberMeServicesId = $templateId.'.'.$id;
4746
} else {
4847
$templateId = 'security.authentication.rememberme.services.simplehash';
49-
$rememberMeServicesId = $templateId.'.'.$id;
5048
}
5149

50+
$rememberMeServicesId = $templateId.'.'.$id;
51+
5252
if ($container->hasDefinition('security.logout_listener.'.$id)) {
5353
$container
5454
->getDefinition('security.logout_listener.'.$id)

src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,13 @@ private function createFirewalls($config, ContainerBuilder $container)
244244
->replaceArgument(0, $listeners)
245245
->replaceArgument(1, $exceptionListener)
246246
;
247+
248+
if ($container->hasDefinition('security.authentication.rememberme.services.persistent.'.$name)) {
249+
$context->replaceArgument(2, new Reference('security.authentication.rememberme.services.persistent.'.$name));
250+
} elseif ($container->hasDefinition('security.authentication.rememberme.services.simplehash.'.$name)) {
251+
$context->replaceArgument(2, new Reference('security.authentication.rememberme.services.simplehash.'.$name));
252+
}
253+
247254
$map[$contextId] = $matcher;
248255
}
249256
$mapDef->replaceArgument(1, $map);

src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
<service id="security.firewall.context" class="Symfony\Bundle\SecurityBundle\Security\FirewallContext" abstract="true">
112112
<argument type="collection" />
113113
<argument type="service" id="security.exception_listener" />
114+
<argument type="service" on-invalid="null" />
114115
</service>
115116

116117
<service id="security.logout_url_generator" class="Symfony\Component\Security\Http\Logout\LogoutUrlGenerator" public="false">

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\SecurityBundle\Security;
1313

1414
use Symfony\Component\Security\Http\Firewall\ExceptionListener;
15+
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
1516

1617
/**
1718
* This is a wrapper around the actual firewall configuration which allows us
@@ -23,15 +24,22 @@ class FirewallContext
2324
{
2425
private $listeners;
2526
private $exceptionListener;
27+
private $rememberMeServices;
2628

27-
public function __construct(array $listeners, ExceptionListener $exceptionListener = null)
29+
public function __construct(array $listeners, ExceptionListener $exceptionListener = null, RememberMeServicesInterface $rememberMeServices = null)
2830
{
2931
$this->listeners = $listeners;
3032
$this->exceptionListener = $exceptionListener;
33+
$this->rememberMeServices = $rememberMeServices;
3134
}
3235

3336
public function getContext()
3437
{
3538
return array($this->listeners, $this->exceptionListener);
3639
}
40+
41+
public function getRememberMeServices()
42+
{
43+
return $this->rememberMeServices;
44+
}
3745
}

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

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\SecurityBundle\Security;
1313

1414
use Symfony\Component\Security\Http\FirewallMapInterface;
15+
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
1516
use Symfony\Component\HttpFoundation\Request;
1617
use Symfony\Component\DependencyInjection\ContainerInterface;
1718

@@ -33,14 +34,49 @@ public function __construct(ContainerInterface $container, array $map)
3334
$this->map = $map;
3435
}
3536

37+
/**
38+
* Gets the listeners for the given Request.
39+
*
40+
* @param Request $request
41+
*
42+
* @return array An array of listeners
43+
*/
3644
public function getListeners(Request $request)
45+
{
46+
$context = $this->getContext($request);
47+
48+
if (null === $context) {
49+
return array(array(), null);
50+
}
51+
52+
return $context->getContext();
53+
}
54+
55+
/**
56+
* Gets the remember me services for the given Request.
57+
*
58+
* @param Request $request
59+
*
60+
* @return RememberMeServicesInterface|null
61+
*/
62+
public function getRememberMeServices(Request $request)
63+
{
64+
$context = $this->getContext($request);
65+
66+
return $context ? $context->getRememberMeServices() : null;
67+
}
68+
69+
/**
70+
* @param Request $request
71+
*
72+
* @return FirewallContext|null
73+
*/
74+
private function getContext(Request $request)
3775
{
3876
foreach ($this->map as $contextId => $requestMatcher) {
3977
if (null === $requestMatcher || $requestMatcher->matches($request)) {
40-
return $this->container->get($contextId)->getContext();
78+
return $this->container->get($contextId);
4179
}
4280
}
43-
44-
return array(array(), null);
4581
}
4682
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 Symfony\Bundle\SecurityBundle\Security\FirewallContext;
15+
use Symfony\Component\Security\Http\Firewall\ExceptionListener;
16+
use Symfony\Component\Security\Http\Firewall\ListenerInterface;
17+
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
18+
19+
class FirewallContextTest extends \PHPUnit_Framework_TestCase
20+
{
21+
public function testGetters()
22+
{
23+
$rememberMeServices = $this
24+
->getMockBuilder(RememberMeServicesInterface::class)
25+
->disableOriginalConstructor()
26+
->getMock();
27+
28+
$exceptionListener = $this
29+
->getMockBuilder(ExceptionListener::class)
30+
->disableOriginalConstructor()
31+
->getMock();
32+
33+
$listeners = array(
34+
$this
35+
->getMockBuilder(ListenerInterface::class)
36+
->disableOriginalConstructor()
37+
->getMock(),
38+
);
39+
40+
$context = new FirewallContext($listeners, $exceptionListener, $rememberMeServices);
41+
42+
$this->assertSame(array($listeners, $exceptionListener), $context->getContext());
43+
$this->assertSame($rememberMeServices, $context->getRememberMeServices());
44+
}
45+
}

0 commit comments

Comments
 (0)
0