[11.x] use Auth::userResolver when resolving the authenticated user #54382
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Laravel 11 introduced Contextual Attributes, which allows a developer to resolve dependencies from the container using PHP's Attributes to annotate parameters.
The
Illuminate\Container\Attributes\Authenticated
Attribute resolves the current authenticated user and injects it to the parameter annotated with it.The Authentication component allows a developer to provide a custom user resolver closure with a custom logic on how to resolve the current authenticated user.
This resolver is then bound to the container, to the Gate component and to the Request object.
But the
Illuminate\Container\Attributes\Authenticated
Attribute did not use this resolver.In projects which provide a custom resolver, this could result in the wrong user instance -- or no instance at all (aka
null
) -- being resolved, instead of using the custom logic as intended by the developer.This PR:
Illuminate\Container\Attributes\Authenticated
Attribute to use the Authentication component user resolverAuthManager
mock objectNote
I used the same logic as the resolver used in the request to allow the Attribute's guard parameter to be used:
framework/src/Illuminate/Auth/AuthServiceProvider.php
Lines 85 to 92 in 2d10f2a