2010-10-28 14:04:25 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Symfony\Bundle\DoctrineMongoDBBundle\Security;
|
|
|
|
|
2010-12-16 21:27:16 +00:00
|
|
|
use Symfony\Component\Security\User\AccountInterface;
|
2010-10-28 14:04:25 +01:00
|
|
|
use Symfony\Component\Security\User\UserProviderInterface;
|
2010-12-16 21:27:16 +00:00
|
|
|
use Symfony\Component\Security\Exception\UnsupportedAccountException;
|
2010-10-28 14:04:25 +01:00
|
|
|
use Symfony\Component\Security\Exception\UsernameNotFoundException;
|
|
|
|
|
|
|
|
class DocumentUserProvider implements UserProviderInterface
|
|
|
|
{
|
2010-12-16 21:27:16 +00:00
|
|
|
protected $class;
|
2010-10-28 14:04:25 +01:00
|
|
|
protected $repository;
|
|
|
|
protected $property;
|
|
|
|
|
2010-12-16 21:27:16 +00:00
|
|
|
public function __construct($em, $class, $property = null)
|
2010-10-28 14:04:25 +01:00
|
|
|
{
|
2010-12-16 21:27:16 +00:00
|
|
|
$this->class = $class;
|
2010-10-28 14:04:25 +01:00
|
|
|
$this->repository = $em->getRepository($class);
|
|
|
|
$this->property = $property;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function loadUserByUsername($username)
|
|
|
|
{
|
|
|
|
if (null !== $this->property) {
|
|
|
|
$user = $this->repository->findOneBy(array($this->property => $username));
|
|
|
|
} else {
|
|
|
|
if (!$this->repository instanceof UserProviderInterface) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('The Doctrine repository "%s" must implement UserProviderInterface.', get_class($this->repository)));
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = $this->repository->loadUserByUsername($username);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null === $user) {
|
|
|
|
throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
|
|
|
|
}
|
|
|
|
|
2010-12-16 21:27:16 +00:00
|
|
|
return $user;
|
2010-12-08 12:51:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2010-12-16 21:27:16 +00:00
|
|
|
public function reloadUserByAccount(AccountInterface $account)
|
2010-12-08 12:51:26 +00:00
|
|
|
{
|
2010-12-16 21:27:16 +00:00
|
|
|
if (!$account instanceof $this->class) {
|
|
|
|
throw new UnsupportedAccountException(sprintf('Instances of "%s" are not supported.', get_class($account)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->loadUserByUsername((string) $account);
|
2010-10-28 14:04:25 +01:00
|
|
|
}
|
|
|
|
}
|