Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

[DoctrineBridge] Made it possible to change the manager used by the provider #2928

Merged
merged 4 commits into from

6 participants

@stof
Collaborator

This improves the support of several entity managers by allowing using a non-default one for the provider.

It is BC for the user as the default value for the name is null which means using the default one.

I'm preparing the PR for DoctrineBundle too

@stof
Collaborator

I'm wondering if the EntityFactory used to integrate the bundles with SecurityBundle should be moved to the bridge or not. Moving it (making the key and the abstract service id configurable) would allow reusing it in all Doctrine bundles instead of copy-pasting it (see the CouchDBBundle pull request linked above).
The bridge was initially meant to integrate third party libraries with the components and this class is about the SecurityBundle, not the component. But on the other hand, we already share the abstract DI extension between the bundles using the bridge.

@stof
Collaborator

@fabpot @beberlei thoughts ?

.../Bridge/Doctrine/Security/User/EntityUserProvider.php
@@ -11,6 +11,7 @@
namespace Symfony\Bridge\Doctrine\Security\User;
+use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\ObjectManager;
@Tobion Collaborator
Tobion added a note

can be removed now, or?

@stof Collaborator
stof added a note

indeed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@stof
Collaborator

@fabpot @beberlei what do you thing about moving the EntityFactory to the bridge ?

@henrikbjorn

Missing mongodb bundle

@stof
Collaborator

@henrikbjorn I was planning to send the PR for mongodb too but the namespace change was not merged yet yesterday. And now, you want to wait for the answer to know if I need to copy-paste the factory to the mongodb bundle too or if I move it to the bridge

@beberlei

I think moving it to the Bridge makes sense if we can re-use across all the bundles then. Also it is really about integrating security with doctrine, so its a bridge topic.

@stof
Collaborator

I updated the PR to move the factory to the bridge. The DoctrineBundle and DoctrineCouchDBBundle PRs are updated too.

@fabpot the PR should be ready to be merged

@fabpot
Owner

Tests do not pass for me:

...E

Time: 0 seconds, Memory: 14.75Mb

There was 1 error:

1) Symfony\Tests\Bridge\Doctrine\Security\User\EntityUserProviderTest::testSupportProxy
Argument 1 passed to Symfony\Bridge\Doctrine\Security\User\EntityUserProvider::__construct() must implement interface Doctrine\Common\Persistence\ManagerRegistry, instance of Doctrine\ORM\EntityManager given, called in tests/Symfony/Tests/Bridge/Doctrine/Security/User/EntityUserProviderTest.php on line 89 and defined

src/Symfony/Bridge/Doctrine/Security/User/EntityUserProvider.php:35
tests/Symfony/Tests/Bridge/Doctrine/Security/User/EntityUserProviderTest.php:89
@stof
Collaborator

@fabpot I fixed it before your comment (thanks travis ^^). It was the test added in my other PR to 2.0 and so not updated in the original commit. I forgot it when rebasing

@stof stof referenced this pull request in doctrine/DoctrineMongoDBBundle
Merged

Added a user provider #66

@fabpot fabpot referenced this pull request from a commit
@fabpot fabpot merged branch stof/entity_provider_registry (PR #2928)
Commits
-------

373ab4c Fixed tests added from 2.0
9653be6 Moved the EntityFactory to the bridge
caa105f Removed useless use statement
24319bb [DoctrineBridge] Made it possible to change the manager used by the provider

Discussion
----------

[DoctrineBridge] Made it possible to change the manager used by the provider

This improves the support of several entity managers by allowing using a non-default one for the provider.

It is BC for the user as the default value for the name is ``null`` which means using the default one.

I'm preparing the PR for DoctrineBundle too

---------------------------------------------------------------------------

by stof at 2011/12/19 14:16:38 -0800

I'm wondering if the EntityFactory used to integrate the bundles with SecurityBundle should be moved to the bridge or not. Moving it (making the key and the abstract service id configurable) would allow reusing it in all Doctrine bundles instead of copy-pasting it (see the CouchDBBundle pull request linked above).
The bridge was initially meant to integrate third party libraries with the components and this class is about the SecurityBundle, not the component. But on the other hand, we already share the abstract DI extension between the bundles using the bridge.

---------------------------------------------------------------------------

by stof at 2011/12/19 14:17:48 -0800

@fabpot @beberlei thoughts ?

---------------------------------------------------------------------------

by stof at 2011/12/21 04:43:50 -0800

@fabpot @beberlei what do you thing about moving the EntityFactory to the bridge ?

---------------------------------------------------------------------------

by henrikbjorn at 2011/12/21 05:10:56 -0800

Missing mongodb bundle

---------------------------------------------------------------------------

by stof at 2011/12/21 05:52:06 -0800

@henrikbjorn I was planning to send the PR for mongodb too but the namespace change was not merged yet yesterday. And now, you want to wait for the answer to know if I need to copy-paste the factory to the mongodb bundle too or if I move it to the bridge

---------------------------------------------------------------------------

by beberlei at 2011/12/21 15:14:17 -0800

I think moving it to the Bridge makes sense if we can re-use across all the bundles then. Also it is really about integrating security with doctrine, so its a bridge topic.

---------------------------------------------------------------------------

by stof at 2011/12/22 08:39:52 -0800

I updated the PR to move the factory to the bridge. The DoctrineBundle and DoctrineCouchDBBundle PRs are updated too.

@fabpot the PR should be ready to be merged

---------------------------------------------------------------------------

by fabpot at 2011/12/22 08:53:02 -0800

Tests do not pass for me:

    ...E

    Time: 0 seconds, Memory: 14.75Mb

    There was 1 error:

    1) Symfony\Tests\Bridge\Doctrine\Security\User\EntityUserProviderTest::testSupportProxy
    Argument 1 passed to Symfony\Bridge\Doctrine\Security\User\EntityUserProvider::__construct() must implement interface Doctrine\Common\Persistence\ManagerRegistry, instance of Doctrine\ORM\EntityManager given, called in tests/Symfony/Tests/Bridge/Doctrine/Security/User/EntityUserProviderTest.php on line 89 and defined

    src/Symfony/Bridge/Doctrine/Security/User/EntityUserProvider.php:35
    tests/Symfony/Tests/Bridge/Doctrine/Security/User/EntityUserProviderTest.php:89

---------------------------------------------------------------------------

by stof at 2011/12/22 08:56:33 -0800

@fabpot I fixed it before your comment (thanks travis ^^). It was the test added in my other PR to 2.0 and so not updated in the original commit. I forgot it when rebasing
4404d6f
@fabpot fabpot merged commit 373ab4c into symfony:master
@mmucklo mmucklo referenced this pull request from a commit
@fabpot fabpot merged branch stof/entity_provider_registry (PR #2928)
Commits
-------

373ab4c Fixed tests added from 2.0
9653be6 Moved the EntityFactory to the bridge
caa105f Removed useless use statement
24319bb [DoctrineBridge] Made it possible to change the manager used by the provider

Discussion
----------

[DoctrineBridge] Made it possible to change the manager used by the provider

This improves the support of several entity managers by allowing using a non-default one for the provider.

It is BC for the user as the default value for the name is ``null`` which means using the default one.

I'm preparing the PR for DoctrineBundle too

---------------------------------------------------------------------------

by stof at 2011/12/19 14:16:38 -0800

I'm wondering if the EntityFactory used to integrate the bundles with SecurityBundle should be moved to the bridge or not. Moving it (making the key and the abstract service id configurable) would allow reusing it in all Doctrine bundles instead of copy-pasting it (see the CouchDBBundle pull request linked above).
The bridge was initially meant to integrate third party libraries with the components and this class is about the SecurityBundle, not the component. But on the other hand, we already share the abstract DI extension between the bundles using the bridge.

---------------------------------------------------------------------------

by stof at 2011/12/19 14:17:48 -0800

@fabpot @beberlei thoughts ?

---------------------------------------------------------------------------

by stof at 2011/12/21 04:43:50 -0800

@fabpot @beberlei what do you thing about moving the EntityFactory to the bridge ?

---------------------------------------------------------------------------

by henrikbjorn at 2011/12/21 05:10:56 -0800

Missing mongodb bundle

---------------------------------------------------------------------------

by stof at 2011/12/21 05:52:06 -0800

@henrikbjorn I was planning to send the PR for mongodb too but the namespace change was not merged yet yesterday. And now, you want to wait for the answer to know if I need to copy-paste the factory to the mongodb bundle too or if I move it to the bridge

---------------------------------------------------------------------------

by beberlei at 2011/12/21 15:14:17 -0800

I think moving it to the Bridge makes sense if we can re-use across all the bundles then. Also it is really about integrating security with doctrine, so its a bridge topic.

---------------------------------------------------------------------------

by stof at 2011/12/22 08:39:52 -0800

I updated the PR to move the factory to the bridge. The DoctrineBundle and DoctrineCouchDBBundle PRs are updated too.

@fabpot the PR should be ready to be merged

---------------------------------------------------------------------------

by fabpot at 2011/12/22 08:53:02 -0800

Tests do not pass for me:

    ...E

    Time: 0 seconds, Memory: 14.75Mb

    There was 1 error:

    1) Symfony\Tests\Bridge\Doctrine\Security\User\EntityUserProviderTest::testSupportProxy
    Argument 1 passed to Symfony\Bridge\Doctrine\Security\User\EntityUserProvider::__construct() must implement interface Doctrine\Common\Persistence\ManagerRegistry, instance of Doctrine\ORM\EntityManager given, called in tests/Symfony/Tests/Bridge/Doctrine/Security/User/EntityUserProviderTest.php on line 89 and defined

    src/Symfony/Bridge/Doctrine/Security/User/EntityUserProvider.php:35
    tests/Symfony/Tests/Bridge/Doctrine/Security/User/EntityUserProviderTest.php:89

---------------------------------------------------------------------------

by stof at 2011/12/22 08:56:33 -0800

@fabpot I fixed it before your comment (thanks travis ^^). It was the test added in my other PR to 2.0 and so not updated in the original commit. I forgot it when rebasing
da8b860
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
17 ...n/Security/UserProvider/EntityFactory.php → ...n/Security/UserProvider/EntityFactory.php
@@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
-namespace Symfony\Bundle\DoctrineBundle\DependencyInjection\Security\UserProvider;
+namespace Symfony\Bridge\Doctrine\DependencyInjection\Security\UserProvider;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
@@ -25,18 +25,28 @@
*/
class EntityFactory 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('doctrine.orm.security.user.provider'))
+ ->setDefinition($id, new DefinitionDecorator($this->providerId))
->addArgument($config['class'])
->addArgument($config['property'])
+ ->addArgument($config['manager_name'])
;
}
public function getKey()
{
- return 'entity';
+ return $this->key;
}
public function addConfiguration(NodeDefinition $node)
@@ -45,6 +55,7 @@ public function addConfiguration(NodeDefinition $node)
->children()
->scalarNode('class')->isRequired()->cannotBeEmpty()->end()
->scalarNode('property')->defaultNull()->end()
+ ->scalarNode('manager_name')->defaultNull()->end()
->end()
;
}
View
5 src/Symfony/Bridge/Doctrine/Security/User/EntityUserProvider.php
@@ -11,7 +11,7 @@
namespace Symfony\Bridge\Doctrine\Security\User;
-use Doctrine\Common\Persistence\ObjectManager;
+use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\User\UserProviderInterface;
@@ -32,8 +32,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);
View
4 src/Symfony/Bundle/DoctrineBundle/DoctrineBundle.php
@@ -12,10 +12,10 @@
namespace Symfony\Bundle\DoctrineBundle;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
-use Symfony\Bundle\DoctrineBundle\DependencyInjection\Security\UserProvider\EntityFactory;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\DoctrineValidationPass;
+use Symfony\Bridge\Doctrine\DependencyInjection\Security\UserProvider\EntityFactory;
use Symfony\Bundle\DoctrineBundle\DependencyInjection\Compiler\RegisterEventListenersAndSubscribersPass;
/**
@@ -33,7 +33,7 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new RegisterEventListenersAndSubscribersPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION);
if ($container->hasExtension('security')) {
- $container->getExtension('security')->addUserProviderFactory(new EntityFactory());
+ $container->getExtension('security')->addUserProviderFactory(new EntityFactory('entity', 'doctrine.orm.security.user.provider'));
}
$container->addCompilerPass(new DoctrineValidationPass('orm'));
View
2  src/Symfony/Bundle/DoctrineBundle/Resources/config/orm.xml
@@ -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>
View
19 tests/Symfony/Tests/Bridge/Doctrine/Security/User/EntityUserProviderTest.php
@@ -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';
@@ -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',
@@ -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(
@@ -86,12 +86,23 @@ public function testSupportProxy()
$em->flush();
$em->clear();
- $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 = $em->getReference('Symfony\Tests\Bridge\Doctrine\Fixtures\CompositeIdentEntity', array('id1' => 1, 'id2' => 1));
$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);
Something went wrong with that request. Please try again.