Skip to content

[Security] Documented new logout event #13520

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
Apr 17, 2020
Merged
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions reference/configuration/security.rst
Original file line number Diff line number Diff line change
Expand Up @@ -538,9 +538,18 @@ The ``invalidate_session`` option allows to redefine this behavior. Set this
option to ``false`` in every firewall and the user will only be logged out from
the current firewall and not the other ones.

.. _reference-security-logout-success-handler:

success_handler
~~~~~~~~~~~~~~~

.. deprecated:: 5.1

This option is deprecated since Symfony 5.1. Register an
:doc:`event listener </event_dispatcher>` on the
:class:`Symfony\\Component\\Security\\Http\\Event\\LogoutEvent`
instead.

**type**: ``string`` **default**: ``'security.logout.success_handler'``

The service ID used for handling a successful logout. The service must implement
Expand Down
88 changes: 85 additions & 3 deletions security.rst
Original file line number Diff line number Diff line change
Expand Up @@ -899,11 +899,93 @@ Next, you'll need to create a route for this URL (but not a controller):
And that's it! By sending a user to the ``app_logout`` route (i.e. to ``/logout``)
Symfony will un-authenticate the current user and redirect them.

Customizing Logout
~~~~~~~~~~~~~~~~~~

.. versionadded:: 5.1

The ``LogoutEvent`` was introduced in Symfony 5.1. Prior to this
version, you had to use a
:ref:`logout success handler <reference-security-logout-success-handler>`
to customize the logout.

In some cases you need to execute extra logic upon logout (e.g. invalidate
some tokens) or want to customize what happens after a logout. During
logout, a :class:`Symfony\\Component\\Security\\Http\\Event\\LogoutEvent`
is dispatched. Register an :doc:`event listener or subscriber </event_dispatcher>`
to execute custom logic. The following information is available in the
event class:

``getToken()``
Returns the security token of the session that is about to be logged
out.
``getRequest()``
Returns the current request.
``getResponse()``
Returns a response, if it is already set by a custom listener. Use
``setResponse()`` to configure a custom logout response.


.. tip::

Need more control of what happens after logout? Add a ``success_handler`` key
under ``logout`` and point it to a service id of a class that implements
:class:`Symfony\\Component\\Security\\Http\\Logout\\LogoutSuccessHandlerInterface`.
Every Security firewall has its own event dispatcher
(``security.event_dispatcher.FIREWALLNAME``). The logout event is
dispatched on both the global and firewall dispatcher. You can register
on the firewall dispatcher if you want your listener to only be
executed for a specific firewall. For instance, if you have an ``api``
and ``main`` firewall, use this configuration to register only on the
logout event in the ``main`` firewall:

.. configuration-block::

.. code-block:: yaml

# config/services.yaml
services:
# ...

App\EventListener\CustomLogoutSubscriber:
tags:
- name: kernel.event_subscriber
dispacher: security.event_dispatcher.main

.. code-block:: xml

<!-- config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<!-- ... -->

<service id="App\EventListener\CustomLogoutSubscriber">
<tag name="kernel.event_subscriber"
dispacher="security.event_dispatcher.main"
/>
</service>
</services>
</container>

.. code-block:: php

// config/services.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use App\EventListener\CutomLogoutListener;
use App\EventListener\CutomLogoutSubscriber;
use Symfony\Component\Security\Http\Event\LogoutEvent;

return function(ContainerConfigurator $configurator) {
$services = $configurator->services();

$services->set(CustomLogoutSubscriber::class)
->tag('kernel.event_subscriber', [
'dispatcher' => 'security.event_dispatcher.main',
]);
};

.. _security-role-hierarchy:

Expand Down