From 1cb955e305cd629c62853e4d11cacdb7c8a11666 Mon Sep 17 00:00:00 2001 From: Antoine Makdessi Date: Tue, 14 Apr 2015 18:05:39 +0200 Subject: [PATCH 1/4] Usage of denyAccessUnlessGranted in the controller --- cookbook/security/voters_data_permission.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cookbook/security/voters_data_permission.rst b/cookbook/security/voters_data_permission.rst index 31bf0b3de99..2367241f446 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,7 @@ 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, 'Unauthorised access!'); return new Response('

'.$post->getName().'

'); } @@ -224,5 +221,8 @@ from the authorization checker is called. .. versionadded:: 2.6 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. + + ``denyAccessUnlessGranted()`` method was introduced in Symfony 2.6 as a shortcut. + This uses ``security.authorization_checker`` and throws ``AccessDeniedException`` if needed. It's that easy! From 791201aa9def4fe78060940c172d07e41ecc9dd5 Mon Sep 17 00:00:00 2001 From: Antoine Makdessi Date: Wed, 15 Apr 2015 09:43:42 +0200 Subject: [PATCH 2/4] minor fix --- cookbook/security/voters_data_permission.rst | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cookbook/security/voters_data_permission.rst b/cookbook/security/voters_data_permission.rst index 2367241f446..868c53c19b5 100644 --- a/cookbook/security/voters_data_permission.rst +++ b/cookbook/security/voters_data_permission.rst @@ -212,7 +212,14 @@ from the authorization checker is called. $post = ...; // keep in mind, this will call all registered security voters - $this->denyAccessUnlessGranted('view', $post, '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().'

'); } From 20a151ffcb2b5607b3f814d2e1fcefca84edd269 Mon Sep 17 00:00:00 2001 From: Antoine Makdessi Date: Mon, 27 Apr 2015 08:55:18 +0200 Subject: [PATCH 3/4] Fix versionadded directive --- cookbook/security/voters_data_permission.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cookbook/security/voters_data_permission.rst b/cookbook/security/voters_data_permission.rst index 868c53c19b5..a70ec99c901 100644 --- a/cookbook/security/voters_data_permission.rst +++ b/cookbook/security/voters_data_permission.rst @@ -228,7 +228,7 @@ from the authorization checker is called. .. versionadded:: 2.6 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 ``denyAccessUnlessGranted()`` method was introduced in Symfony 2.6 as a shortcut. This uses ``security.authorization_checker`` and throws ``AccessDeniedException`` if needed. From 4ed6695ba92541a4e3487038fdeab9eebe9311e6 Mon Sep 17 00:00:00 2001 From: Antoine Makdessi Date: Mon, 27 Apr 2015 10:56:36 +0200 Subject: [PATCH 4/4] Wording fix --- cookbook/security/voters_data_permission.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cookbook/security/voters_data_permission.rst b/cookbook/security/voters_data_permission.rst index a70ec99c901..3d46160ac02 100644 --- a/cookbook/security/voters_data_permission.rst +++ b/cookbook/security/voters_data_permission.rst @@ -228,8 +228,9 @@ from the authorization checker is called. .. versionadded:: 2.6 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 - ``denyAccessUnlessGranted()`` method was introduced in Symfony 2.6 as a shortcut. - This uses ``security.authorization_checker`` and throws ``AccessDeniedException`` if needed. + 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!