bshaffer / oauth2-server-bundle

OAuth2 for your Symfony Application
MIT License
106 stars 72 forks source link

Provide easy means to change entity managers #32

Open mrardon opened 9 years ago

mrardon commented 9 years ago

Allow configuration to switch entity managers.

We use several database connections and manually map our entity managers. This causes problems when we don't want the OAuth entities to exist in the main entity manager.

As a workaround I added a compiler pass which while not the prettiest of solutions but it allows everything to work as intended.


use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class OAuthCompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        $replaceEmServices = [
            'oauth2.user_provider',
            'oauth2.scope_manager',
            'oauth2.storage.client_credentials',
            'oauth2.storage.authorization_code',
            'oauth2.storage.user_credentials',
            'oauth2.storage.access_token',
            'oauth2.storage.refresh_token',
            'oauth2.storage.scope',
            'oauth2.client_manager',
        ];

        //Set this to the service id of your entity manager
        $newEntityManager = 'doctrine.orm.connect_entity_manager';

        foreach ($replaceEmServices as $serviceId) {
            if ($container->hasDefinition($serviceId)) {
                $definition = $container->getDefinition($serviceId);
                $reference  = new Reference($newEntityManager);

                $definition->replaceArgument(0, $reference);
            }
        }
    }
}
Gmulti commented 9 years ago

Does it work with an object manager like mongodb odm ?