8000 [SecurityBundle] Allow accessing remember_me services via FirewallMap by chalasr · Pull Request #19819 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[SecurityBundle] Allow accessing remember_me services via FirewallMap #19819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public function create(ContainerBuilder $container, $id, $config, $userProvider,
// remember me services
if (isset($config['token_provider'])) {
$templateId = 'security.authentication.rememberme.services.persistent';
$rememberMeServicesId = $templateId.'.'.$id;
} else {
$templateId = 'security.authentication.rememberme.services.simplehash';
$rememberMeServicesId = $templateId.'.'.$id;
}

$rememberMeServicesId = $templateId.'.'.$id;

if ($container->hasDefinition('security.logout_listener.'.$id)) {
$container
->getDefinition('security.logout_listener.'.$id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ private function createFirewalls($config, ContainerBuilder $container)
->replaceArgument(2, new Reference($configId))
;

if ($container->hasDefinition('security.authentication.rememberme.services.persistent.'.$name)) {
$context->replaceArgument(3, new Reference('security.authentication.rememberme.services.persistent.'.$name));
} elseif ($container->hasDefinition('security.authentication.rememberme.services.simplehash.'.$name)) {
$context->replaceArgument(3, new Reference('security.authentication.rememberme.services.simplehash.'.$name));
}

$map[$contextId] = $matcher;
}
$mapDef->replaceArgument(1, $map);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
<argument type="collection" />
<argument type="service" id="security.exception_listener" />
<argument /> <!-- FirewallConfig -->
<argument type="service" on-invalid="null" />
</service>

<service id="security.firewall.config" class="Symfony\Bundle\SecurityBundle\Security\FirewallConfig" abstract="true" public="false">
Expand Down
10 changes: 9 additions & 1 deletion src/Symfony/Bundle/SecurityBundle/Security/FirewallContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\SecurityBundle\Security;

use Symfony\Component\Security\Http\Firewall\ExceptionListener;
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;

/**
* This is a wrapper around the actual firewall configuration which allows us
Expand All @@ -24,8 +25,9 @@ class FirewallContext
private $listeners;
private $exceptionListener;
private $config;
private $rememberMeServices;

public function __construct(array $listeners, ExceptionListener $exceptionListener = null, FirewallConfig $config = null)
public function __construct(array $listeners, ExceptionListener $exceptionListener = null, FirewallConfig $config = null, RememberMeServicesInterface $rememberMeServices = null)
{
if (null === $config) {
@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);
Expand All @@ -34,6 +36,7 @@ public function __construct(array $listeners, ExceptionListener $exceptionListen
$this->listeners = $listeners;
$this->exceptionListener = $exceptionListener;
$this->config = $config;
$this->rememberMeServices = $rememberMeServices;
}

public function getConfig()
Expand All @@ -45,4 +48,9 @@ public function getContext()
{
return array($this->listeners, $this->exceptionListener);
}

8000 public function getRememberMeServices()
{
return $this->rememberMeServices;
}
}
19 changes: 19 additions & 0 deletions src/Symfony/Bundle/SecurityBundle/Security/FirewallMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\SecurityBundle\Security;

use Symfony\Component\Security\Http\FirewallMapInterface;
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerInterface;

Expand Down Expand Up @@ -63,6 +64,24 @@ public function getFirewallConfig(Request $request)
return $context->getConfig();
}

/**
* Gets the remember me services for the given Request.
*
* @param Request $request
*
* @return RememberMeServicesInterface|null
*/
public function getRememberMeServices(Request $request)
{
$context = $this->getFirewallContext($request);

if (null === $context) {
return;
}

return $context->getRememberMeServices();
}

private function getFirewallContext(Request $request)
{
if ($this->contexts->contains($request)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@
use Symfony\Bundle\SecurityBundle\Security\FirewallContext;
use Symfony\Component\Security\Http\Firewall\ExceptionListener;
use Symfony\Component\Security\Http\Firewall\ListenerInterface;
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;

class FirewallContextTest extends \PHPUnit_Framework_TestCase
{
public function testGetters()
{
$config = new FirewallConfig('main', 'request_matcher', 'user_checker');

$rememberMeServices = $this
->getMockBuilder(RememberMeServicesInterface::class)
->disableOriginalConstructor()
->getMock();
$exceptionListener = $this
->getMockBuilder(ExceptionListener::class)
->disableOriginalConstructor()
Expand All @@ -34,9 +39,10 @@ public function testGetters()
->getMock(),
);

$context = new FirewallContext($listeners, $exceptionListener, $config);
$context = new FirewallContext($listeners, $exceptionListener, $config, $rememberMeServices);

$this->assertEquals(array($listeners, $exceptionListener), $context->getContext());
$this->assertEquals($config, $context->getConfig());
$this->assertSame(array($listeners, $exceptionListener), $context->getContext());
$this->assertSame($config, $context->getConfig());
$this->assertSame($rememberMeServices, $context->getRememberMeServices());
}
}
0