8000 Add a CurrentUser attribute to force the UserValueResolver · symfony/symfony@c699230 · GitHub
[go: up one dir, main page]

Skip to content

Commit c699230

Browse files
committed
Add a CurrentUser attribute to force the UserValueResolver
1 parent 79a1a7b commit c699230

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed

src/Symfony/Component/Security/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* Changed `AuthorizationChecker` to call the access decision manager in unauthenticated sessions with a `NullToken`
99
* [BC break] Removed `AccessListener::PUBLIC_ACCESS` in favor of `AuthenticatedVoter::PUBLIC_ACCESS`
1010
* Added `Passport` to `LoginFailureEvent`.
11+
* Added a CurrentUser attribute to force the UserValueResolver to resolve an argument to the current user.
1112

1213
5.1.0
1314
-----
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Http\Attribute;
13+
14+
use Attribute;
15+
use Symfony\Component\HttpKernel\Attribute\ArgumentInterface;
16+
17+
/**
18+
* Indicates that a controller argument should receive the current logged user.
19+
*/
20+
@@Attribute(Attribute::TARGET_PARAMETER)
21+
class CurrentUser implements ArgumentInterface
22+
{
23+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
1818
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1919
use Symfony\Component\Security\Core\User\UserInterface;
20+
use Symfony\Component\Security\Http\Attribute\CurrentUser;
2021

2122
/**
2223
* Supports the argument type of {@see UserInterface}.
@@ -34,6 +35,10 @@ public function __construct(TokenStorageInterface $tokenStorage)
3435

3536
public function supports(Request $request, ArgumentMetadata $argument): bool
3637
{
38+
if ($argument->getAttribute() instanceof CurrentUser) {
39+
return true;
40+
}
41+
3742
// only security user implementations are supported
3843
if (UserInterface::class !== $argument->getType()) {
3944
return false;

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
2020
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
2121
use Symfony\Component\Security\Core\User\UserInterface;
22+
use Symfony\Component\Security\Http\Attribute\CurrentUser;
2223
use Symfony\Component\Security\Http\Controller\UserValueResolver;
2324

2425
class UserValueResolverTest extends TestCase
@@ -68,6 +69,23 @@ public function testResolve()
6869
$this->assertSame([$user], iterator_to_array($resolver->resolve(Request::create('/'), $metadata)));
6970
}
7071

72+
/**
73+
* @requires PHP 8
74+
*/
75+
public function testResolveWithAttribute()
76+
{
77+
$user = $this->getMockBuilder(UserInterface::class)->getMock();
78+
$token = new UsernamePasswordToken($user, 'password', 'provider');
79+
$tokenStorage = new TokenStorage();
80+
$tokenStorage->setToken($token);
81+
82+
$resolver = new UserValueResolver($tokenStorage);
83+
$metadata = new ArgumentMetadata('foo', null, false, false, null, false, new CurrentUser());
84+
85+
$this->assertTrue($resolver->supports(Request::create('/'), $metadata));
86+
$this->assertSame([$user], iterator_to_array($resolver->resolve(Request::create('/'), $metadata)));
87+
}
88+
7189
public function testIntegration()
7290
{
7391
$user = $this->getMockBuilder(UserInterface::class)->getMock();

src/Symfony/Component/Security/Http/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"symfony/deprecation-contracts": "^2.1",
2121
"symfony/security-core": "^5.2",
2222
"symfony/http-foundation": "^4.4.7|^5.0.7",
23-
"symfony/http-kernel": "^4.4|^5.0",
23+
"symfony/http-kernel": "^5.2",
2424
"symfony/polyfill-php80": "^1.15",
2525
"symfony/property-access": "^4.4|^5.0"
2626
},

0 commit comments

Comments
 (0)
0