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

Skip to content

Commit 8d3078d

Browse files
committed
[Security] #[CurrentUser] argument should resolve to null when it is anonymous
1 parent 5028aaf commit 8d3078d

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/Symfony/Component/Security/Http/Controller/UserValueResolver.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,9 @@ public function __construct(TokenStorageInterface $tokenStorage)
3535

3636
public function supports(Request $request, ArgumentMetadata $argument): bool
3737
{
38-
if ($argument->getAttribute() instanceof CurrentUser) {
39-
return true;
40-
}
41-
42-
// only security user implementations are supported
43-
if (UserInterface::class !== $argument->getType()) {
38+
// with the attribute, the type can be any UserInterface implementation
39+
// otherwise, the type must be UserInterface
40+
if (UserInterface::class !== $argument->getType() && !$argument->getAttribute() instanceof CurrentUser) {
4441
return false;
4542
}
4643

src/Symfony/Component/Security/Http/Tests/Controller/UserValueResolverTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ public function testResolveWithAttribute()
8383
$this->assertSame([$user], iterator_to_array($resolver->resolve(Request::create('/'), $metadata)));
8484
}
8585

86+
public function testResolveWithAttributeAndNoUser()
87+
{
88+
$tokenStorage = new TokenStorage();
89+
$tokenStorage->setToken(new UsernamePasswordToken('username', 'password', 'provider'));
90+
91+
$resolver = new UserValueResolver($tokenStorage);
92+
$metadata = new ArgumentMetadata('foo', null, false, false, null, false, new CurrentUser());
93+
94+
$this->assertFalse($resolver->supports(Request::create('/'), $metadata));
95+
}
96+
8697
public function testIntegration()
8798
{
8899
$user = $this->createMock(UserInterface::class);

0 commit comments

Comments
 (0)