Skip to content

[Cookbook][Security] document the new AuthenticationUtils #4723

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

Merged
merged 1 commit into from
Jan 16, 2015
Merged
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
29 changes: 11 additions & 18 deletions cookbook/security/form_login_setup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ First, enable form login under your firewall:
# app/config/security.yml
security:
# ...

firewalls:
default:
anonymous: ~
Expand Down Expand Up @@ -98,7 +98,7 @@ under your ``form_login`` configuration (``/login`` and ``/login_check``):
.. configuration-block::

.. code-block:: php-annotations

// src/AppBundle/Controller/SecurityController.php
// ...
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
Expand Down Expand Up @@ -165,28 +165,16 @@ form::

// src/AppBundle/Controller/SecurityController.php
// ...

// ADD THIS use STATEMENT above your class
use Symfony\Component\Security\Core\Security;

public function loginAction(Request $request)
{
$session = $request->getSession();
$authenticationUtils = $this->get('security.authentication_utils');

// get the login error if there is one
if ($request->attributes->has(Security::AUTHENTICATION_ERROR)) {
$error = $request->attributes->get(
Security::AUTHENTICATION_ERROR
);
} elseif (null !== $session && $session->has(Security::AUTHENTICATION_ERROR)) {
$error = $session->get(Security::AUTHENTICATION_ERROR);
$session->remove(Security::AUTHENTICATION_ERROR);
} else {
$error = '';
}
$error = $authenticationUtils->getLastAuthenticationError();

// last username entered by the user
$lastUsername = (null === $session) ? '' : $session->get(Security::LAST_USERNAME);
$lastUsername = $authenticationUtils->getLastUsername();

return $this->render(
'security/login.html.twig',
Expand All @@ -198,6 +186,11 @@ form::
);
}

.. versionadded:: 2.6
The ``security.authentication_utils`` service and the
:class:`Symfony\\Component\\Security\\Http\\Authentication\\AuthenticationUtils`
class were introduced in Symfony 2.6.

Don't let this controller confuse you. As you'll see in a moment, when the
Copy link
Member

Choose a reason for hiding this comment

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

maybe we should reword this, as the controller isn't very confusing anymore.

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, it may still be confusing that you don't do any login actions at all, but only handle the errors.

user submits the form, the security system automatically handles the form
submission for you. If the user had submitted an invalid username or password,
Expand Down Expand Up @@ -471,4 +464,4 @@ any firewall. This means you can't check for security or even access the
user object on these pages. See :doc:`/cookbook/controller/error_pages`
for more details.

.. _`FOSUserBundle`: https://github.com/FriendsOfSymfony/FOSUserBundle
.. _`FOSUserBundle`: https://github.com/FriendsOfSymfony/FOSUserBundle