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('

'.$post->getName().'

'); } @@ -225,4 +229,8 @@ from the authorization checker is called. The ``security.authorization_checker`` service was introduced in Symfony 2.6. Prior to Symfony 2.6, you had to use the ``isGranted()`` method of the ``security.context`` service. +.. versionadded:: 2.6 + The ``denyAccessUnlessGranted()`` method was introduced in Symfony 2.6 as a shortcut. + It uses ``security.authorization_checker`` and throws an ``AccessDeniedException`` if needed. + It's that easy!