Skip to content

[Security/Http] Make UserValueResolver accept any subtype of UserInterface #30401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public function __construct(TokenStorageInterface $tokenStorage)
public function supports(Request $request, ArgumentMetadata $argument)
{
// only security user implementations are supported
if (UserInterface::class !== $argument->getType()) {
$type = $argument->getType();
if (UserInterface::class !== $type && !is_subclass_of($type, UserInterface::class)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deprecated classes don't get new features, should be reverted

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public function __construct(TokenStorageInterface $tokenStorage)
public function supports(Request $request, ArgumentMetadata $argument)
{
// only security user implementations are supported
if (UserInterface::class !== $argument->getType()) {
$type = $argument->getType();
if (UserInterface::class !== $type && !is_subclass_of($type, UserInterface::class)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if l48 should be changed to return $user instanceof $type to prevent a type error?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please test this with a parameter converter to see if this will work as expected when the UserInterface is an entity and parameter converters are enabled.

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\DefaultValueResolver;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Http\Controller\UserValueResolver;
Expand Down Expand Up @@ -69,6 +68,20 @@ public function testResolve()
$this->assertSame([$user], iterator_to_array($resolver->resolve(Request::create('/'), $metadata)));
}

public function testResolveWithSubclass()
{
$user = $this->getMockForAbstractClass(DummySubUser::class);
$token = new UsernamePasswordToken($user, 'password', 'provider');
$tokenStorage = new TokenStorage();
$tokenStorage->setToken($token);

$resolver = new UserValueResolver($tokenStorage);
$metadata = new ArgumentMetadata('foo', DummySubUser::class, false, false, null);

$this->assertTrue($resolver->supports(Request::create('/'), $metadata));
$this->assertSame([$user], iterator_to_array($resolver->resolve(Request::create('/'), $metadata)));
}

public function testIntegration()
{
$user = $this->getMockBuilder(UserInterface::class)->getMock();
Expand Down