@@ -40,13 +40,21 @@ To make it shorter, the getter and setter methods for each have been removed to
40
40
focus on the most important methods that come from the
41
41
:class: `Symfony\\ Component\\ Security\\ Core\\ User\\ UserInterface `.
42
42
43
+ .. versionadded :: 2.1
44
+
45
+ In Symfony 2.1 the ``Symfony\\Component\\Security\\Core\\User\\EquatableInterface ``
46
+ was introduced, it contains single method ``isEqualTo(UserInterface $user) ``.
47
+ You can implement this interface if you need to override default implementation
48
+ of comparsion logic in authentication mechanism.
49
+
43
50
.. code-block :: php
44
51
45
52
// src/Acme/UserBundle/Entity/User.php
46
53
47
54
namespace Acme\UserBundle\Entity;
48
55
49
56
use Symfony\Component\Security\Core\User\UserInterface;
57
+ use Symfony\Component\Security\Core\User\EquatableInterface;
50
58
use Doctrine\ORM\Mapping as ORM;
51
59
52
60
/**
@@ -100,11 +108,6 @@ focus on the most important methods that come from the
100
108
return array('ROLE_USER');
101
109
}
102
110
103
- public function equals(UserInterface $user)
104
- {
105
- return $user->getUsername() === $this->username;
106
- }
107
-
108
111
public function eraseCredentials()
109
112
{
110
113
}
@@ -123,18 +126,30 @@ focus on the most important methods that come from the
123
126
{
124
127
return $this->password;
125
128
}
129
+
130
+ /**
131
+ * EquatableInterface
132
+ */
133
+ public function isEqualTo(UserInterface $user)
134
+ {
135
+ return $user->getUsername() === $this->username;
136
+ }
126
137
}
127
138
128
139
In order to use an instance of the ``AcmeUserBundle:User `` class in the Symfony
129
140
security layer, the entity class must implement the
130
141
:class: `Symfony\\ Component\\ Security\\ Core\\ User\\ UserInterface `. This
131
- interface forces the class to implement the six following methods: ``getRoles() ``,
132
- ``getPassword() ``, ``getSalt() ``, ``getUsername() ``, ``eraseCredentials() ``,
133
- ``equals() ``. For more details on each of these, see :class: `Symfony\\ Component\\ Security\\ Core\\ User\\ UserInterface `.
134
-
135
- To keep it simple, the ``equals() `` method just compares the ``username `` field
136
- but it's also possible to do more checks depending on the complexity of your
137
- data model. On the other hand, the ``eraseCredentials() `` method remains empty
142
+ interface forces the class to implement the five following methods: ``getRoles() ``,
143
+ ``getPassword() ``, ``getSalt() ``, ``getUsername() ``, ``eraseCredentials() ``.
144
+ For more details on each of these, see :class: `Symfony\\ Component\\ Security\\ Core\\ User\\ UserInterface `.
145
+
146
+ To keep it simple, the ``isEqualTo() `` method form ``EquatableInterface ``
147
+ just compares the ``username `` field but it's also possible to do more checks
148
+ depending on the complexity of your data model, also in most cases, implementing
149
+ ``EquatableInterface `` is not nessesery, because security component has good default
150
+ implementation, see the ``hasUserChanged() `` method of
151
+ :class: `Symfony\\ Component\\ Security\\ Core\\ Authentication\\ Token\\ AbstractToken `.
152
+ On the other hand, the ``eraseCredentials() `` method remains empty
138
153
as we don't care about it in this tutorial.
139
154
140
155
Below is an export of my ``User `` table from MySQL. For details on how to
0 commit comments