Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
ker0x wants to merge1 commit intosymfony:6.2
base:6.2
Choose a base branch
Loading
fromker0x:feature/currentuser-attribute
Open
Show file tree
Hide file tree
Changes fromall 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 deletionscontroller/value_resolver.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletiondoctrine/events.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssecurity.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp