Braian Mellor
Braian Mellor

Reputation: 1954

How to manage several firewalls in symfony 3.4

I'm trying to manage a frontend and backend with different user's rol. Inside security.yml added this

security:
    providers:
        admin:
            entity:
                class:      LoginBundle:Usuarios
                property:   mail
        external:
            entity:
                class:      LoginBundle:UsuariosExternos
                property:   mail

    firewalls:
        # disables authentication for assets and the profiler, adapt it according to your needs
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            anonymous: true
            provider: admin
            form_login:
                login_path: /
            logout:
                path:   logout
                target: /
            access_denied_url: /eu/
        external:
            anonymous: true
            provider: external
            form_login:
                login_path: /
            logout:
                path:   logoutExternalUser
                target: /

With this codete firewall external is not working and when I log as a external (frontend) the logout path is /logout and not logoutExternalUser. Also with this config if I write the path /admin it goes to / and not to /admin/login We try to user a AccessDeniedHandlerInterface but we don't know how to get the user rol in that instance.

Any help?

Upvotes: 1

Views: 239

Answers (1)

revengeance
revengeance

Reputation: 891

You are missing patterns. That's why the firwalls are not fireing UP.

You should set pattern for which it listens.

Check, I updated code.

security:
providers:
    admin:
        entity:
            class:      LoginBundle:Usuarios # idk if you realized this correctly
            property:   mail
    external:
        entity:
            class:      LoginBundle:UsuariosExternos
            property:   mail

firewalls:
    admin:
        pattern: ^/admin # pattern in which this will activate
        anonymous: true
        provider: admin
        form_login:
            login_path: adminLogin # implement route
        logout:
            path:   security_logout # should work and destroy session
            target: /
        access_denied_url: /eu/
    external:
        pattern: ^/external
        anonymous: true
        provider: external
        form_login:
            login_path: loginExternal # implement route
        logout:
            path:   security_logout 
            target: /

I recommend you to check https://symfony.com/doc/3.4/security/guard_authentication.html

It's nice way to build any kind of auth you need, if default security doesn't work for you.

Upvotes: 3

Related Questions