user2094540
user2094540

Reputation:

FosUserBundle : access control with 2 or more roles?

I'm using FosUserBundle for my Symfony2 project. I've added a rule for the custom ROLE_VALIDE to restrict the paths like /user. It works for the users having this role.

The problem is that I also want the admins to be able to access this path. I've tested with both roles ROLE_ADMIN and ROLE_ADMIN + ROLE_VALIDE but I have the 403 error page.

Is there a way to add more than one role in the access_control ?

access_control:
    - { path: ^/admin, role: ROLE_ADMIN }
    - { path: ^/user, role: ROLE_VALIDE }

Upvotes: 1

Views: 1465

Answers (1)

BENARD Patrick
BENARD Patrick

Reputation: 30975

What about role hierarchy in your security.yml ?

doc : http://symfony.com/doc/current/book/security.html#hierarchical-roles

role_hierarchy:
    ROLE_USER:        [ROLE_USER]
    ROLE_VALIDE:      [ROLE_USER, ROLE_VALIDE]
    ROLE_ADMIN:       [ROLE_USER, ROLE_VALIDE, ROLE_ADMIN]

With this, if route is waiting for ROLE_VALIDE, ROLE_ADMIN is ok because is has ROLE_VALIDE inside it.

Upvotes: 1

Related Questions