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

Skip to content

Commit df9e848

Browse files
committed
Make user lazy loading working without user provider
1 parent 494ef42 commit df9e848

File tree

8 files changed

+63
-20
lines changed

8 files changed

+63
-20
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,13 @@ private function createFirewalls(array $config, ContainerBuilder $container)
249249
}
250250
$arguments[1] = $userProviderIteratorsArgument = new IteratorArgument($userProviders);
251251
$contextListenerDefinition->setArguments($arguments);
252+
$nbUserProviders = \count($userProviders);
252253

253-
if (\count($userProviders) > 1) {
254+
if ($nbUserProviders > 1) {
254255
$container->setDefinition('security.user_providers', new Definition(ChainUserProvider::class, [$userProviderIteratorsArgument]))
255256
->setPublic(false);
257+
} elseif (0 === $nbUserProviders) {
258+
$container->removeDefinition('security.listener.user_provider');
256259
} else {
257260
$container->setAlias('security.user_providers', new Alias(current($providerIds)))->setPublic(false);
258261
}

src/Symfony/Bundle/SecurityBundle/Tests/Functional/AuthenticatorTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ public function testFirewallUserProvider($email, $withinFirewall)
4444
}
4545
}
4646

47+
/**
48+
* @dataProvider provideEmails
49+
*/
50+
public function testWithoutUserProvider($email)
51+
{
52+
$client = $this->createClient(['test_case' => 'Authenticator', 'root_config' => 'no_user_provider.yml']);
53+
54+
$client->request('GET', '/profile', [], [], [
55+
'HTTP_X-USER-EMAIL' => $email,
56+
]);
57+
58+
$this->assertJsonStringEqualsJsonString('{"email":"'.$email.'"}', $client->getResponse()->getContent());
59+
}
60+
4761
public function provideEmails()
4862
{
4963
yield ['[email protected]', true];

src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/AuthenticatorBundle/ApiAuthenticator.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,21 @@
1717
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1818
use Symfony\Component\Security\Core\Exception\AuthenticationException;
1919
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
20+
use Symfony\Component\Security\Core\User\User;
2021
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
2122
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
2223
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
2324
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
2425

2526
class ApiAuthenticator extends AbstractAuthenticator
2627
{
28+
private $selfLoadingUser = false;
29+
30+
public function __construct(bool $selfLoadingUser = false)
31+
{
32+
$this->selfLoadingUser = $selfLoadingUser;
33+
}
34+
2735
public function supports(Request $request): ?bool
2836
{
2937
return $request->headers->has('X-USER-EMAIL');
@@ -36,7 +44,12 @@ public function authenticate(Request $request): PassportInterface
3644
throw new BadCredentialsException('Email is not a valid email address.');
3745
}
3846

39-
return new SelfValidatingPassport(new UserBadge($email));
47+
$userLoader = null;
48+
if ($this->selfLoadingUser) {
49+
$userLoader = function ($username) { return new User($username, 'test', ['ROLE_USER']); };
50+
}
51+
52+
return new SelfValidatingPassport(new UserBadge($email, $userLoader));
4053
}
4154

4255
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response

src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/Authenticator/config.yml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,3 @@ services:
1515
- ['setContainer', ['@Psr\Container\ContainerInterface']]
1616
tags: [container.service_subscriber]
1717
Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle\ApiAuthenticator: ~
18-
19-
security:
20-
enable_authenticator_manager: true
21-
22-
encoders:
23-
Symfony\Component\Security\Core\User\User: plaintext
24-
25-
providers:
26-
in_memory:
27-
memory:
28-
users:
29-
'[email protected]': { password: test, roles: [ROLE_USER] }
30-
in_memory2:
31-
memory:
32-
users:
33-
'[email protected]': { password: test, roles: [ROLE_USER] }
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
imports:
22
- { resource: ./config.yml }
3+
- { resource: ./security.yml }
34

45
security:
56
firewalls:
67
api:
78
pattern: /
89
provider: in_memory
910
custom_authenticator: Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle\ApiAuthenticator
10-
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
imports:
22
- { resource: ./config.yml }
3+
- { resource: ./security.yml }
34

45
security:
56
firewalls:
67
api:
78
pattern: /
89
custom_authenticator: Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle\ApiAuthenticator
9-
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
imports:
2+
- { resource: ./config.yml }
3+
4+
services:
5+
Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle\ApiAuthenticator:
6+
- true
7+
8+
security:
9+
enable_authenticator_manager: true
10+
11+
firewalls:
12+
api:
13+
pattern: /
14+
custom_authenticator: Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AuthenticatorBundle\ApiAuthenticator
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
security:
2+
enable_authenticator_manager: true
3+
4+
encoders:
5+
Symfony\Component\Security\Core\User\User: plaintext
6+
7+
providers:
8+
in_memory:
9+
memory:
10+
users:
11+
'[email protected]': { password: test, roles: [ROLE_USER] }
12+
in_memory2:
13+
memory:
14+
users:
15+
'[email protected]': { password: test, roles: [ROLE_USER] }

0 commit comments

Comments
 (0)