@@ -203,7 +203,6 @@ from the authorization checker is called.
203
203
204
204
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
205
205
use Symfony\Component\HttpFoundation\Response;
206
- use Symfony\Component\Security\Core\Exception\AccessDeniedException;
207
206
208
207
class PostController extends Controller
209
208
{
@@ -213,9 +212,14 @@ from the authorization checker is called.
213
212
$post = ...;
214
213
215
214
// 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
+ // }
219
223
220
224
return new Response('<h1 >'.$post->getName().'</h1 >');
221
225
}
@@ -225,4 +229,8 @@ from the authorization checker is called.
225
229
The ``security.authorization_checker `` service was introduced in Symfony 2.6. Prior
226
230
to Symfony 2.6, you had to use the ``isGranted() `` method of the ``security.context `` service.
227
231
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
+
228
236
It's that easy!
0 commit comments