Skip to content

Fixed use of equals (closes #2109) #2111

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 11, 2013
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
23 changes: 17 additions & 6 deletions cookbook/security/custom_provider.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,26 @@ create a ``User`` class that represents that data. The ``User`` can look
however you want and contain any data. The only requirement is that the
class implements :class:`Symfony\\Component\\Security\\Core\\User\\UserInterface`.
The methods in this interface should therefore be defined in the custom user
class: ``getRoles()``, ``getPassword()``, ``getSalt()``, ``getUsername()``,
``eraseCredentials()``, ``equals()``.
class: :method:`Symfony\\Component\\Security\\Core\\User\\UserInterface::getRoles`,
:method`Symfony\\Component\\Security\\Core\\User\\UserInterfacegetPassword`,
:method:`Symfony\\Component\\Security\\Core\\User\\UserInterface::getSalt`,
:method:`Symfony\\Component\\Security\\Core\\User\\UserInterface::getUsername`,
:method:`Symfony\\Component\\Security\\Core\\User\\UserInterface::eraseCredentials`.
It is also usefull to implement the
:class:`Symfony\\Component\\Security\\Core\\User\\EquatableInterface` interface,
to define a method how check if the user is equal to the current user. This
interface requires a :method:`Symfony\\Component\\Security\\Core\\User\\EquatableInterface::isEqualTo`
method.

Let's see this in action::

// src/Acme/WebserviceUserBundle/Security/User/WebserviceUser.php
namespace Acme\WebserviceUserBundle\Security\User;

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

class WebserviceUser implements UserInterface
class WebserviceUser implements UserInterface, EquatableInterface
{
private $username;
private $password;
Expand Down Expand Up @@ -71,7 +80,7 @@ Let's see this in action::
{
}

public function equals(UserInterface $user)
public function isEqualTo(UserInterface $user)
{
if (!$user instanceof WebserviceUser) {
return false;
Expand All @@ -93,11 +102,13 @@ Let's see this in action::
}
}

.. versionadded:: 2.1
The ``EquatableInterface`` was added in Symfony 2.1, use the ``equals()``
method of the ``UserInterface`` in Symfony 2.0

If you have more information about your users - like a "first name" - then
you can add a ``firstName`` field to hold that data.

For more details on each of the methods, see :class:`Symfony\\Component\\Security\\Core\\User\\UserInterface`.

Create a User Provider
----------------------

Expand Down