From e35ae765118214dcab0175c8f211c2074cdc44f0 Mon Sep 17 00:00:00 2001 From: William DURAND Date: Wed, 31 Aug 2011 09:36:25 +0200 Subject: [PATCH] [Security] Added a ModelUserProvider. Fixes #35 --- Security/User/ModelUserProvider.php | 99 +++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 Security/User/ModelUserProvider.php diff --git a/Security/User/ModelUserProvider.php b/Security/User/ModelUserProvider.php new file mode 100644 index 0000000..e062ccc --- /dev/null +++ b/Security/User/ModelUserProvider.php @@ -0,0 +1,99 @@ + + */ +class ModelUserProvider implements UserProviderInterface +{ + /** + * A Model class name. + * @var string + */ + protected $class; + /** + * A Query class name. + * @var string + */ + protected $queryClass; + /** + * @var string + */ + protected $property; + + /** + * Default constructor + * + * @param $class The User model class. + * @param $property The property to use to retrieve a user. + */ + public function __construct($class, $property = null) + { + $this->class = $class; + $this->queryClass = $class.'Query'; + $this->property = $property; + } + + /** + * {@inheritdoc} + */ + public function loadUserByUsername($username) + { + $queryClass = $this->queryClass; + + $query = $queryClass::create() + + if (null !== $property) { + $filter = 'filterBy' . ucfirst($property); + $query->$filter($username); + } else { + $query->filterByUsername($username); + } + + $user = $query->findOne(); + + if (null === $user) { + throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username)); + } + + return $user; + } + + /** + * {@inheritdoc} + */ + public function refreshUser(UserInterface $user) + { + if ($user instanceof $this->class) { + throw new UnsupportedAccountException(sprintf('Instances of "%s" are not supported.', get_class($user))); + } + + return $this->loadUserByUsername($user->getUsername()); + } + + /** + * {@inheritdoc} + */ + public function supportsClass($class) + { + return $class === $this->class; + } +}