From 01ca0ad35dfda90cdcc72fbbaebf8fe852a88ab2 Mon Sep 17 00:00:00 2001 From: William DURAND Date: Fri, 20 Apr 2012 15:23:01 +0200 Subject: [PATCH] =?UTF-8?q?[Propel1]=C2=A0Added=20security=20layer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Security/UserProvider/PropelFactory.php | 58 ++++++++++ .../Security/User/PropelUserProvider.php | 100 ++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 src/Symfony/Bridge/Propel1/DependencyInjection/Security/UserProvider/PropelFactory.php create mode 100644 src/Symfony/Bridge/Propel1/Security/User/PropelUserProvider.php diff --git a/src/Symfony/Bridge/Propel1/DependencyInjection/Security/UserProvider/PropelFactory.php b/src/Symfony/Bridge/Propel1/DependencyInjection/Security/UserProvider/PropelFactory.php new file mode 100644 index 0000000000..75ce92f801 --- /dev/null +++ b/src/Symfony/Bridge/Propel1/DependencyInjection/Security/UserProvider/PropelFactory.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Propel1\DependencyInjection\Security\UserProvider; + +use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\UserProvider\UserProviderFactoryInterface; +use Symfony\Component\Config\Definition\Builder\NodeDefinition; +use Symfony\Component\DependencyInjection\DefinitionDecorator; +use Symfony\Component\DependencyInjection\ContainerBuilder; + +/** + * PropelFactory creates services for Doctrine user provider. + * + * @author William Durand + */ +class PropelFactory implements UserProviderFactoryInterface +{ + private $key; + private $providerId; + + public function __construct($key, $providerId) + { + $this->key = $key; + $this->providerId = $providerId; + } + + public function create(ContainerBuilder $container, $id, $config) + { + $container + ->setDefinition($id, new DefinitionDecorator($this->providerId)) + ->addArgument($config['class']) + ->addArgument($config['property']) + ; + } + + public function getKey() + { + return $this->key; + } + + public function addConfiguration(NodeDefinition $node) + { + $node + ->children() + ->scalarNode('class')->isRequired()->cannotBeEmpty()->end() + ->scalarNode('property')->defaultNull()->end() + ->end() + ; + } +} diff --git a/src/Symfony/Bridge/Propel1/Security/User/PropelUserProvider.php b/src/Symfony/Bridge/Propel1/Security/User/PropelUserProvider.php new file mode 100644 index 0000000000..4392bb4eb5 --- /dev/null +++ b/src/Symfony/Bridge/Propel1/Security/User/PropelUserProvider.php @@ -0,0 +1,100 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Propel1\Security\User; + +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; + +/** + * Provides easy to use provisioning for Propel model users. + * + * @author William DURAND + */ +class PropelUserProvider implements UserProviderInterface +{ + /** + * A Model class name. + * @var string + */ + protected $class; + + /** + * A Query class name. + * @var string + */ + protected $queryClass; + + /** + * A property to use to retrieve the user. + * @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 !== $this->property) { + $filter = 'filterBy' . ucfirst($this->property); + $query->$filter($username); + } else { + $query->filterByUsername($username); + } + + if (null === $user = $query->findOne()) { + throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username)); + } + + return $user; + } + + /** + * {@inheritdoc} + */ + public function refreshUser(UserInterface $user) + { + if (!$user instanceof $this->class) { + throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user))); + } + + $queryClass = $this->queryClass; + + return $queryClass::create()->findPk($user->getPrimaryKey()); + } + + /** + * {@inheritdoc} + */ + public function supportsClass($class) + { + return $class === $this->class; + } +}