Skip to content

Commit

Permalink
[SecurityBundle] Firewall providers building - code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
blanchonvincent authored and fabpot committed Dec 12, 2014
1 parent b0ab687 commit c3c904d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 27 deletions.
Expand Up @@ -495,6 +495,7 @@ private function createUserDaoProvider($name, $provider, ContainerBuilder $conta
{
$name = $this->getUserProviderId(strtolower($name));

// Doctrine Entity and In-memory DAO provider are managed by factories
foreach ($this->userProviderFactories as $factory) {
$key = str_replace('-', '_', $factory->getKey());

Expand All @@ -521,37 +522,12 @@ private function createUserDaoProvider($name, $provider, ContainerBuilder $conta

$container
->setDefinition($name, new DefinitionDecorator('security.user.provider.chain'))
->addArgument($providers)
;

return $name;
}

// Doctrine Entity DAO provider
if (isset($provider['entity'])) {
$container
->setDefinition($name, new DefinitionDecorator('security.user.provider.entity'))
->addArgument($provider['entity']['class'])
->addArgument($provider['entity']['property'])
;
->addArgument($providers);

return $name;
}

// In-memory DAO provider
$definition = $container->setDefinition($name, new DefinitionDecorator('security.user.provider.in_memory'));
foreach ($provider['users'] as $username => $user) {
$userId = $name.'_'.$username;

$container
->setDefinition($userId, new DefinitionDecorator('security.user.provider.in_memory.user'))
->setArguments(array($username, (string) $user['password'], $user['roles']))
;

$definition->addMethodCall('createUser', array(new Reference($userId)));
}

return $name;
throw new InvalidConfigurationException(sprintf('Unable to create definition for "%s" user provider', $name));
}

private function getUserProviderId($name)
Expand Down
@@ -0,0 +1,25 @@
<?php

namespace Symfony\Bundle\SecurityBundle\Tests\DependencyInjection\Fixtures\UserProvider;

use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\UserProvider\UserProviderFactoryInterface;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class DummyProvider implements UserProviderFactoryInterface
{
public function create(ContainerBuilder $container, $id, $config)
{

}

public function getKey()
{
return 'foo';
}

public function addConfiguration(NodeDefinition $node)
{

}
}
Expand Up @@ -13,6 +13,7 @@

use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension;
use Symfony\Bundle\SecurityBundle\SecurityBundle;
use Symfony\Bundle\SecurityBundle\Tests\DependencyInjection\Fixtures\UserProvider\DummyProvider;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class SecurityExtensionTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -66,6 +67,33 @@ public function testFirewallWithoutAuthenticationListener()
$container->compile();
}

/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
* @expectedExceptionMessage Unable to create definition for "security.user.provider.concrete.my_foo" user provider
*/
public function testFirewallWithInvalidUserProvider()
{
$container = $this->getRawContainer();

$extension = $container->getExtension('security');
$extension->addUserProviderFactory(new DummyProvider());

$container->loadFromExtension('security', array(
'providers' => array(
'my_foo' => array('foo' => []),
),

'firewalls' => array(
'some_firewall' => array(
'pattern' => '/.*',
'http_basic' => [],
),
),
));

$container->compile();
}

protected function getRawContainer()
{
$container = new ContainerBuilder();
Expand Down

0 comments on commit c3c904d

Please sign in to comment.