Skip to content

Commit f285c5a

Browse files
committed
Clarifying some details on serialize/unserialize and making it consistent with changes we recently made
1 parent 95e9c76 commit f285c5a

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

cookbook/security/entity_provider.rst

+18-5
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ focus on the most important methods that come from the
171171
172172
.. note::
173173

174-
When implementing the
174+
If you choose to implement
175175
:class:`Symfony\\Component\\Security\\Core\\User\\EquatableInterface`,
176176
you determine yourself which properties need to be compared to distinguish
177177
your user objects.
@@ -198,14 +198,27 @@ interface forces the class to implement the five following methods:
198198

199199
For more details on each of these, see :class:`Symfony\\Component\\Security\\Core\\User\\UserInterface`.
200200

201-
.. note::
201+
.. sidebar:: What is the importance of serialize and unserialize?
202202

203203
The :phpclass:`Serializable` interface and its ``serialize`` and ``unserialize``
204204
methods have been added to allow the ``User`` class to be serialized
205205
to the session. This may or may not be needed depending on your setup,
206-
but it's probably a good idea. Only the ``id`` needs to be serialized,
207-
because the :method:`Symfony\\Bridge\\Doctrine\\Security\\User\\EntityUserProvider::refreshUser`
208-
method reloads the user on each request by using the ``id``.
206+
but it's probably a good idea. The ``id`` is the most important value
207+
that needs to be serialized because the
208+
:method:`Symfony\\Bridge\\Doctrine\\Security\\User\\EntityUserProvider::refreshUser`
209+
method reloads the user on each request by using the ``id``. In practice,
210+
this means that the User object is reloaded from the database on each
211+
request using the ``id`` from the serialized object. This makes sure
212+
all of the User's data is fresh.
213+
214+
Symfony also uses the ``username``, ``salt``, and ``password`` to verify
215+
that the User has not changed between requests. Failing to serialize
216+
these may cause you to be logged out on each request. If your User implements
217+
:class:`Symfony\\Component\\Security\\Core\\User\\EquatableInterface`,
218+
then instead of these properties being checked, your ``isEqualTo`` method
219+
is simply called, and you can check whatever properties you want. Unless
220+
you understand this, you probably *won't* need to implement this interface
221+
or worry about it.
209222

210223
Below is an export of the ``User`` table from MySQL with user ``admin`` and
211224
password ``admin`` (which has been encoded). For details on how to create

0 commit comments

Comments
 (0)