Skip to content
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

#[CurrentUser]: Cannot autowire argument $user of "App\Controller\FooController::method()": it references class "App\Entity\User" but no such service exists if user isn't logged in #45257

Closed
fluffycondor opened this issue Jan 31, 2022 · 0 comments · Fixed by #45761

Comments

@fluffycondor
Copy link

@fluffycondor fluffycondor commented Jan 31, 2022

Symfony version(s) affected

5.4, 6.0

Description

If I try to autowire \App\Entity\User to a non-nullable variable using #[CurrentUser] attribute in a controller method and I'm not logged in, Symfony crashes with a RuntimeException $user references class "App\Entity\User" but no such service exists.
It works like a charm if I logged in or the variable is nullable.

I'd expect it throws \Symfony\Component\Security\Core\Exception\AccessDeniedException instead, because if a programmer requests a non-null $user for a method then I think there's is an implication this method is accessible only if a user is logged in (as if there's a $this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED'); line in the beginning).

How to reproduce

<?php

declare(strict_types=1);

namespace App\Controller;

use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\CurrentUser;

class FooController extends AbstractController
{
    #[Route('/foo', name: 'foo')]
    public function foo(#[CurrentUser] User $user): Response
    {
        return new Response('', Response::HTTP_NO_CONTENT);
    }
}

Possible Solution

No response

Additional Context

No response

@fluffycondor fluffycondor changed the title #[SecurityUser]: Cannot autowire argument $user of "App\Controller\FooController::method()": it references class "App\Entity\User" but no such service exists. #[SecurityUser]: Cannot autowire argument $user of "App\Controller\FooController::method()": it references class "App\Entity\User" but no such service exists if user isn't logged in Jan 31, 2022
@fluffycondor fluffycondor changed the title #[SecurityUser]: Cannot autowire argument $user of "App\Controller\FooController::method()": it references class "App\Entity\User" but no such service exists if user isn't logged in #[CurrentUser]: Cannot autowire argument $user of "App\Controller\FooController::method()": it references class "App\Entity\User" but no such service exists if user isn't logged in Jan 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants