diff --git a/security.rst b/security.rst index e46c46abb3a..247bef0a4a6 100644 --- a/security.rst +++ b/security.rst @@ -994,17 +994,14 @@ shown above. ----------------------------- After authentication, the ``User`` object of the current user can be accessed -via the ``security.token_storage`` service. From inside a controller, this will -look like:: - - use Symfony\Component\Security\Core\User\UserInterface; +via the ``getUser()`` shortcut (which uses the ``security.token_storage`` +service). From inside a controller, this will look like:: public function indexAction() { $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); $user = $this->getUser(); - // or you can also type-hint a method argument with UserInterface: e.g. "UserInterface $user" } .. tip:: @@ -1012,10 +1009,6 @@ look like:: The user will be an object and the class of that object will depend on your :ref:`user provider `. -.. versionadded:: 3.2 - The ability to get the user by type-hinting an argument with UserInterface - was introduced in Symfony 3.2. - Now you can call whatever methods are on *your* User object. For example, if your User object has a ``getFirstName()`` method, you could use that:: @@ -1036,14 +1029,7 @@ It's important to check if the user is authenticated first. If they're not, ``$user`` will either be ``null`` or the string ``anon.``. Wait, what? Yes, this is a quirk. If you're not logged in, the user is technically the string ``anon.``, though the ``getUser()`` controller shortcut converts this to -``null`` for convenience. When type-hinting the -:class:`Symfony\\Component\\Security\\Core\\User\\UserInterface\\UserInterface` -and being logged-in is optional, you can allow a null value for the argument:: - - public function indexAction(UserInterface $user = null) - { - // $user is null when not logged-in or anon. - } +``null`` for convenience. The point is this: always check to see if the user is logged in before using the User object, and use the ``isGranted()`` method (or @@ -1059,6 +1045,25 @@ the User object, and use the ``isGranted()`` method (or } +.. note:: + + An alternative way to get the current user in a controller is to type-hint + the controller argument with + :class:`Symfony\\Component\\Security\\Core\\User\\UserInterface\\UserInterface` + (and default it to ``null`` if being logged-in is optional):: + + use Symfony\Component\Security\Core\User\UserInterface\UserInterface; + + public function indexAction(UserInterface $user = null) + { + // $user is null when not logged-in or anon. + } + + This is only recommended for experienced developers who don't extend from the + :ref:`Symfony base controller ` and + don't use the :class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\ControllerTrait` + either. Otherwise, it's recommended to keep using the ``getUser()`` shortcut. + Retrieving the User in a Template ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~