Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.2k
[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
base:6.2
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1881,29 +1881,65 @@ Fetching the User Object | ||||||
------------------------ | ||||||
After authentication, the ``User`` object of the current user can be | ||||||
accessed via the:ref:`#[CurrentUser] <controller-value-resolver-current-user>` attribute or``getUser()`` shortcut in the | ||||||
:ref:`base controller <the-base-controller-class-services>`: | ||||||
.. configuration-block:: | ||||||
.. 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 | ||||||
#[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()); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 and perhaps add other example with current user that may be null? | ||||||
} | ||||||
} | ||||||
.. code-block:: php | ||||||
// src/Controller/ProfileController.php | ||||||
namespace App\Controller; | ||||||
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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggested change
| ||||||
Fetching the User from a Service | ||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||