Pierre de LESPINAY
Pierre de LESPINAY

Reputation: 46178

Use the chain provider defined from a service

I use multiple user providers in my app:

security:
    providers:
        chain_provider:
            chain:
                providers: [entity, json]
        entity:
            id: myapp.entity.user_provider
        json:
            id: myapp.json.user_provider

Now I need to load a user from a service

services:
    myapp.my.service:
        class: AppBundle\Services\My
        argument: [@the.defined.chain.provider]
// AppBundle\Services\My
namespace AppBundle\Services;
class My
{
    public function loadUser($username)
    {
        return $this->userProvider->loadUserByUsername($username);
    }
}

How can I use the chain provider explicitly ?

Upvotes: 0

Views: 1341

Answers (2)

Yassine Guedidi
Yassine Guedidi

Reputation: 1715

By looking at the code of SecurityBundle, you just can use the service security.user.provider.concrete.{name}, so in your case security.user.provider.concrete.chain_provider

Upvotes: 1

user4545769
user4545769

Reputation:

The providers are initialised as services and you can do the same with the Chain provider:

services:
    the.defined.chain.provider:
        class: Symfony\Component\Security\Core\User\ChainUserProvider
        arguments: [ [ @myapp.entity.user_provider, @myapp.json.user_provider ] ]

Then in your security.yml file, replace your providers with:

security:
    providers:
        default: 
            id: the.defined.chain.provider

And in your services file just keep your example for myapp.my.service

So you'd be creating the user provider chain yourself as a service (which is effectively what the security bundle does with your configuration) and using it in two places.

Upvotes: 1

Related Questions