Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8c4fd12

Browse files
committed
feature #26787 [Security] Make security.providers optional (MatTheCat)
This PR was squashed before being merged into the 4.1-dev branch (closes #26787). Discussion ---------- [Security] Make security.providers optional | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #21998 | License | MIT Don't really know if it's viable but I just hit #21998 so I would like to tackle this. Commits ------- ee54bfa [Security] Make security.providers optional
2 parents a59d0f6 + ee54bfa commit 8c4fd12

File tree

9 files changed

+138
-1
lines changed

9 files changed

+138
-1
lines changed

src/Symfony/Bundle/SecurityBundle/DependencyInjection/MainConfiguration.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,6 @@ private function addProvidersSection(ArrayNodeDefinition $rootNode)
314314
),
315315
'my_entity_provider' => array('entity' => array('class' => 'SecurityBundle:User', 'property' => 'username')),
316316
))
317-
->isRequired()
318317
->requiresAtLeastOneElement()
319318
->useAttributeAsKey('name')
320319
->prototype('array')

src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,9 @@ private function createAuthenticationListeners($container, $id, $firewall, &$aut
422422
$userProvider = null;
423423
} elseif ($defaultProvider) {
424424
$userProvider = $defaultProvider;
425+
} elseif (empty($providerIds)) {
426+
$userProvider = sprintf('security.user.provider.missing.%s', $key);
427+
$container->setDefinition($userProvider, (new ChildDefinition('security.user.provider.missing'))->replaceArgument(0, $id));
425428
} else {
426429
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));
427430
}

src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@
166166
</service>
167167

168168
<!-- Provisioning -->
169+
<service id="security.user.provider.missing" class="Symfony\Component\Security\Core\User\MissingUserProvider" abstract="true">
170+
<argument /> <!-- firewall -->
171+
</service>
172+
169173
<service id="security.user.provider.in_memory" class="Symfony\Component\Security\Core\User\InMemoryUserProvider" abstract="true" />
170174
<service id="security.user.provider.in_memory.user" class="Symfony\Component\Security\Core\User\User" abstract="true">
171175
<deprecated>The "%service_id%" service is deprecated since Symfony 4.1.</deprecated>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\MissingUserProviderBundle;
13+
14+
use Symfony\Component\HttpKernel\Bundle\Bundle;
15+
16+
class MissingUserProviderBundle extends Bundle
17+
{
18+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\SecurityBundle\Tests\Functional;
13+
14+
class MissingUserProviderTest extends WebTestCase
15+
{
16+
/**
17+
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
18+
* @expectedExceptionMessage "default" firewall requires a user provider but none was defined.
19+
*/
20+
public function testUserProviderIsNeeded()
21+
{
22+
$client = $this->createClient(array('test_case' => 'MissingUserProvider', 'root_config' => 'config.yml'));
23+
24+
$client->request('GET', '/', array(), array(), array(
25+
'PHP_AUTH_USER' => 'username',
26+
'PHP_AUTH_PW' => 'pa$$word',
27+
));
28+
}
29+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\MissingUserProviderBundle\MissingUserProviderBundle;
13+
use Symfony\Bundle\SecurityBundle\SecurityBundle;
14+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
15+
16+
return array(
17+
new FrameworkBundle(),
18+
new SecurityBundle(),
19+
new MissingUserProviderBundle(),
20+
);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
imports:
2+
- { resource: ./../config/framework.yml }
3+
4+
security:
5+
firewalls:
6+
default:
7+
http_basic: ~
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
home:
2+
path: /
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Security\Core\User;
13+
14+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
15+
16+
/**
17+
* MissingUserProvider is a dummy user provider used to throw proper exception
18+
* when a firewall requires a user provider but none was defined.
19+
*
20+
* @internal
21+
*/
22+
class MissingUserProvider implements UserProviderInterface
23+
{
24+
/**
25+
* @param string $firewall the firewall missing a provider
26+
*/
27+
public function __construct(string $firewall)
28+
{
29+
throw new InvalidConfigurationException(sprintf('"%s" firewall requires a user provider but none was defined.', $firewall));
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function loadUserByUsername($username)
36+
{
37+
throw new \BadMethodCallException();
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function refreshUser(UserInterface $user)
44+
{
45+
throw new \BadMethodCallException();
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function supportsClass($class)
52+
{
53+
throw new \BadMethodCallException();
54+
}
55+
}

0 commit comments

Comments
 (0)