Symfony: how to find out the list of the different urls to which the connected user can access

I want to compose an index page presenting to the logged in user the list of resource urls which he can access according to the roles assigned to him

I found a solution by parsing the security.yaml file using the Yaml component and going through the security.acces_control section:

security:
    ......
    access_control:
        - {path: ^ / admin, roles: ROLE_ADMIN}
         - {path: ^ / profile, roles: ROLE_USER}
        - {path: ^ / login, roles: IS_AUTHENTICATED_ANONYMOUSLY}
        - {path: ^ / register, roles: IS_AUTHENTICATED_ANONYMOUSLY}
        - {path: ^ / resetting, roles: IS_AUTHENTICATED_ANONYMOUSLY}
        - {path: ^ / path1, roles: ROLE_USER}
        - {path: ^ / path2, roles: ROLE_CONTRIBUTOR}
        - .....
        - {path: ^ / pathn, roles: ROLE_CONTRIBUTOR}

Thus, if the logged in user only has the ROLE_USER role, he will only see /profile and /path1 on the index page.

But I know this way of doing that is bad practice.
Do you have a better solution with Symfony components to avoid parsing security.yaml?

I thought about using AccesMap but the ‘security.acces_map’ service is private and cannot be used.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *