formapro / FpOpenIdBundle

Symfony2 OpenID security extension
53 stars 31 forks source link

add support of multiple entity managers #80

Open YahouSerious opened 11 years ago

YahouSerious commented 11 years ago

Is it possible to add a configuration option "entity_manager" for multitenancy ? Thanks

makasim commented 11 years ago

Sorry, I dont get what you want. Could you be more descriptive?

YahouSerious commented 11 years ago

I'm working on a multi customer application( multi database connection ). Each client got his own database, so I need to use a custom entity manager. Your "identity_class" use the default entity manager. This could be a good thing to use custom entity manager(in my case :)).

cf: http://symfony.com/doc/master/cookbook/doctrine/multiple_entity_managers.html

makasim commented 11 years ago

Good question. I dont know a good way to solve it, honestly I dont even have time to investigate it deeper. So you do a PR, that's would be really good.

Solution for know to create your own provider and use it. or overwrite this service

makasim commented 11 years ago

closed by mistake

YahouSerious commented 11 years ago

ok, but at this time, i got trouble with class OpenIdIdentity. It can't inherite from Fp\OpenIdBundle\Entity\UserIdentity. Are you sure you must not put something like "@ORM\Column(name="identity", type="string")" in your classes ? Thanks.

makasim commented 11 years ago

make sure doctrine auto mapping enabled.

YahouSerious commented 11 years ago

I can't enable auto mapping when several entity managers are defined

makasim commented 11 years ago

so you have to add fp openid mappings explisitly, use this example:

doctrine:
    orm:
        entity_managers:
            default:
                # The name of a DBAL connection (the one marked as default is used if not set)
                connection:                               default
                mappings: # Required
                    CoreBundle:                           { type: annotation, dir: Entity/, alias: '' }
                    JMSPaymentCoreBundle:                 { type: xml, dir: Resources/config/doctrine }
YahouSerious commented 11 years ago

Like this ? mappings: FpOpenIdBundle: type: annotation prefix: Fp\OpenId\Entity dir: "%kernel.root_dir%/../vendor/fp/openid-bundle/Fp/OpenIdBundle/Entity" alias: 'FpOpenId' is_bundle: false

makasim commented 11 years ago

no, fpopenid uses xml for schemas, so that would be (not tested):

mappings: 
    FpOpenIdBundle: { type: xml, dir: Resources/config/doctrine }
YahouSerious commented 11 years ago

Thanks it works ! :)

Now i'm trying to inject the custom entity manager ...

config.yml:

# OpenId
fp_open_id:
    db_driver: orm
    entity_manager: "@doctrine.orm.customer_entity_manager"
    identity_class: App\SiteBundle\Entity\OpenIdIdentity

Configuration.php

/**
     * 
     *  {@inheritDoc}
     */
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('fp_open_id');

        $rootNode
            ->children()
                ->scalarNode('db_driver')->defaultNull()->end()
                ->scalarNode('entity_manager')->defaultValue('doctrine.orm.entity_manager')->end()
                ->scalarNode('identity_class')->defaultNull()->end()
            ->end()
        ;

        $this->addTemplateSection($rootNode);

        return $treeBuilder;
    }

FpOpenIdExtension.php

// ....

/**
     * {@inheritDoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        // ... 

        if ($configs['entity_manager']) {
            $this->loadEntityManagerr($configs, $container, $loader);
        }
    }

/**
     * @param array $configs
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
     *
     * @throws \InvalidArgumentException
     */
    public function loadEntityManagerr(array $configs, ContainerBuilder $container, $loader)
    {
        $container->setParameter('entity_manager', $configs['entity_manager']);
    }

And here is the trouble, i don't know how inject the entity manager in fp_openid.identity_manager service:

<services>
        <service id="fp_openid.identity_manager" class="%fp_openid.identity_manager.class%">
           <!--  <argument type="service" id="doctrine.orm.entity_manager" /> --> 
            <argument type="service" id="%entity_manager%" />
            <argument>%fp_openid.model.identity.class%</argument>
        </service>

    </services>

Thanks for your help