DocumentUserProvider.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace Symfony\Bundle\DoctrineMongoDBBundle\Security;
  3. use Symfony\Component\Security\User\UserProviderInterface;
  4. use Symfony\Component\Security\Exception\UsernameNotFoundException;
  5. class DocumentUserProvider implements UserProviderInterface
  6. {
  7. protected $repository;
  8. protected $property;
  9. public function __construct($em, $class, $property = null)
  10. {
  11. $this->repository = $em->getRepository($class);
  12. $this->property = $property;
  13. }
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public function loadUserByUsername($username)
  18. {
  19. if (null !== $this->property) {
  20. $user = $this->repository->findOneBy(array($this->property => $username));
  21. } else {
  22. if (!$this->repository instanceof UserProviderInterface) {
  23. throw new \InvalidArgumentException(sprintf('The Doctrine repository "%s" must implement UserProviderInterface.', get_class($this->repository)));
  24. }
  25. $user = $this->repository->loadUserByUsername($username);
  26. }
  27. if (null === $user) {
  28. throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
  29. }
  30. return $user;
  31. }
  32. }