Skip to content

Commit

Permalink
feature #27062 [SecurityBundle] Register a UserProviderInterface al…
Browse files Browse the repository at this point in the history
…ias if one provider only (sroze)

This PR was merged into the 4.1-dev branch.

Discussion
----------

[SecurityBundle] Register a `UserProviderInterface` alias if one provider only

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | ø
| License       | MIT
| Doc PR        | ø

When using only one user provider (as in most of the cases), it will register an alias for the `UserProviderInterface` interface and allow auto-wiring of it.

Commits
-------

4cbddd8 Register a `UserProviderInterface` alias if one provider only
  • Loading branch information
nicolas-grekas committed Apr 29, 2018
2 parents f07bcb2 + 4cbddd8 commit e902caa
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
use Symfony\Component\Security\Core\Encoder\Argon2iPasswordEncoder;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Http\Controller\UserValueResolver;

/**
Expand Down Expand Up @@ -180,6 +181,10 @@ private function createFirewalls($config, ContainerBuilder $container)
$arguments[1] = new IteratorArgument($userProviders);
$contextListenerDefinition->setArguments($arguments);

if (1 === \count($providerIds)) {
$container->setAlias(UserProviderInterface::class, current($providerIds));
}

$customUserChecker = false;

// load firewall map
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\Security\Core\User\UserProviderInterface;

class SecurityExtensionTest extends TestCase
{
Expand Down Expand Up @@ -270,6 +271,51 @@ public function testRemovesExpressionCacheWarmerDefinitionIfNoExpressions()
$this->assertFalse($container->hasDefinition('security.cache_warmer.expression'));
}

public function testRegisterTheUserProviderAlias()
{
$container = $this->getRawContainer();

$container->loadFromExtension('security', array(
'providers' => array(
'default' => array('id' => 'foo'),
),

'firewalls' => array(
'some_firewall' => array(
'pattern' => '/.*',
'http_basic' => null,
),
),
));

$container->compile();

$this->assertTrue($container->hasAlias(UserProviderInterface::class));
}

public function testDoNotRegisterTheUserProviderAliasWithMultipleProviders()
{
$container = $this->getRawContainer();

$container->loadFromExtension('security', array(
'providers' => array(
'first' => array('id' => 'foo'),
'second' => array('id' => 'bar'),
),

'firewalls' => array(
'some_firewall' => array(
'pattern' => '/.*',
'http_basic' => array('provider' => 'second'),
),
),
));

$container->compile();

$this->assertFalse($container->has(UserProviderInterface::class));
}

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

0 comments on commit e902caa

Please sign in to comment.