Skip to content

Commit

Permalink
[DoctrineBridge] Made it possible to change the manager used by the p…
Browse files Browse the repository at this point in the history
…rovider
  • Loading branch information
stof committed Dec 22, 2011
1 parent 10c60ab commit 24319bb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bridge\Doctrine\Security\User;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
Expand All @@ -32,8 +33,9 @@ class EntityUserProvider implements UserProviderInterface
private $property;
private $metadata;

public function __construct(ObjectManager $em, $class, $property = null)
public function __construct(ManagerRegistry $registry, $class, $property = null, $managerName = null)
{
$em = $registry->getManager($managerName);
$this->class = $class;
$this->metadata = $em->getClassMetadata($class);

Expand Down
Expand Up @@ -31,6 +31,7 @@ public function create(ContainerBuilder $container, $id, $config)
->setDefinition($id, new DefinitionDecorator('doctrine.orm.security.user.provider'))
->addArgument($config['class'])
->addArgument($config['property'])
->addArgument($config['manager_name'])
;
}

Expand All @@ -45,6 +46,7 @@ public function addConfiguration(NodeDefinition $node)
->children()
->scalarNode('class')->isRequired()->cannotBeEmpty()->end()
->scalarNode('property')->defaultNull()->end()
->scalarNode('manager_name')->defaultNull()->end()
->end()
;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/DoctrineBundle/Resources/config/orm.xml
Expand Up @@ -75,7 +75,7 @@

<!-- security -->
<service id="doctrine.orm.security.user.provider" class="%doctrine.orm.security.user.provider.class%" abstract="true" public="false">
<argument type="service" id="doctrine.orm.entity_manager" />
<argument type="service" id="doctrine" />
</service>
</services>
</container>
Expand Up @@ -33,7 +33,7 @@ public function testRefreshUserGetsUserByPrimaryKey()
$em->persist($user2);
$em->flush();

$provider = new EntityUserProvider($em, 'Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity', 'name');
$provider = new EntityUserProvider($this->getManager($em), 'Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity', 'name');

// try to change the user identity
$user1->name = 'user2';
Expand All @@ -46,7 +46,7 @@ public function testRefreshUserRequiresId()
$em = $this->createTestEntityManager();

$user1 = new CompositeIdentEntity(null, null, 'user1');
$provider = new EntityUserProvider($em, 'Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity', 'name');
$provider = new EntityUserProvider($this->getManager($em), 'Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity', 'name');

$this->setExpectedException(
'InvalidArgumentException',
Expand All @@ -65,7 +65,7 @@ public function testRefreshInvalidUser()
$em->persist($user1);
$em->flush();

$provider = new EntityUserProvider($em, 'Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity', 'name');
$provider = new EntityUserProvider($this->getManager($em), 'Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity', 'name');

$user2 = new CompositeIdentEntity(1, 2, 'user2');
$this->setExpectedException(
Expand All @@ -92,6 +92,17 @@ public function testSupportProxy()
$this->assertTrue($provider->supportsClass(get_class($user2)));
}

private function getManager($em, $name = null)
{
$manager = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
$manager->expects($this->once())
->method('getManager')
->with($this->equalTo($name))
->will($this->returnValue($em));

return $manager;
}

private function createSchema($em)
{
$schemaTool = new SchemaTool($em);
Expand Down

0 comments on commit 24319bb

Please sign in to comment.