[DoctrineBridge] Made it possible to change the manager used by the provider
This commit is contained in:
parent
10c60ab5d5
commit
24319bb0f4
@ -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;
|
||||
@ -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);
|
||||
|
||||
|
@ -31,6 +31,7 @@ class EntityFactory implements UserProviderFactoryInterface
|
||||
->setDefinition($id, new DefinitionDecorator('doctrine.orm.security.user.provider'))
|
||||
->addArgument($config['class'])
|
||||
->addArgument($config['property'])
|
||||
->addArgument($config['manager_name'])
|
||||
;
|
||||
}
|
||||
|
||||
@ -45,6 +46,7 @@ class EntityFactory implements UserProviderFactoryInterface
|
||||
->children()
|
||||
->scalarNode('class')->isRequired()->cannotBeEmpty()->end()
|
||||
->scalarNode('property')->defaultNull()->end()
|
||||
->scalarNode('manager_name')->defaultNull()->end()
|
||||
->end()
|
||||
;
|
||||
}
|
||||
|
@ -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>
|
||||
|
@ -33,7 +33,7 @@ class EntityUserProviderTest extends DoctrineOrmTestCase
|
||||
$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';
|
||||
@ -46,7 +46,7 @@ class EntityUserProviderTest extends DoctrineOrmTestCase
|
||||
$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',
|
||||
@ -65,7 +65,7 @@ class EntityUserProviderTest extends DoctrineOrmTestCase
|
||||
$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(
|
||||
@ -92,6 +92,17 @@ class EntityUserProviderTest extends DoctrineOrmTestCase
|
||||
$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);
|
||||
|
Reference in New Issue
Block a user