Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Security] deprecate multiple providers in context listener #21792

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

Merged
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
3 changes: 3 additions & 0 deletions 3 UPGRADE-3.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ Process
Security
--------

* Deprecated the ability to pass multiple user providers to the `ContextListener`. Pass only the user provider responsible
for the active firewall instead.
Copy link
Member

Choose a reason for hiding this comment

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

looks like it's not wrapped at 80 chars


* The `RoleInterface` has been deprecated. Extend the `Symfony\Component\Security\Core\Role\Role`
class in your custom role implementations instead.

Expand Down
3 changes: 3 additions & 0 deletions 3 UPGRADE-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ Process
Security
--------

* Dropped support for passing multiple user providers to the `ContextListener`. Pass only the user provider responsible
for the active firewall instead.

* The `RoleInterface` has been removed. Extend the `Symfony\Component\Security\Core\Role\Role`
class instead.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,6 @@ private function createFirewalls($config, ContainerBuilder $container)
$firewalls = $config['firewalls'];
$providerIds = $this->createUserProviders($config, $container);

// make the ContextListener aware of the configured user providers
$definition = $container->getDefinition('security.context_listener');
$arguments = $definition->getArguments();
$userProviders = array();
foreach ($providerIds as $userProviderId) {
$userProviders[] = new Reference($userProviderId);
}
$arguments[1] = $userProviders;
$definition->setArguments($arguments);

// load firewall map
$mapDef = $container->getDefinition('security.firewall.map');
$map = $authenticationProviders = $contextRefs = array();
Expand Down Expand Up @@ -327,7 +317,7 @@ private function createFirewall(ContainerBuilder $container, $id, $firewall, &$a
$contextKey = $firewall['context'];
}

$listeners[] = new Reference($this->createContextListener($container, $contextKey));
$listeners[] = new Reference($this->createContextListener($container, $contextKey, $defaultProvider));
}

$config->replaceArgument(6, $contextKey);
Expand Down Expand Up @@ -436,14 +426,15 @@ private function createFirewall(ContainerBuilder $container, $id, $firewall, &$a
return array($matcher, $listeners, $exceptionListener);
}

private function createContextListener($container, $contextKey)
private function createContextListener($container, $contextKey, $providerId)
{
if (isset($this->contextListeners[$contextKey])) {
return $this->contextListeners[$contextKey];
}

$listenerId = 'security.context_listener.'.count($this->contextListeners);
$listener = $container->setDefinition($listenerId, new ChildDefinition('security.context_listener'));
$listener->replaceArgument(1, new Reference($providerId));
$listener->replaceArgument(2, $contextKey);

return $this->contextListeners[$contextKey] = $listenerId;
Expand Down
6 changes: 6 additions & 0 deletions 6 src/Symfony/Component/Security/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

3.3.0
-----

* Deprecated the ability to pass multiple user providers to the `ContextListener`. Pass only the user provider responsible
for the active firewall instead.

3.2.0
-----

Expand Down
16 changes: 15 additions & 1 deletion 16 src/Symfony/Component/Security/Http/Firewall/ContextListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,26 @@ class ContextListener implements ListenerInterface
private $registered;
private $trustResolver;

public function __construct(TokenStorageInterface $tokenStorage, array $userProviders, $contextKey, LoggerInterface $logger = null, EventDispatcherInterface $dispatcher = null, AuthenticationTrustResolverInterface $trustResolver = null)
/**
* @param TokenStorageInterface $tokenStorage
* @param UserProviderInterface|UserProviderInterface[] $userProviders
* @param string $contextKey
* @param LoggerInterface|null $logger
* @param EventDispatcherInterface|null $dispatcher
* @param AuthenticationTrustResolverInterface|null $trustResolver
*/
public function __construct(TokenStorageInterface $tokenStorage, $userProviders, $contextKey, LoggerInterface $logger = null, EventDispatcherInterface $dispatcher = null, AuthenticationTrustResolverInterface $trustResolver = null)
{
if (empty($contextKey)) {
throw new \InvalidArgumentException('$contextKey must not be empty.');
}

if (is_array($userProviders)) {
@trigger_error(sprintf('Being able to pass multiple user providers to the constructor of %s is deprecated since version 3.3 and will not be supported anymore in 4.0. Only pass the user provider for the current firewall context instead.', __CLASS__), E_USER_DEPRECATED);
Copy link
Member

Choose a reason for hiding this comment

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

missing " around %s

} else {
$userProviders = array($userProviders);
}

foreach ($userProviders as $userProvider) {
if (!$userProvider instanceof UserProviderInterface) {
throw new \InvalidArgumentException(sprintf('User provider "%s" must implement "Symfony\Component\Security\Core\User\UserProviderInterface".', get_class($userProvider)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Http\Firewall\ContextListener;
use Symfony\Component\EventDispatcher\EventDispatcher;

Expand All @@ -35,12 +36,13 @@ public function testItRequiresContextKey()
{
new ContextListener(
$this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock(),
array(),
$this->getMockBuilder(UserProviderInterface::class)->getMock(),
''
);
}

/**
* @group legacy
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage User provider "stdClass" must implement "Symfony\Component\Security\Core\User\UserProviderInterface
*/
Expand Down Expand Up @@ -109,7 +111,7 @@ public function testOnKernelResponseWithoutSession()
new Response()
);

$listener = new ContextListener($tokenStorage, array(), 'session', null, new EventDispatcher());
$listener = new ContextListener($tokenStorage, $this->getMockBuilder(UserProviderInterface::class)->getMock(), 'session', null, new EventDispatcher());
$listener->onKernelResponse($event);

$this->assertTrue($session->isStarted());
Expand All @@ -128,7 +130,7 @@ public function testOnKernelResponseWithoutSessionNorToken()
new Response()
);

$listener = new ContextListener(new TokenStorage(), array(), 'session', null, new EventDispatcher());
$listener = new ContextListener(new TokenStorage(), $this->getMockBuilder(UserProviderInterface::class)->getMock(), 'session', null, new EventDispatcher());
$listener->onKernelResponse($event);

$this->assertFalse($session->isStarted());
Expand Down Expand Up @@ -163,7 +165,7 @@ public function testInvalidTokenInSession($token)
->method('setToken')
->with(null);

$listener = new ContextListener($tokenStorage, array(), 'key123');
$listener = new ContextListener($tokenStorage, $this->getMockBuilder(UserProviderInterface::class)->getMock(), 'key123');
$listener->handle($event);
}

Expand All @@ -184,7 +186,7 @@ public function testHandleAddsKernelResponseListener()
->disableOriginalConstructor()
->getMock();

$listener = new ContextListener($tokenStorage, array(), 'key123', null, $dispatcher);
$listener = new ContextListener($tokenStorage, $this->getMockBuilder(UserProviderInterface::class)->getMock(), 'key123', null, $dispatcher);

$event->expects($this->any())
->method('isMasterRequest')
Expand All @@ -208,7 +210,7 @@ public function testOnKernelResponseListenerRemovesItself()
->disableOriginalConstructor()
->getMock();

$listener = new ContextListener($tokenStorage, array(), 'key123', null, $dispatcher);
$listener = new ContextListener($tokenStorage, $this->getMockBuilder(UserProviderInterface::class)->getMock(), 'key123', null, $dispatcher);

$request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
$request->expects($this->any())
Expand Down Expand Up @@ -242,7 +244,7 @@ public function testHandleRemovesTokenIfNoPreviousSessionWasFound()
$tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock();
$tokenStorage->expects($this->once())->method('setToken')->with(null);

$listener = new ContextListener($tokenStorage, array(), 'key123');
$listener = new ContextListener($tokenStorage, $this->getMockBuilder(UserProviderInterface::class)->getMock(), 'key123');
$listener->handle($event);
}

Expand All @@ -268,7 +270,7 @@ protected function runSessionOnKernelResponse($newToken, $original = null)
new Response()
);

$listener = new ContextListener($tokenStorage, array(), 'session', null, new EventDispatcher());
$listener = new ContextListener($tokenStorage, $this->getMockBuilder(UserProviderInterface::class)->getMock(), 'session', null, new EventDispatcher());
$listener->onKernelResponse($event);

return $session;
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.