-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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('<h1>'.$post->getName().'</h1>'); | ||
} | ||
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would reword this a bit: "The |
||
This uses ``security.authorization_checker`` and throws ``AccessDeniedException`` if needed. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It uses |
||
|
||
It's that easy! |
There was a problem hiding this comment.
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)?