Skip to content

UserValueResolver and SecurityUserValueResolver improvement #26971

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 3 commits 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
12 changes: 11 additions & 1 deletion src/Symfony/Bundle/SecurityBundle/SecurityUserValueResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(TokenStorageInterface $tokenStorage)
public function supports(Request $request, ArgumentMetadata $argument)
{
// only security user implementations are supported
if (UserInterface::class !== $argument->getType()) {
if (!$argument->getType() || !$this->implementsCorrectInterface($argument->getType())) {
Copy link
Member

Choose a reason for hiding this comment

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

Should be in Symfony\Component\Security\Http\Controller\UserValueResolver instead (see deprecation notice above)

Copy link
Author

Choose a reason for hiding this comment

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

It's in both classes :)

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 are frozen until they are suppressed, it should not get new features.

return false;
}

Expand All @@ -59,4 +59,14 @@ public function resolve(Request $request, ArgumentMetadata $argument)
{
yield $this->tokenStorage->getToken()->getUser();
}

/**
* @param string $type
*
* @return bool
*/
private function implementsCorrectInterface($type)
{
return UserInterface::class === $type || array_key_exists(UserInterface::class, class_implements($type));
Copy link
Member

Choose a reason for hiding this comment

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

Let's inline this logic in the calling condition

Copy link
Member

Choose a reason for hiding this comment

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

isn't this what is_a() or is_subclass_of() does?

Copy link
Author

Choose a reason for hiding this comment

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

Didn't know 😳thanks!

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ public function testResolve()
$this->assertSame(array($user), iterator_to_array($resolver->resolve(Request::create('/'), $metadata)));
}

public function testResolveUserInterfaceImplementation()
{
$user = $this->getMockBuilder(UserInterface::class)->getMock();
$token = $this->getMockBuilder(TokenInterface::class)->getMock();
$token->expects($this->any())->method('getUser')->willReturn($user);
$tokenStorage = new TokenStorage();
$tokenStorage->setToken($token);

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

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

public function testIntegration()
{
$user = $this->getMockBuilder(UserInterface::class)->getMock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(TokenStorageInterface $tokenStorage)
public function supports(Request $request, ArgumentMetadata $argument)
{
// only security user implementations are supported
if (UserInterface::class !== $argument->getType()) {
if (!$argument->getType() || !$this->implementsCorrectInterface($argument->getType())) {
return false;
}

Expand All @@ -54,4 +54,14 @@ public function resolve(Request $request, ArgumentMetadata $argument)
{
yield $this->tokenStorage->getToken()->getUser();
}

/**
* @param string $type
*
* @return bool
*/
private function implementsCorrectInterface($type)
{
return UserInterface::class === $type || array_key_exists(UserInterface::class, class_implements($type));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@ public function testResolve()
$this->assertSame(array($user), iterator_to_array($resolver->resolve(Request::create('/'), $metadata)));
}

public function testResolveUserInterfaceImplementation()
{
$user = $this->getMockBuilder(UserInterface::class)->getMock();
$token = $this->getMockBuilder(TokenInterface::class)->getMock();
$token->expects($this->any())->method('getUser')->willReturn($user);
$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(array($user), iterator_to_array($resolver->resolve(Request::create('/'), $metadata)));
}

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