Of course, you can simply add any roles starting with ROLE_SOMEROLE. The security.yml file has two main parts: 1. Restrict access 2. Which member can access
but. access_control: which restricts the pattern and indicates the role that can be accessed. b. role_hierarchy: here is the hierarchical structure of the role, for the example below, the Admin user (ROLE_ADMIN) has the roles ROLE_USER, ROLE_NEWS_AUTHOR. This way, he can access all USER and NEWS_AUTHOR pages. Whatever hierarchy you could give.
access_control: - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }/login any one can access this pattern - { path: ^/admin/, roles: ROLE_ADMIN }//block all pattern /admin/anything* - { path: ^/news/, roles: ROLE_NEWS_AUTHOR } //block all pattern /news/anything* role_hierarchy: ROLE_ADMIN: [ROLE_USER,ROLE_NEWS_AUTHOR]
In your controller, you can check the roles,
if(TRUE ===$this->get('security.context')->isGranted('ROLE_ADMIN') ) { // do something related to ADMIN } else if(TRUE ===$this->get('security.context')->isGranted('ROLE_NEWS_AUTHOR') ) { // do something related to News Editor }
Hope this helps you. HAppy.
Asish ap
source share