alexandre marillesse
alexandre marillesse

Reputation: 85

How to configure firewall with multiple provider on Symfony 4?

I'm writing this message to ask you how to launch my Symfony app with multiple user providers? This is my security yaml:

security:
    encoders:
        App\Entity\User:
            algorithm: auto


    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
    providers:
        chain_provider:
            chain:
                providers: [in_memory, app_user_provider]
        # used to reload user from session & other features (e.g. switch_user)
        app_user_provider:
            entity:
                class: App\Entity\User
                property: email
        in_memory:
            memory:
                users:
                    foo: { password: test , roles: [ 'ROLE_ADMIN' ] }
        # used to reload user from session & other features (e.g. switch_user)
#            memory:
#                users:
#                    admin: { password: '123412A' ,roles: ['ROLE_ADMIN']}
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
#            form_login:
#                csrf_token_generator : security.csrf.token_manager
            anonymous: true

            logout:
                path: app_logout
            guard:
                authenticators:
                    - App\Security\UtilisateurAuthenticator

                # where to redirect after logout
                # target: app_any_route

                # where to redirect after logout
                # target: app_any_route

            # activate different ways to authenticate
            # https://symfony.com/doc/current/security.html#firewalls-authentication

            # https://symfony.com/doc/current/security/impersonating_user.html
            # switch_user: true

    # Easy way to control access for large sections of your site
    # Note: Only the *first* access control that matches will be used
    access_control:
         - { path: ^/Conversation , roles: [ROLE_ADMIN , ROLE_USER] }
         - { path: ^/admin, roles: ROLE_ADMIN }
         - { path: ^/login$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        # - { path: ^/profile, roles: ROLE_USER }

I get this error message, how to solve it?

Not configuring explicitly the provider for the "guard" listener on "main" firewall is ambiguous as there is more than one registered provider.

Upvotes: 1

Views: 1528

Answers (1)

rugolinifr
rugolinifr

Reputation: 1281

You just need to declare the user provider you want to use within your main firewall. It must be one of those declared under the providers key.

# security.yaml
# ...

main:
    provider: chain_provider # or app_user_provider or in_memory
    form_login: 
    # ...

Upvotes: 2

Related Questions