简体   繁体   中英

Symfony access_control by role

I read this page about Symfony security !

But I can't use the access_control. I have two routes back and dashboard. Back is only for ROLE_SUPER_ADMIN and in order to go to the dashboard, the user must be authenticated.

Anonymous users can go everywhere and ROLE_USER can go to back (SUPER ADMIN only)

This is my file security.yml

security:
encoders:
    FOS\UserBundle\Model\UserInterface: sha512

role_hierarchy:
    ROLE_ADMIN:       ROLE_USER
    ROLE_SUPER_ADMIN: ROLE_ADMIN

providers:
    fos_userbundle:
        id: fos_user.user_provider.username

firewalls:
    main:
        pattern:    ^/
        form_login:
            provider: fos_userbundle
            csrf_provider: form.csrf_provider
        logout:       true
        anonymous:    true

access_control:
    - { path: ^/, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/dashboard, roles: ROLE_USER }
    - { path: ^/back, roles: ROLE_ADMIN }

Thanks for your help

Of course Anonymous users can go everywhere! Take a look to your first rule

access_control:
    - { path: ^/, role: IS_AUTHENTICATED_ANONYMOUSLY }

Even for /back path the rule seems to act properly because you hit the first access_control rule that grant the action

Faster solution : try to invert your rules order

Better solution : try to write better (less general) rules [if your app let you do that]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM