Skip to content

Commit 39796aa

Browse files
committed
minor #797 Use @IsGranted instead of @Security. (DevMDamien)
This PR was squashed before being merged into the master branch (closes #797). Discussion ---------- Use @IsGranted instead of @Security. As asked in issue #792, I replaced some `@Security("is_granted()")` annotations by `@IsGranted`. Commits ------- 166ffcf Use @IsGranted instead of @Security.
2 parents 3505d21 + 166ffcf commit 39796aa

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

src/Controller/Admin/BlogController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use App\Form\PostType;
1616
use App\Repository\PostRepository;
1717
use App\Utils\Slugger;
18+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
1819
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
1920
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
2021
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
@@ -118,7 +119,7 @@ public function new(Request $request): Response
118119
public function show(Post $post): Response
119120
{
120121
// This security check can also be performed
121-
// using an annotation: @Security("is_granted('show', post)")
122+
// using an annotation: @IsGranted("show", subject="post")
122123
$this->denyAccessUnlessGranted('show', $post, 'Posts can only be shown to their authors.');
123124

124125
return $this->render('admin/blog/show.html.twig', [
@@ -130,11 +131,10 @@ public function show(Post $post): Response
130131
* Displays a form to edit an existing Post entity.
131132
*
132133
* @Route("/{id}/edit", requirements={"id": "\d+"}, methods={"GET", "POST"}, name="admin_post_edit")
134+
* @IsGranted("edit", subject="post", message="Posts can only be edited by their authors.")
133135
*/
134136
public function edit(Request $request, Post $post): Response
135137
{
136-
$this->denyAccessUnlessGranted('edit', $post, 'Posts can only be edited by their authors.');
137-
138138
$form = $this->createForm(PostType::class, $post);
139139
$form->handleRequest($request);
140140

@@ -157,7 +157,7 @@ public function edit(Request $request, Post $post): Response
157157
* Deletes a Post entity.
158158
*
159159
* @Route("/{id}/delete", methods={"POST"}, name="admin_post_delete")
160-
* @Security("is_granted('delete', post)")
160+
* @IsGranted("delete", subject="post")
161161
*
162162
* The Security annotation value is an expression (if it evaluates to false,
163163
* the authorization mechanism will prevent the user accessing this resource).

src/Controller/BlogController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
use App\Form\CommentType;
1818
use App\Repository\PostRepository;
1919
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
20+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
2021
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
21-
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
2222
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
2323
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
2424
use Symfony\Component\EventDispatcher\GenericEvent;
@@ -78,7 +78,7 @@ public function postShow(Post $post): Response
7878

7979
/**
8080
* @Route("/comment/{postSlug}/new", methods={"POST"}, name="comment_new")
81-
* @Security("is_granted('IS_AUTHENTICATED_FULLY')")
81+
* @IsGranted("IS_AUTHENTICATED_FULLY")
8282
* @ParamConverter("post", options={"mapping": {"postSlug": "slug"}})
8383
*
8484
* NOTE: The ParamConverter mapping is required because the route parameter

0 commit comments

Comments
 (0)