Skip to content

[Security] Add example to fetch User with CurrentUser attribute #18005

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

Open
wants to merge 1 commit into
base: 6.2
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions controller/value_resolver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ Symfony ships with the following value resolvers in the

In addition, some components, bridges and official bundles provide other value resolvers:

.. _controller-value-resolver-current-user:

:class:`Symfony\\Component\\Security\\Http\\Controller\\UserValueResolver`
Injects the object that represents the current logged in user if type-hinted
with ``UserInterface``. You can also type-hint your own ``User`` class but you
Expand Down
2 changes: 1 addition & 1 deletion doctrine/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ listener in the Symfony application by creating a new service for it and

.. configuration-block::

.. code-block:: attribute
.. code-block:: php-attributes

// src/App/EventListener/SearchIndexer.php
namespace App\EventListener;
Expand Down
66 changes: 51 additions & 15 deletions security.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1881,29 +1881,65 @@ Fetching the User Object
------------------------

After authentication, the ``User`` object of the current user can be
accessed via the ``getUser()`` shortcut in the
:ref:`base controller <the-base-controller-class-services>`::
accessed via the :ref:`#[CurrentUser] <controller-value-resolver-current-user>` attribute or ``getUser()`` shortcut in the
:ref:`base controller <the-base-controller-class-services>`:

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
.. configuration-block::

class ProfileController extends AbstractController
{
public function index(): Response
.. code-block:: php-attributes

// src/Controller/ProfileController.php
namespace App\Controller;

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

class ProfileController extends AbstractController
{
// usually you'll want to make sure the user is authenticated first,
// see "Authorization" below
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
#[IsGranted('IS_AUTHENTICATED_FULLY')]
public function index(
// returns your User object, or null if the user is not authenticated
#[CurrentUser] ?User $user
): Response {
// Call whatever methods you've added to your User class
// For example, if you added a getFirstName() method, you can use that.
return new Response('Well hi there '.$user->getFirstName());
Copy link
Contributor

Choose a reason for hiding this comment

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

what if $user result in being null like if someone remove the isgranted above
better force it to not null

and perhaps add other example with current user that may be null?

}
}

// returns your User object, or null if the user is not authenticated
// use inline documentation to tell your editor your exact User class
/** @var \App\Entity\User $user */
$user = $this->getUser();
.. code-block:: php

// src/Controller/ProfileController.php
namespace App\Controller;

// Call whatever methods you've added to your User class
// For example, if you added a getFirstName() method, you can use that.
return new Response('Well hi there '.$user->getFirstName());
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class ProfileController extends AbstractController
{
public function index(): Response
{
// usually you'll want to make sure the user is authenticated first,
// see "Authorization" below
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');

// returns your User object, or null if the user is not authenticated
// use inline documentation to tell your editor your exact User class
/** @var \App\Entity\User $user */
$user = $this->getUser();

// Call whatever methods you've added to your User class
// For example, if you added a getFirstName() method, you can use that.
return new Response('Well hi there '.$user->getFirstName());
}
}
}

.. note::

The ``#[CurrentUser]`` attribute can only be used in controller arguments to
retrieve the authenticated user.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
retrieve the authenticated user.
retrieve the authenticated user if any.


Fetching the User from a Service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down