symfony2: check isGranted for a route

后端 未结 2 1546
天涯浪人
天涯浪人 2020-12-11 06:51

supposed having certain route string like \"/path/index.html\" protected by firewall, how to chek whether current user is able to access it?

Thanks in advance!

2条回答
  •  孤街浪徒
    2020-12-11 07:22

    You could use security.access_control configuration option:

    securty:
        access_control:
            - { path: "^/path/index.html$", roles: ROLE_SOME_ROLE}
    

    Or simply check that manually from within your controller:

    class SomeController extends Controller {
        public function indexAction() {
            if (!$this->get('security.context')->isGranted(...)) {
                throw new AccessDeniedException(...);
            }
    
            ...
        }
    }
    

提交回复
热议问题