0

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 ?

Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307

2 Answers2

1

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.

John Noel
  • 1,401
  • 10
  • 13
1

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

Yassine Guedidi
  • 1,695
  • 11
  • 12