Search code examples
phpsymfonyauthenticationsymfony4symfony-security

How to connect from SonataAdmin authenticator to other authenticator?


I have a symfony application which has inside the Sonata Admin Bundle for the admin part, with it's own firewall (admin) and the firewall for the user part of the application (main).

At the moment, the admin which is connected with sonata can't access the API that is designed for the user because it is authenticated for the Sonata Admin Bundle authenticator and for the API it sees him as a null user or not authenticated one.

I want to allow the admin to access an API that is made for the part of the application that is behind the firewall for the user part.

Config for the firewalls in the security.yaml file:

 firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        admin:
            pattern:            ^/admin(.*)
            form_login:
                provider:       app_user_admin
                login_path:     admin_login
                use_forward:    false
                check_path:     admin_login
                failure_path:   null
            logout:
                path:           admin_logout
                target:         admin_login
            anonymous:          true
            guard:
                authenticators:
                    - App\Security\AdminLoginAuthenticator
        main:
            anonymous: true
            logout:
                path:   security_logout
            guard:
                authenticators:
                    - App\Security\UserLoginAuthenticator

Is there a way to can connect the two authenticators for the admin? Like, on a success login for the admin to call the authenticator for the main firewall?


Solution

  • After some digging and some help, I found out that symfony security has something like this built in.

    It's called Symfony context and does the exact same thing.

    For future reference, this is what you really need to add to the config file:

    firewalls:
            dev:
                pattern: ^/(_(profiler|wdt)|css|images|js)/
                security: false
            admin:
                context: just_a_random_name
                pattern:            ^/admin(.*)
                form_login:
                    provider:       app_user_admin
                    login_path:     admin_login
                    use_forward:    false
                    check_path:     admin_login
                    failure_path:   null
                logout:
                    path:           admin_logout
                    target:         admin_login
                anonymous:          true
                guard:
                    authenticators:
                        - App\Security\AdminLoginAuthenticator
            main:
                context: just_a_random_name
                anonymous: true
                logout:
                    path:   security_logout
                guard:
                    authenticators:
                        - App\Security\UserLoginAuthenticator