8000 [Security] Make security.providers optional by MatTheCat · Pull Request #26787 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Security] Make security.providers optional #26787

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
Apr 19, 2018
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
[Security] Make security.providers optional
  • Loading branch information
Mathieu Lechat authored and fabpot committed Apr 19, 2018
commit ee54bfa6462ba328be02b093ffb59796b9eaff07
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ private function addProvidersSection(ArrayNodeDefinition $rootNode)
),
'my_entity_provider' => array('entity' => array('class' => 'SecurityBundle:User', 'property' => 'username')),
))
->isRequired()
->requiresAtLeastOneElement()
->useAttributeAsKey('name')
->prototype('array')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,9 @@ private function createAuthenticationListeners($container, $id, $firewall, &$aut
$userProvider = null;
} elseif ($defaultProvider) {
$userProvider = $defaultProvider;
} elseif (empty($providerIds)) {
$userProvider = sprintf('security.user.provider.missing.%s', $key);
$container->setDefinition($userProvider, (new ChildDefinition('security.user.provider.missing'))->replaceArgument(0, $id));
} else {
throw new InvalidConfigurationException(sprintf('Not configuring explicitly the provider for the "%s" listener on "%s" firewall is ambiguous as there is more than one registered provider.', $key, $id));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@
</service>

<!-- Provisioning -->
<service id="security.user.provider.missing" class="Symfony\Component\Security\Core\User\MissingUserProvider" abstract="true">
<argument /> <!-- firewall -->
</service>

10000 <service id="security.user.provider.in_memory" class="Symfony\Component\Security\Core\User\InMemoryUserProvider" abstract="true" />
<service id="security.user.provider.in_memory.user" class="Symfony\Component\Security\Core\User\User" abstract="true">
<deprecated>The "%service_id%" service is deprecated since Symfony 4.1.</deprecated>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\MissingUserProviderBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class MissingUserProviderBundle extends Bundle
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\SecurityBundle\Tests\Functional;

class MissingUserProviderTest extends WebTestCase
{
/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
* @expectedExceptionMessage "default" firewall requires a user provider but none was defined.
*/
public function testUserProviderIsNeeded()
{
$client = $this->createClient(array('test_case' => 'MissingUserProvider', 'root_config' => 'config.yml'));

$client->request('GET', '/', array(), array(), array(
'PHP_AUTH_USER' => 'username',
'PHP_AUTH_PW' => 'pa$$word',
));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\MissingUserProviderBundle\MissingUserProviderBundle;
use Symfony\Bundle\SecurityBundle\SecurityBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;

return array(
new FrameworkBundle(),
new SecurityBundle(),
new MissingUserProviderBundle(),
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
imports:
- { resource: ./../config/framework.yml }

security:
firewalls:
default:
http_basic: ~
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
home:
path: /
55 changes: 55 additions & 0 deletions src/Symfony/Component/Security/Core/User/MissingUserProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Security\Core\User;

use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;

/**
* MissingUserProvider is a dummy user provider used to throw proper exception
* when a firewall requires a user provider but none was defined.
*
* @internal
*/
class MissingUserProvider implements UserProviderInterface
{
/**
* @param string $firewall the firewall missing a provider
*/
public function __construct(string $firewall)
{
throw new InvalidConfigurationException(sprintf('"%s" firewall requires a user provider but none was defined.', $firewall));
}

/**
* {@inheritdoc}
*/
public function loadUserByUsername($username)
{
throw new \BadMethodCallException();
}

/**
* {@inheritdoc}
*/
public function refreshUser(UserInterface $user)
{
throw new \BadMethodCallException();
}

/**
* {@inheritdoc}
*/
public function supportsClass($class)
{
throw new \BadMethodCallException();
}
}
0