@@ -203,7 +203,6 @@ from the authorization checker is called.
203203
204204 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
205205 use Symfony\Component\HttpFoundation\Response;
206- use Symfony\Component\Security\Core\Exception\AccessDeniedException;
207206
208207 class PostController extends Controller
209208 {
@@ -213,9 +212,14 @@ from the authorization checker is called.
213212 $post = ...;
214213
215214 // keep in mind, this will call all registered security voters
216- if (false === $this->get('security.authorization_checker')->isGranted('view', $post)) {
217- throw new AccessDeniedException('Unauthorised access!');
218- }
215+ $this->denyAccessUnlessGranted('view', $post, 'Unauthorized access!');
216+
217+ // the equivalent code without using the denyAccessUnlessGranted() shortcut
218+ // use Symfony\Component\Security\Core\Exception\AccessDeniedException;
219+ //
220+ // if (false === $this->get('security.authorization_checker')->isGranted('view', $post)) {
221+ // throw new AccessDeniedException('Unauthorized access!');
222+ // }
219223
220224 return new Response('<h1 >'.$post->getName().'</h1 >');
221225 }
@@ -225,4 +229,8 @@ from the authorization checker is called.
225229 The ``security.authorization_checker `` service was introduced in Symfony 2.6. Prior
226230 to Symfony 2.6, you had to use the ``isGranted() `` method of the ``security.context `` service.
227231
232+ .. versionadded :: 2.6
233+ The ``denyAccessUnlessGranted() `` method was introduced in Symfony 2.6 as a shortcut.
234+ It uses ``security.authorization_checker `` and throws an ``AccessDeniedException `` if needed.
235+
228236It's that easy!
0 commit comments