Skip to content

Commit 3d5b20d

Browse files
committed
[FrameworkBundle] fix KernelBrowser::loginUser with a stateless firewall
1 parent 0f47ae9 commit 3d5b20d

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

KernelBrowser.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,15 @@ public function loginUser($user, string $firewallContext = 'main'): self
122122

123123
$token = new TestBrowserToken($user->getRoles(), $user, $firewallContext);
124124
$token->setAuthenticated(true);
125-
$session = $this->getContainer()->get('session');
125+
126+
$container = $this->getContainer();
127+
$container->get('security.token_storage')->setToken($token);
128+
129+
if (!$container->has('session')) {
130+
return $this;
131+
}
132+
133+
$session = $container->get('session');
126134
$session->set('_security_'.$firewallContext, serialize($token));
127135
$session->save();
128136

Tests/Functional/SecurityTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function getUsers()
3838
yield ['the-username', ['ROLE_FOO'], null];
3939
yield ['the-username', ['ROLE_FOO'], 'main'];
4040
yield ['other-username', ['ROLE_FOO'], 'custom'];
41+
yield ['stateless-username', ['ROLE_FOO'], 'stateless'];
4142

4243
yield ['the-username', ['ROLE_FOO'], null];
4344
yield ['no-role-username', [], null];

Tests/Functional/app/Security/config.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ security:
1212
memory:
1313
users:
1414
other-username: { password: the-password, roles: ['ROLE_FOO'] }
15+
stateless:
16+
memory:
17+
users:
18+
stateless-username: { password: the-password, roles: ['ROLE_FOO'] }
1519

1620
firewalls:
1721
main:
@@ -24,3 +28,8 @@ security:
2428
form_login:
2529
check_path: /custom/login/check
2630
provider: custom
31+
stateless:
32+
pattern: ^/stateless
33+
stateless: true
34+
http_basic: ~
35+
provider: stateless

Tests/Functional/app/Security/routing.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ security_profile:
55
security_custom_profile:
66
path: /custom/user_profile
77
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SecurityController::profileAction }
8+
9+
security_stateless_profile:
10+
path: /stateless/user_profile
11+
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SecurityController::profileAction }

0 commit comments

Comments
 (0)