From b107a3fdf04ac0ccaa8db036ec01eba859b971c0 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Fri, 11 Nov 2011 22:42:14 +0100 Subject: [PATCH] [SecurityBundle] Refactored the configuration The configuration is now cleaner by avoiding using prototyped nodes as additional keys. This is a BC break for existing providers. - MemoryProvider: security: providers: my_provider: memory: # this level has been added users: # ... - ChainProvider: security: providers: my_provider: chain: # This level has been added providers: # ... --- .../Security/UserProvider/EntityFactory.php | 5 ---- .../DependencyInjection/MainConfiguration.php | 23 +++++++-------- .../Security/UserProvider/InMemoryFactory.php | 29 +++++++++---------- .../UserProviderFactoryInterface.php | 2 -- .../DependencyInjection/SecurityExtension.php | 4 +-- .../Fixtures/php/container1.php | 24 ++++++++++----- .../Fixtures/xml/container1.xml | 18 ++++++++---- .../Fixtures/yml/container1.yml | 20 ++++++++----- 8 files changed, 67 insertions(+), 58 deletions(-) diff --git a/src/Symfony/Bundle/DoctrineBundle/DependencyInjection/Security/UserProvider/EntityFactory.php b/src/Symfony/Bundle/DoctrineBundle/DependencyInjection/Security/UserProvider/EntityFactory.php index 40de4f68d8..ae378aded5 100644 --- a/src/Symfony/Bundle/DoctrineBundle/DependencyInjection/Security/UserProvider/EntityFactory.php +++ b/src/Symfony/Bundle/DoctrineBundle/DependencyInjection/Security/UserProvider/EntityFactory.php @@ -40,11 +40,6 @@ class EntityFactory implements UserProviderFactoryInterface return 'entity'; } - public function getFixableKey() - { - return null; - } - public function addConfiguration(NodeDefinition $node) { $node diff --git a/src/Symfony/Bundle/SecurityBundle/DependencyInjection/MainConfiguration.php b/src/Symfony/Bundle/SecurityBundle/DependencyInjection/MainConfiguration.php index a5f1dd9902..18db285d16 100644 --- a/src/Symfony/Bundle/SecurityBundle/DependencyInjection/MainConfiguration.php +++ b/src/Symfony/Bundle/SecurityBundle/DependencyInjection/MainConfiguration.php @@ -301,29 +301,26 @@ class MainConfiguration implements ConfigurationInterface ->prototype('array') ; - /** @var $providerNodeBuilder \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition */ $providerNodeBuilder ->children() ->scalarNode('id')->end() - ->end() - ->fixXmlConfig('provider') - ->children() - ->arrayNode('providers') - ->beforeNormalization() - ->ifString() - ->then(function($v) { return preg_split('/\s*,\s*/', $v); }) + ->arrayNode('chain') + ->fixXmlConfig('provider') + ->children() + ->arrayNode('providers') + ->beforeNormalization() + ->ifString() + ->then(function($v) { return preg_split('/\s*,\s*/', $v); }) + ->end() + ->prototype('scalar')->end() + ->end() ->end() - ->prototype('scalar')->end() ->end() ->end() ; - /** @var $factory \Symfony\Bundle\SecurityBundle\DependencyInjection\Security\UserProvider\UserProviderFactoryInterface */ foreach ($this->userProviderFactories as $factory) { $name = str_replace('-', '_', $factory->getKey()); - if (null !== $factory->getFixableKey()) { - $providerNodeBuilder->fixXmlConfig($factory->getFixableKey(), $name); - } $factoryNode = $providerNodeBuilder->children()->arrayNode($name)->canBeUnset(); $factory->addConfiguration($factoryNode); diff --git a/src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/UserProvider/InMemoryFactory.php b/src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/UserProvider/InMemoryFactory.php index fbcd3a9c2e..8db4fa9d2a 100644 --- a/src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/UserProvider/InMemoryFactory.php +++ b/src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/UserProvider/InMemoryFactory.php @@ -29,8 +29,7 @@ class InMemoryFactory implements UserProviderFactoryInterface { $definition = $container->setDefinition($id, new DefinitionDecorator('security.user.provider.in_memory')); - - foreach ($config as $username => $user) { + foreach ($config['users'] as $username => $user) { $userId = $id.'_'.$username; $container @@ -44,24 +43,24 @@ class InMemoryFactory implements UserProviderFactoryInterface public function getKey() { - return 'users'; - } - - public function getFixableKey() - { - return 'user'; + return 'memory'; } public function addConfiguration(NodeDefinition $node) { $node - ->useAttributeAsKey('name') - ->prototype('array') - ->children() - ->scalarNode('password')->defaultValue(uniqid())->end() - ->arrayNode('roles') - ->beforeNormalization()->ifString()->then(function($v) { return preg_split('/\s*,\s*/', $v); })->end() - ->prototype('scalar')->end() + ->fixXmlConfig('user') + ->children() + ->arrayNode('users') + ->useAttributeAsKey('name') + ->prototype('array') + ->children() + ->scalarNode('password')->defaultValue(uniqid())->end() + ->arrayNode('roles') + ->beforeNormalization()->ifString()->then(function($v) { return preg_split('/\s*,\s*/', $v); })->end() + ->prototype('scalar')->end() + ->end() + ->end() ->end() ->end() ->end() diff --git a/src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/UserProvider/UserProviderFactoryInterface.php b/src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/UserProvider/UserProviderFactoryInterface.php index 947b2c6100..a6e4a7642f 100644 --- a/src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/UserProvider/UserProviderFactoryInterface.php +++ b/src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/UserProvider/UserProviderFactoryInterface.php @@ -26,7 +26,5 @@ interface UserProviderFactoryInterface function getKey(); - function getFixableKey(); - function addConfiguration(NodeDefinition $builder); } diff --git a/src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php b/src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php index 1c69b2b00f..7bed1e65d0 100644 --- a/src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php +++ b/src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php @@ -472,9 +472,9 @@ class SecurityExtension extends Extension } // Chain provider - if ($provider['providers']) { + if (isset($provider['chain'])) { $providers = array(); - foreach ($provider['providers'] as $providerName) { + foreach ($provider['chain']['providers'] as $providerName) { $providers[] = new Reference($this->getUserProviderId(strtolower($providerName))); } diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Fixtures/php/container1.php b/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Fixtures/php/container1.php index 078db51133..f92cab658a 100644 --- a/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Fixtures/php/container1.php +++ b/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Fixtures/php/container1.php @@ -18,26 +18,34 @@ $container->loadFromExtension('security', array( ), 'providers' => array( 'default' => array( - 'users' => array( - 'foo' => array('password' => 'foo', 'roles' => 'ROLE_USER'), + 'memory' => array( + 'users' => array( + 'foo' => array('password' => 'foo', 'roles' => 'ROLE_USER'), + ), ), ), 'digest' => array( - 'users' => array( - 'foo' => array('password' => 'foo', 'roles' => 'ROLE_USER, ROLE_ADMIN'), + 'memory' => array( + 'users' => array( + 'foo' => array('password' => 'foo', 'roles' => 'ROLE_USER, ROLE_ADMIN'), + ), ), ), 'basic' => array( - 'users' => array( - 'foo' => array('password' => '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33', 'roles' => 'ROLE_SUPER_ADMIN'), - 'bar' => array('password' => '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33', 'roles' => array('ROLE_USER', 'ROLE_ADMIN')), + 'memory' => array( + 'users' => array( + 'foo' => array('password' => '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33', 'roles' => 'ROLE_SUPER_ADMIN'), + 'bar' => array('password' => '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33', 'roles' => array('ROLE_USER', 'ROLE_ADMIN')), + ), ), ), 'service' => array( 'id' => 'user.manager', ), 'chain' => array( - 'providers' => array('service', 'basic'), + 'chain' => array( + 'providers' => array('service', 'basic'), + ), ), ), diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Fixtures/xml/container1.xml b/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Fixtures/xml/container1.xml index 9a5b3a574f..15c48d6b97 100644 --- a/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Fixtures/xml/container1.xml +++ b/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Fixtures/xml/container1.xml @@ -17,21 +17,29 @@ - + + + - + + + - - + + + + - + + + diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Fixtures/yml/container1.yml b/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Fixtures/yml/container1.yml index cc67e43187..a4dc65bd6a 100644 --- a/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Fixtures/yml/container1.yml +++ b/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Fixtures/yml/container1.yml @@ -13,19 +13,23 @@ security: providers: default: - users: - foo: { password: foo, roles: ROLE_USER } + memory: + users: + foo: { password: foo, roles: ROLE_USER } digest: - users: - foo: { password: foo, roles: 'ROLE_USER, ROLE_ADMIN' } + memory: + users: + foo: { password: foo, roles: 'ROLE_USER, ROLE_ADMIN' } basic: - users: - foo: { password: 0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33, roles: ROLE_SUPER_ADMIN } - bar: { password: 0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33, roles: [ROLE_USER, ROLE_ADMIN] } + memory: + users: + foo: { password: 0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33, roles: ROLE_SUPER_ADMIN } + bar: { password: 0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33, roles: [ROLE_USER, ROLE_ADMIN] } service: id: user.manager chain: - providers: [service, basic] + chain: + providers: [service, basic] firewalls: