1
votes

following setup:

I have two different login sections for two different users.

First user is a normal user (Entity: AppBundle:User) the second one is the admin (Entity: AppBundle:Admin).

I have two different login paths, providers, firewalls and encoders. I can login and logout either as a user or as admin with no problems .

The problem:

If the admin has logged in I need him to stay in the admin domain and not be able to access the user login. Same for the normal user, he must not be able to login as admin if he is already logged in as a user.

So the problem is, that if the admin is logged in he still can switch to the user login domain and login as user, the session will than contain both USER AND ADMIN.

security.yml:

providers:
    admin_db_provider:
        name: admin_provider
        entity:
            class: AppBundle:Admin
        ...
    user_db_provider:
        name: user_provider
        entity:
            class: AppBundle:User
        ...
firewalls:
    admin_secured_domain:
        pattern: ^/admin
        anonymous: ~
        provider: admin_provider
        form_login:
            login_path: admin.authentication
            check_path: admin.authentication
            username_parameter: userName
            pasword_parameter: password
        ...
    user_secured_domain:
        pattern: ^/user
        anonymous: ~
        provider: user_provider
        form_login:
            login_path: user.authentication
            check_path: user.authentication
            username_parameter: userName
            pasword_parameter: password
        ...
access_control:
    - { path: ^/user/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
encoders:
    ...

I assume this happens because I use two different providers for the login check.

When I am logged in as a user and access the admin login I can see in the Symfony profiler that the user switches from "user" to "anon." and same is for the admin, it switches from "admin" to "anon." if I am logged in as admin and access the user login.

I just need to redirect them back to their domain, but I am not able to check the role at this point.

1

1 Answers

2
votes

What you are looking for is the 'security context'.

See this similar question below: Session lost during impersonation

If you want your users from different firewalls to share the same security context, then you must define the same context for the different firewalls:

# app/config/security.yml
security:
    # ...
    firewalls:
        admin_secured_domain:
            # ...
            context: my_context
        user_secured_domain:
            # ...
            context: my_context

See the symfony documentation about the security context: http://symfony.com/doc/current/reference/configuration/security.html#reference-security-firewall-context

Most applications will only need one firewall. But if your application does use multiple firewalls, you'll notice that if you're authenticated in one firewall, you're not automatically authenticated in another. In other words, the systems don't share a common "context": each firewall acts like a separate security system.