更新时间:2023-12-04 10:22:28
让我们假设我是Adam,其角色为'ROLE_ADMIN'.我无法登录到前端.
lets assume that I'm user Adam with role 'ROLE_ADMIN'. I can't login to frontend.
您应该简单地将此代码添加到您的控制器中:
You should simple add this code to your controllers:
if( $this->get('security.context')->isGranted('YOUR ROLE') )
return new Response('yea!');
因此,如果要保护BackendController并允许使用"ROLE_ADMIN"登录用户,则应添加以下代码:
So, If you want to secure BackendController and let to login users with 'ROLE_ADMIN' you should add this code:
if( $this->get('security.context')->isGranted('ROLE_ADMIN') )
return new Response('You are granted to see this site.');
此代码检查当前用户(我)是否具有角色ROLE_ADMIN.如果要检查用户是否具有'ROLE_ADMIN'并且没有'ROLE_USER',只需添加:
This code checks if current user (me) has role ROLE_ADMIN. If you want to check if user has 'ROLE_ADMIN' AND doesn't have 'ROLE_USER' just add:
$security = $this->get('security.context');
if( $security->isGranted('ROLE_ADMIN') && !$security->isGranted('ROLE_USER') )
return new Response('You are not granted to see this site.');