diff --git a/cookbook/security/voters_data_permission.rst b/cookbook/security/voters_data_permission.rst index 31bf0b3de99..3d46160ac02 100644 --- a/cookbook/security/voters_data_permission.rst +++ b/cookbook/security/voters_data_permission.rst @@ -203,7 +203,6 @@ from the authorization checker is called. use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Response; - use Symfony\Component\Security\Core\Exception\AccessDeniedException; class PostController extends Controller { @@ -213,9 +212,14 @@ from the authorization checker is called. $post = ...; // keep in mind, this will call all registered security voters - if (false === $this->get('security.authorization_checker')->isGranted('view', $post)) { - throw new AccessDeniedException('Unauthorised access!'); - } + $this->denyAccessUnlessGranted('view', $post, 'Unauthorized access!'); + + // the equivalent code without using the denyAccessUnlessGranted() shortcut + // use Symfony\Component\Security\Core\Exception\AccessDeniedException; + // + // if (false === $this->get('security.authorization_checker')->isGranted('view', $post)) { + // throw new AccessDeniedException('Unauthorized access!'); + // } return new Response('