Symfony2 ACL - can't set multiple user sources on a single provider
Asked Answered
S

2

8

I'm trying to figure out why I can't set multiple user providers into a single provider. Currently I'm configuring ACL. For the user providers I want to have a couple of "hard-coded" users and users which would be loaded from a database.

Reading the documentation it's stated that you're not required to have two user providers - one for the in_memory users and one for the database users. You should be able to combine them into a single user provider (which is what I'm trying to do).

The suggested configuration is:

security:
    providers:
        main_provider:
            memory:
                users:
                    foo: { password: test }
            entity:
                class: Acme\UserBundle\Entity\User,
                property: username

My configuration is:

security:
    providers:
        main_provider:
            memory:
                users:
                    foo: { password: test }
            entity:
                class: Company\EntitiesBundle\Entity\User,
                property: username

Unfortunately I get this exception:

InvalidConfigurationException: Invalid configuration for path "security.providers.main_provider": You cannot set multiple provider types for the same provider

If I, however, set two different providers and chain them, it works without problems. I can't figure out why this would happen? It's clearly stated in the documentation - you can accomplish this even more easily by combining the two sources into a single provider.
What am I missing here?

Skite answered 18/11, 2012 at 16:47 Comment(0)
B
3

Why don't you chain providers? The documentation that you're referring states that you can use multiple user providers "...by creating a new provider that chains the two together".

http://symfony.com/doc/current/book/security.html#using-multiple-user-providers

Each authentication mechanism (e.g. HTTP Authentication, form login, etc) uses exactly one user provider, and will use the first declared user provider by default. But what if you want to specify a few users via configuration and the rest of your users in the database? This is possible by creating a new provider that chains the two together.

Now, all authentication mechanisms will use the chain_provider, since it's the first specified. The chain_provider will, in turn, try to load the user from both the in_memory and user_db providers.

All you have to do is to setup a chain provider.

# app/config/security.yml
security:
    providers:
        main_provider:
            chain:
                providers: [memory_provider, entity_provider]
        memory_provider:
            memory:
                users:
                    foo: { password: test }
        entity_provider:
            entity:
                class: Company\EntitiesBundle\Entity\User
                property: username
Batrachian answered 9/1, 2014 at 21:10 Comment(2)
Simply because the solution was not working at all, although it was documented. I have tried this on a clean symfony source and it still wasn't working at that time. I haven't needed this since then, so I don't know if it's fixed by now. Anyway, thank you for submitting your answer! Unfortunately I can't verify if this solution is working or not. I'll only upvote and when I have some time to test it, I will accept in the answer if it works :)Skite
Sorry for the long wait on this one. I was quite busy with other projects and just recently had time to experiment. It appears this problem is resolved in 2.4 and the example you provide works fine. Thanks for helping!Skite
W
-2

Which Symfony version are you using? If 2.0, in 2.0 documentation the configuration is slightly different:

# app/config/security.yml
security:
    providers:
        main_provider:
            users:
                foo: { password: test }
            entity: { class: Acme\UserBundle\Entity\User, property: username }

Notice the memory key missing.

Whortleberry answered 8/12, 2012 at 23:26 Comment(1)
The question is about multiple providers. This answer is for a single provider.Cacka

© 2022 - 2024 — McMap. All rights reserved.