T
T
Think With Your Head2016-11-04 21:24:34
symfony
Think With Your Head, 2016-11-04 21:24:34

How to make several firewalls work in symphony?

Hello!
There are two forms in the application, two entities - admin and user (I’ll make a reservation right away that I don’t want to combine them into one and I can’t, although this would solve the problem).
Separately, the firewalls work fine, but if you code them together, then the second one, which is lower (user), refuses to check_path and simply re-renders the login page without displaying errors. If you change the admin and user firewalls in the code in places, then the one that appears at the bottom starts to fail, which, as it were, hints, but still I can’t understand what the error is. I tried to experiment with the firewall pattern (by the way, I found detailed information for what it is responsible for, there are only general phrases in the documentation) - it didn’t give any effect, except for various bugs.

# app/config/security.yml
security:
    encoders:
        user_provider:
            class: UserBundle\Entity\User 
            algorithm:            plaintext
            ignore_case:          false
        admin_provider:
            class: AdminBundle\Entity\Admin
            algorithm:            plaintext
            ignore_case:          false
        Symfony\Component\Security\Core\User\User: plaintext

    role_hierarchy:
        ROLE_ADMIN: ROLE_USER
        ROLE_ROOT: ROLE_ADMIN

    providers:
        administration_provider:
            chain:
                providers: [root_provider, admin_provider]
        root_provider:
            memory:
                users:
                    [email protected]: 
                        password: 123
                        roles: 'ROLE_ROOT'
        admin_provider:
            entity:
                class: AdminBundle:Admin
                property: email
        user_provider:
            entity:
                class: UserBundle:User
                property: email

    firewalls:
        admin:
            pattern: ^/
            anonymous: ~
            form_login:
                login_path: admin
                check_path: admin
                default_target_path: dashboard
                provider: administration_provider
            logout:
                path:   admin_logout
                target: /admin
        user:
            pattern: ^/
            anonymous: ~
            form_login:
                login_path: login
                check_path: login
                default_target_path: profile
                provider: user_provider
            logout:
                path:   user_logout
                target: /

    access_control:
        - { path: ^/dashboard, role: ROLE_ADMIN }
        - { path: ^/profile, role: ROLE_USER }
        - { path: /.*, role: IS_AUTHENTICATED_ANONYMOUSLY }

I would be very grateful if you tell me where my mistake is

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2016-11-04
@thinkbrain2

pattern must be different. Symfony "takes the first one" according to the route pattern.
Or to unite yuzvery, or to divide on routes or the domain. For example, for / one user, for /foo another.
How does Symfony know which provider to use at the moment if there is only one firewall? No way.
Inherit two users from one abstract one and slip it into the framework. And authorize and drive through roles without being tied to a specific class. Crutch - but it will work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question