Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage of denyAccessUnlessGranted in the controller #5178

Closed
wants to merge 4 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions cookbook/security/voters_data_permission.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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('<h1>'.$post->getName().'</h1>');
}
Expand All @@ -224,5 +228,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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember having display issues on symfony.com with blank lines in versionadded directives. Can you please start a new one (repeat line 228 before this line)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would reword this a bit: "The denyAccessUnlessGranted()method [...]"

This uses ``security.authorization_checker`` and throws ``AccessDeniedException`` if needed.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It uses security.authorization_checker and throws an AccessDeniedException if needed.


It's that easy!