Skip to content

[SecurityBundle] Don't trigger auto-picking notice if provider is set per listener #25045

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
merged 1 commit into from
Nov 20, 2017
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
Original file line number Diff line number Diff line change
Expand Up @@ -346,17 +346,14 @@ private function createFirewall(ContainerBuilder $container, $id, $firewall, &$a
$config->replaceArgument(4, $firewall['stateless']);

// Provider id (take the first registered provider if none defined)
$defaultProvider = null;
if (isset($firewall['provider'])) {
if (!isset($providerIds[$normalizedName = str_replace('-', '_', $firewall['provider'])])) {
throw new InvalidConfigurationException(sprintf('Invalid firewall "%s": user provider "%s" not found.', $id, $firewall['provider']));
}
$defaultProvider = $providerIds[$normalizedName];
} else {
} elseif (1 === count($providerIds)) {
$defaultProvider = reset($providerIds);

if (count($providerIds) > 1) {
@trigger_error(sprintf('Firewall "%s" has no "provider" set but multiple providers exist. Using the first configured provider (%s) is deprecated since 3.4 and will throw an exception in 4.0, set the "provider" key on the firewall instead.', $id, key($providerIds)), E_USER_DEPRECATED);
}
}

$config->replaceArgument(5, $defaultProvider);
Expand Down Expand Up @@ -500,7 +497,7 @@ private function createContextListener($container, $contextKey)
return $this->contextListeners[$contextKey] = $listenerId;
}

private function createAuthenticationListeners($container, $id, $firewall, &$authenticationProviders, $defaultProvider, array $providerIds, $defaultEntryPoint)
private function createAuthenticationListeners($container, $id, $firewall, &$authenticationProviders, $defaultProvider = null, array $providerIds, $defaultEntryPoint)
{
$listeners = array();
$hasListeners = false;
Expand All @@ -516,7 +513,7 @@ private function createAuthenticationListeners($container, $id, $firewall, &$aut
}
$userProvider = $providerIds[$normalizedName];
} else {
$userProvider = $defaultProvider;
$userProvider = $defaultProvider ?: $this->getFirstProvider($id, $key, $providerIds);
}

list($provider, $listenerId, $defaultEntryPoint) = $factory->create($container, $id, $firewall[$key], $userProvider, $defaultEntryPoint);
Expand Down Expand Up @@ -699,7 +696,7 @@ private function createExceptionListener($container, $config, $id, $defaultEntry

private function createSwitchUserListener($container, $id, $config, $defaultProvider, $stateless)
{
$userProvider = isset($config['provider']) ? $this->getUserProviderId($config['provider']) : $defaultProvider;
$userProvider = isset($config['provider']) ? $this->getUserProviderId($config['provider']) : ($defaultProvider ?: $this->getFirstProvider($id, 'switch_user', $providerIds));

// in 4.0, ignore the `switch_user.stateless` key if $stateless is `true`
if ($stateless && false === $config['stateless']) {
Expand Down Expand Up @@ -803,4 +800,14 @@ private function getExpressionLanguage()

return $this->expressionLanguage;
}

/**
* @deprecated since version 3.4, to be removed in 4.0
*/
private function getFirstProvider($firewallName, $listenerName, array $providerIds)
{
@trigger_error(sprintf('Listener "%s" on firewall "%s" has no "provider" set but multiple providers exist. Using the first configured provider (%s) is deprecated since 3.4 and will throw an exception in 4.0, set the "provider" key on the firewall instead.', $listenerName, $firewallName, $first = array_keys($providerIds)[0]), E_USER_DEPRECATED);

return $providerIds[$first];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public function testSwitchUserNotStatelessOnStatelessFirewall()

/**
* @group legacy
* @expectedDeprecation Firewall "default" has no "provider" set but multiple providers exist. Using the first configured provider (first) is deprecated since 3.4 and will throw an exception in 4.0, set the "provider" key on the firewall instead.
* @expectedDeprecation Listener "http_basic" on firewall "default" has no "provider" set but multiple providers exist. Using the first configured provider (first) is deprecated since 3.4 and will throw an exception in 4.0, set the "provider" key on the firewall instead.
*/
public function testDeprecationForAmbiguousProvider()
{
Expand All @@ -199,6 +199,27 @@ public function testDeprecationForAmbiguousProvider()
$container->compile();
}

public function testPerListenerProvider()
{
$container = $this->getRawContainer();
$container->loadFromExtension('security', array(
'providers' => array(
'first' => array('id' => 'foo'),
'second' => array('id' => 'bar'),
),

'firewalls' => array(
'default' => array(
'http_basic' => array('provider' => 'second'),
'logout_on_user_change' => true,
),
),
));

$container->compile();
$this->addToAssertionCount(1);
}

protected function getRawContainer()
{
$container = new ContainerBuilder();
Expand Down