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

Skip to content

Commit f8cc3a0

Browse files
committed
[SecurityBundle] Allow accessing remember_me services via FirewallMap
1 parent 79259aa commit f8cc3a0

File tree

6 files changed

+44
-5
lines changed

6 files changed

+44
-5
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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,12 @@ private function createFirewalls($config, ContainerBuilder $container)
249249
->replaceArgument(2, new Reference($configId))
250250
;
251251

252+
if ($container->hasDefinition('security.authentication.rememberme.services.persistent.'.$name)) {
253+
$context->replaceArgument(3, new Reference('security.authentication.rememberme.services.persistent.'.$name));
254+
} elseif ($container->hasDefinition('security.authentication.rememberme.services.simplehash.'.$name)) {
255+
$context->replaceArgument(3, new Reference('security.authentication.rememberme.services.simplehash.'.$name));
256+
}
257+
252258
$map[$contextId] = $matcher;
253259
}
254260
$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
@@ -112,6 +112,7 @@
112112
<argument type="collection" />
113113
<argument type="service" id="security.exception_listener" />
114114
<argument /> <!-- FirewallConfig -->
115+
<argument type="service" on-invalid="null" />
115116
</service>
116117

117118
<service id="security.firewall.config" class="Symfony\Bundle\SecurityBundle\Security\FirewallConfig" abstract="true" 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
@@ -24,8 +25,9 @@ class FirewallContext
2425
private $listeners;
2526
private $exceptionListener;
2627
private $config;
28+
private $rememberMeServices;
2729

28-
public function __construct(array $listeners, ExceptionListener $exceptionListener = null, FirewallConfig $config = null)
30+
public function __construct(array $listeners, ExceptionListener $exceptionListener = null, FirewallConfig $config = null, RememberMeServicesInterface $rememberMeServices = null)
2931
{
3032
if (null === $config) {
3133
@trigger_error(sprintf('"%s()" expects an instance of "%s" as third argument since version 3.2 and will trigger an error in 4.0 if not provided.', __METHOD__, FirewallConfig::class), E_USER_DEPRECATED);
@@ -34,6 +36,7 @@ public function __construct(array $listeners, ExceptionListener $exceptionListen
3436
$this->listeners = $listeners;
3537
$this->exceptionListener = $exceptionListener;
3638
$this->config = $config;
39+
$this->rememberMeServices = $rememberMeServices;
3740
}
3841

3942
public function getConfig()
@@ -45,4 +48,9 @@ public function getContext()
4548
{
4649
return array($this->listeners, $this->exceptionListener);
4750
}
51+
52+
public function getRememberMeServices()
53+
{
54+
return $this->rememberMeServices;
55+
}
4856
}

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

Lines changed: 19 additions & 0 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

@@ -63,6 +64,24 @@ public function getFirewallConfig(Request $request)
6364
return $context->getConfig();
6465
}
6566

67+
/**
68+
* Gets the remember me services for the given Request.
69+
*
70+
* @param Request $request
71+
*
72+
* @return RememberMeServicesInterface|null
73+
*/
74+
public function getRememberMeServices(Request $request)
75+
{
76+
$context = $this->getFirewallContext($request);
77+
78+
if (null === $context) {
79+
return;
80+
}
81+
82+
return $context->getRememberMeServices();
83+
}
84+
6685
private function getFirewallContext(Request $request)
6786
{
6887
if ($this->contexts->contains($request)) {

src/Symfony/Bundle/SecurityBundle/Tests/Security/FirewallContextTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@
1515
use Symfony\Bundle\SecurityBundle\Security\FirewallContext;
1616
use Symfony\Component\Security\Http\Firewall\ExceptionListener;
1717
use Symfony\Component\Security\Http\Firewall\ListenerInterface;
18+
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
1819

1920
class FirewallContextTest extends \PHPUnit_Framework_TestCase
2021
{
2122
public function testGetters()
2223
{
2324
$config = new FirewallConfig('main', 'request_matcher', 'user_checker');
24-
25+
$rememberMeServices = $this
26+
->getMockBuilder(RememberMeServicesInterface::class)
27+
->disableOriginalConstructor()
28+
->getMock();
2529
$exceptionListener = $this
2630
->getMockBuilder(ExceptionListener::class)
2731
->disableOriginalConstructor()
@@ -34,9 +38,10 @@ public function testGetters()
3438
->getMock(),
3539
);
3640

37-
$context = new FirewallContext($listeners, $exceptionListener, $config);
41+
$context = new FirewallContext($listeners, $exceptionListener, $config, $rememberMeServices);
3842

3943
$this->assertEquals(array($listeners, $exceptionListener), $context->getContext());
4044
$this->assertEquals($config, $context->getConfig());
45+
$this->assertEquals($rememberMeServices, $context->getRememberMeServices());
4146
}
4247
}

0 commit comments

Comments
 (0)
0