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

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

wants to merge 1 commit into from

Conversation

Pierstoval
Copy link
Contributor

@Pierstoval Pierstoval commented Feb 27, 2019

Q A
Branch? master/4.3
Bug fix? no
New feature? yes
BC breaks? no (not sure)
Deprecations? no
Tests pass? yes
License MIT

With a controller we can inject the UserInterface object like this:

public function index(Request $request, UserInterface $user = null): Response
{
    // ...
}

However, this has a drawback: it hides the domain user for actions that need the right type-hint, else, it means we're "blind", which means that we must add if ($user instanceof MyUserObject) { /* ... */} to execute our logic.

The goal of this PR is to allow this:

use App\Entity\User;
// ...
public function index(Request $request, User $user = null): Response
{
    // ...
}

This is just a basic suggestion that is quite straightforward (so if it's not suitable, feel free to tell me, if you know the component better than me 👍 )!

If it's not possible, I also thought about creating a SecurityTokenValueResolver, also probably straightforward 👍 (will probably work on a separate PR for this)

@chalasr
Copy link
Member

chalasr commented Feb 28, 2019

Already thought about this, but was unsure. ping @linaori

@chalasr chalasr added this to the next milestone Feb 28, 2019
Copy link
Member

@chalasr chalasr left a comment

Choose a reason for hiding this comment

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

Could you please add a note in the Security CHANGELOG?

@@ -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

@@ -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?

@@ -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
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.

@Koc
Copy link
Contributor

Koc commented Mar 3, 2019

This was proposed multiple times ago (#18510 , #26971) and was rejected due to reasons described in this comment #18510 (comment)

@chalasr
Copy link
Member

chalasr commented Apr 18, 2019

Now that we have #30914 I think we can close here.

@Pierstoval Pierstoval closed this Apr 18, 2019
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.3 Apr 30, 2019
@Pierstoval Pierstoval deleted the user-value-resolver-subtypes branch June 10, 2020 08:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants