You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feature #45761 Throw access denied if CurrentUser cannot be resolved instead of a 500 (Seldaek)
This PR was squashed before being merged into the 6.1 branch.
Discussion
----------
Throw access denied if CurrentUser cannot be resolved instead of a 500
| Q | A
| ------------- | ---
| Branch? | 6.1
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Tickets | Fix#45257
| License | MIT
| Doc PR | symfony/symfony-docs#... <!-- required for new features -->
When using `#[CurrentUser] User $user` in my controller I do expect to get that, and if the user is not logged in clearly I am expecting a logged in user here so throwing an AccessDeniedException for me would be super convenient.
Right now it simply stops resolving that param, and we end up with a 500 for example:
> [2022-03-16T06:33:37.867185+00:00] request.CRITICAL: Uncaught PHP Exception Symfony\Component\DependencyInjection\Exception\RuntimeException: "Cannot autowire argument $loggedUser of "App\Controller\UserController::fooAction()": it references class "App\Entity\User" but no such service exists."
Yes it's a failure of my firewall config if you will, but on the other hand I don't see the point in having to list every URL that needs a user in the firewall, which is a very un-DRY and error prone process, if it can be done for me in this way.
I would personally consider this a bugfix and submit the PR against 5.4, but I thought I'd start the discussion with a PR for 6.1 :)
Commits
-------
fcafa58 Throw access denied if CurrentUser cannot be resolved instead of a 500
thrownewAccessDeniedException(sprintf('There is no logged-in user to pass to $%s, make the argument nullable if you want to allow anonymous access to the action.', $argument->getName()));
thrownewAccessDeniedException(sprintf('The logged-in user is an instance of "%s" and an user of type "%s" is expected.', $user::class, $argument->getType()));
$this->expectExceptionMessageMatches('/^The logged-in user is an instance of "Mock_UserInterface[^"]+" and an user of type "Symfony\\\\Component\\\\Security\\\\Core\\\\User\\\\InMemoryUser" is expected.$/');
$this->expectExceptionMessage('There is no logged-in user to pass to $foo, make the argument nullable if you want to allow anonymous access to the action.');
0 commit comments