Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8c33cd4

Browse filesBrowse files
committed
feature symfony#25588 Move SecurityUserValueResolver to security-http (chalasr)
This PR was merged into the 4.1-dev branch. Discussion ---------- Move SecurityUserValueResolver to security-http | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a Commits ------- 30a07e7 Move SecurityUserValueResolver to security-http
2 parents 88ed829 + 30a07e7 commit 8c33cd4
Copy full SHA for 8c33cd4

File tree

10 files changed

+179
-1
lines changed
Filter options

10 files changed

+179
-1
lines changed

‎UPGRADE-4.1.md

Copy file name to clipboardExpand all lines: UPGRADE-4.1.md
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ SecurityBundle
1818
--------------
1919

2020
* The `logout_on_user_change` firewall option is deprecated and will be removed in 5.0.
21+
* The `SecurityUserValueResolver` class is deprecated and will be removed in 5.0, use
22+
`Symfony\Component\Security\Http\Controller\UserValueResolver` instead.
2123

2224
Translation
2325
-----------

‎UPGRADE-5.0.md

Copy file name to clipboardExpand all lines: UPGRADE-5.0.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ SecurityBundle
1717
--------------
1818

1919
* The `logout_on_user_change` firewall option has been removed.
20+
* The `SecurityUserValueResolver` class has been removed.
2021

2122
Translation
2223
-----------

‎src/Symfony/Bundle/SecurityBundle/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/CHANGELOG.md
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ CHANGELOG
55
-----
66

77
* The `logout_on_user_change` firewall option is deprecated and will be removed in 5.0.
8+
* deprecated `SecurityUserValueResolver`, use
9+
`Symfony\Component\Security\Http\Controller\UserValueResolver` instead.
810

911
4.0.0
1012
-----

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SecurityFactoryInterface;
1515
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\UserProvider\UserProviderFactoryInterface;
16+
use Symfony\Bundle\SecurityBundle\SecurityUserValueResolver;
1617
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1718
use Symfony\Component\Console\Application;
1819
use Symfony\Component\DependencyInjection\Alias;
@@ -27,6 +28,7 @@
2728
use Symfony\Component\Security\Core\Authorization\ExpressionLanguage;
2829
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
2930
use Symfony\Component\Security\Core\Encoder\Argon2iPasswordEncoder;
31+
use Symfony\Component\Security\Http\Controller\UserValueResolver;
3032

3133
/**
3234
* SecurityExtension.
@@ -111,6 +113,10 @@ public function load(array $configs, ContainerBuilder $container)
111113
$container->getDefinition('security.command.user_password_encoder')->replaceArgument(1, array_keys($config['encoders']));
112114
}
113115

116+
if (!class_exists(UserValueResolver::class)) {
117+
$container->getDefinition('security.user_value_resolver')->setClass(SecurityUserValueResolver::class);
118+
}
119+
114120
$container->registerForAutoconfiguration(VoterInterface::class)
115121
->addTag('security.voter');
116122
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
</service>
4040
<service id="Symfony\Component\Security\Core\Security" alias="security.helper" />
4141

42-
<service id="security.user_value_resolver" class="Symfony\Bundle\SecurityBundle\SecurityUserValueResolver">
42+
<service id="security.user_value_resolver" class="Symfony\Component\Security\Http\Controller\UserValueResolver">
4343
<argument type="service" id="security.token_storage" />
4444
<tag name="controller.argument_value_resolver" priority="40" />
4545
</service>

‎src/Symfony/Bundle/SecurityBundle/SecurityUserValueResolver.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/SecurityUserValueResolver.php
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@
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\Controller\UserValueResolver;
21+
22+
@trigger_error(sprintf('The "%s" class is deprecated since version 4.1 and will be removed in 5.0, use "%s" instead.', SecurityUserValueResolver::class, UserValueResolver::class), E_USER_DEPRECATED);
2023

2124
/**
2225
* Supports the argument type of {@see UserInterface}.
2326
*
2427
* @author Iltar van der Berg <kjarli@gmail.com>
28+
*
29+
* @deprecated since version 4.1, to be removed in 5.0. Use {@link UserValueResolver} instead
2530
*/
2631
final class SecurityUserValueResolver implements ArgumentValueResolverInterface
2732
{

‎src/Symfony/Bundle/SecurityBundle/Tests/SecurityUserValueResolverTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/Tests/SecurityUserValueResolverTest.php
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
2222
use Symfony\Component\Security\Core\User\UserInterface;
2323

24+
/**
25+
* @group legacy
26+
*/
2427
class SecurityUserValueResolverTest extends TestCase
2528
{
2629
public function testResolveNoToken()

‎src/Symfony/Component/Security/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* The `ContextListener::setLogoutOnUserChange()` method is deprecated and will be removed in 5.0.
8+
* added `UserValueResolver`.
89

910
4.0.0
1011
-----
+57Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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\Controller;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
16+
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
17+
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
18+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
19+
use Symfony\Component\Security\Core\User\UserInterface;
20+
21+
/**
22+
* Supports the argument type of {@see UserInterface}.
23+
*
24+
* @author Iltar van der Berg <kjarli@gmail.com>
25+
*/
26+
final class UserValueResolver implements ArgumentValueResolverInterface
27+
{
28+
private $tokenStorage;
29+
30+
public function __construct(TokenStorageInterface $tokenStorage)
31+
{
32+
$this->tokenStorage = $tokenStorage;
33+
}
34+
35+
public function supports(Request $request, ArgumentMetadata $argument)
36+
{
37+
// only security user implementations are supported
38+
if (UserInterface::class !== $argument->getType()) {
39+
return false;
40+
}
41+
42+
$token = $this->tokenStorage->getToken();
43+
if (!$token instanceof TokenInterface) {
44+
return false;
45+
}
46+
47+
$user = $token->getUser();
48+
49+
// in case it's not an object we cannot do anything with it; E.g. "anon."
50+
return $user instanceof UserInterface;
51+
}
52+
53+
public function resolve(Request $request, ArgumentMetadata $argument)
54+
{
55+
yield $this->tokenStorage->getToken()->getUser();
56+
}
57+
}
+101Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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\Tests\Controller;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
17+
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\DefaultValueResolver;
18+
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
19+
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
20+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
21+
use Symfony\Component\Security\Core\User\UserInterface;
22+
use Symfony\Component\Security\Http\Controller\UserValueResolver;
23+
24+
class UserValueResolverTest extends TestCase
25+
{
26+
public function testResolveNoToken()
27+
{
28+
$tokenStorage = new TokenStorage();
29+
$resolver = new UserValueResolver($tokenStorage);
30+
$metadata = new ArgumentMetadata('foo', UserInterface::class, false, false, null);
31+
32+
$this->assertFalse($resolver->supports(Request::create('/'), $metadata));
33+
}
34+
35+
public function testResolveNoUser()
36+
{
37+
$mock = $this->getMockBuilder(UserInterface::class)->getMock();
38+
$token = $this->getMockBuilder(TokenInterface::class)->getMock();
39+
$tokenStorage = new TokenStorage();
40+
$tokenStorage->setToken($token);
41+
42+
$resolver = new UserValueResolver($tokenStorage);
43+
$metadata = new ArgumentMetadata('foo', get_class($mock), false, false, null);
44+
45+
$this->assertFalse($resolver->supports(Request::create('/'), $metadata));
46+
}
47+
48+
public function testResolveWrongType()
49+
{
50+
$tokenStorage = new TokenStorage();
51+
$resolver = new UserValueResolver($tokenStorage);
52+
$metadata = new ArgumentMetadata('foo', null, false, false, null);
53+
54+
$this->assertFalse($resolver->supports(Request::create('/'), $metadata));
55+
}
56+
57+
public function testResolve()
58+
{
59+
$user = $this->getMockBuilder(UserInterface::class)->getMock();
60+
$token = $this->getMockBuilder(TokenInterface::class)->getMock();
61+
$token->expects($this->any())->method('getUser')->willReturn($user);
62+
$tokenStorage = new TokenStorage();
63+
$tokenStorage->setToken($token);
64+
65+
$resolver = new UserValueResolver($tokenStorage);
66+
$metadata = new ArgumentMetadata('foo', UserInterface::class, false, false, null);
67+
68+
$this->assertTrue($resolver->supports(Request::create('/'), $metadata));
69+
$this->assertSame(array($user), iterator_to_array($resolver->resolve(Request::create('/'), $metadata)));
70+
}
71+
72+
public function testIntegration()
73+
{
74+
$user = $this->getMockBuilder(UserInterface::class)->getMock();
75+
$token = $this->getMockBuilder(TokenInterface::class)->getMock();
76+
$token->expects($this->any())->method('getUser')->willReturn($user);
77+
$tokenStorage = new TokenStorage();
78+
$tokenStorage->setToken($token);
79+
80+
$argumentResolver = new ArgumentResolver(null, array(new UserValueResolver($tokenStorage)));
81+
$this->assertSame(array($user), $argumentResolver->getArguments(Request::create('/'), function (UserInterface $user) {}));
82+
}
83+
84+
public function testIntegrationNoUser()
85+
{
86+
$token = $this->getMockBuilder(TokenInterface::class)->getMock();
87+
$tokenStorage = new TokenStorage();
88+
$tokenStorage->setToken($token);
89+
90+
$argumentResolver = new ArgumentResolver(null, array(new UserValueResolver($tokenStorage), new DefaultValueResolver()));
91+
$this->assertSame(array(null), $argumentResolver->getArguments(Request::create('/'), function (UserInterface $user = null) {}));
92+
}
93+
}
94+
95+
abstract class DummyUser implements UserInterface
96+
{
97+
}
98+
99+
abstract class DummySubUser extends DummyUser
100+
{
101+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.