Skip to content

Commit

Permalink
minor #5993 [Cookbook] [Security] Use UserLoaderInterface instead of …
Browse files Browse the repository at this point in the history
…UserProviderInterface (ogizanagi)

This PR was merged into the 2.8 branch.

Discussion
----------

[Cookbook] [Security] Use UserLoaderInterface instead of UserProviderInterface

| Q             | A
| ------------- | ---
| Doc fix?      | yes
| New docs?     | no
| Applies to    | 2.8+
| Fixed tickets | #5968

Commits
-------

79ec09a [Cookbook] [Security] Use UserLoaderInterface instead of UserProviderInterface
  • Loading branch information
xabbuh committed Dec 23, 2015
2 parents afcbc51 + 79ec09a commit 02db120
Showing 1 changed file with 8 additions and 28 deletions.
36 changes: 8 additions & 28 deletions cookbook/security/entity_provider.rst
Expand Up @@ -424,20 +424,18 @@ both are unique in the database. Unfortunately, the native entity provider
is only able to handle querying via a single property on the user.

To do this, make your ``UserRepository`` implement a special
:class:`Symfony\\Component\\Security\\Core\\User\\UserProviderInterface`. This
interface requires three methods: ``loadUserByUsername($username)``,
``refreshUser(UserInterface $user)``, and ``supportsClass($class)``::
:class:`Symfony\\Bridge\\Doctrine\\Security\\User\\UserLoaderInterface`. This
interface only requires one method: ``loadUserByUsername($username)``::

// src/AppBundle/Entity/UserRepository.php
namespace AppBundle\Entity;

use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Doctrine\ORM\EntityRepository;

class UserRepository extends EntityRepository implements UserProviderInterface
class UserRepository extends EntityRepository implements UserLoaderInterface
{
public function loadUserByUsername($username)
{
Expand All @@ -458,30 +456,12 @@ interface requires three methods: ``loadUserByUsername($username)``,

return $user;
}

public function refreshUser(UserInterface $user)
{
$class = get_class($user);
if (!$this->supportsClass($class)) {
throw new UnsupportedUserException(
sprintf(
'Instances of "%s" are not supported.',
$class
)
);
}

return $this->find($user->getId());
}

public function supportsClass($class)
{
return $this->getEntityName() === $class
|| is_subclass_of($class, $this->getEntityName());
}
}

For more details on these methods, see :class:`Symfony\\Component\\Security\\Core\\User\\UserProviderInterface`.
.. versionadded:: 2.8
The :class:`Symfony\\Bridge\\Doctrine\\Security\\User\\UserLoaderInterface`
interface was introduced in 2.8. Prior to Symfony 2.8, you had to implement
``Symfony\Component\Security\Core\User\UserProviderInterface``.

.. tip::

Expand Down

0 comments on commit 02db120

Please sign in to comment.