Skip to content

Don't mention the UserInterface type-hinting #9060

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

Closed
wants to merge 3 commits into from
Closed
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
39 changes: 22 additions & 17 deletions security.rst
Original file line number Diff line number Diff line change
Expand Up @@ -994,28 +994,21 @@ 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::

The user will be an object and the class of that object will depend on
your :ref:`user provider <security-user-providers>`.

.. versionadded:: 3.2
The ability to get the user by type-hinting an argument with UserInterface
was introduced in Symfony 3.2.
Copy link
Contributor

@linaori linaori Jan 15, 2018

Choose a reason for hiding this comment

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


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::

Expand All @@ -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
Expand All @@ -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 <the-base-controller-class-services>` 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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down