Skip to content

Clarify comment about "Retrieving the User Object" #8542

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 5 commits into from
Closed
Changes from 4 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
5 changes: 3 additions & 2 deletions security.rst
Original file line number Diff line number Diff line change
Expand Up @@ -999,13 +999,14 @@ look like::

use Symfony\Component\Security\Core\User\UserInterface;

public function indexAction(UserInterface $user)
public function indexAction()
{
if (!$this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
throw $this->createAccessDeniedException();
}
Copy link
Member

Choose a reason for hiding this comment

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

Bah, now that I look a bit more, this is a bit of a mess :/ still. I'd like to propose a few more changes:

  1. In the above code, use the simpler $this->denyAccessUnlessGranted().

  2. Below, user the simpler $user = $this->getUser().

  3. Move the suggestion about UserInterface to below, and I'll suggest a wording tweak too :)

$user = $this->getUser();
// or you can also type-hint a method argument with UserInterface: e.g. "UserInterface $user"
  1. Below (you need to expand the code-blocks to see it), there is a versionadded:: 3.2. I think we should shorten that quite a bit:
 .. versionadded:: 3.2
     The ability to get the user by type-hinting an argument with UserInterface
     was introduced in Symfony 3.2.


// the above is a shortcut for this
// This is the recommended way but type-hinting the
// UserInterface as a method argument also works
$user = $this->get('security.token_storage')->getToken()->getUser();
}

Expand Down