symfony impersonate across different hosts

2019-09-07 04:57发布

问题:

I'm trying to impersonate different users in Symfony, but different that other examples like Symfony impersonation - separate firewalls and separate user providers, in my case is not only the provider that's different, but the host.

firewalls:
   admin:
        pattern: ^/
        host: "%host_admin%"
        provider: admin
[...]
      switch_user:
        role: ROLE_ADMIN
        provider: client
        #host: "%host_public%" <-- tried this.. does not work
        context: cmb_context

   client:
        pattern: ^/
        host: "%host_public%"
        provider: client
[...]
      switch_user:
        role: ROLE_ADMIN
        provider: client
        context: cmb_context

So, with this set-up when i try to impersonate a user, it remains in the admin site and of course the client, that's been picked up correctly, can not authenticate in the admin side. When moving to the client, of course, the auth session is not present there.

I tried putting the host for the system to know where to impersonate "in" with no luck.

I might have to fall down to the listener mentioned in http://symfony.com/doc/current/security/impersonating_user.html#events but i have not found details on how to do that.

Any ideas? Thanks in advance.

回答1:

The chainUserProvider allows you to add one or more user providers together so you can fetch users from these multiple sources. Note that the order in which you add the providers to the ChainUserProvider will matter: as soon as a user has been found, it will check against that user, even if the credentials of that user fails.

parameters:
    chain_providers: "mydatabase1,mydatabase2"

security:
    providers:
        mydatabase1:
            entity:
                class: My\UserBundle\Entity\Admin
                property: username

        mydatabase2:
            entity:
                class: My\UserBundle\Entity\Customer
                property: username

        my_chain:
            chain:
                providers: %chain_providers%