Skip to content

Commit 9f843b4

Browse files
committed
Use attributes from FrameworkExtraBundle
1 parent a3e0ad1 commit 9f843b4

File tree

3 files changed

+10
-15
lines changed

3 files changed

+10
-15
lines changed

src/Controller/Admin/BlogController.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,10 @@
3131
*
3232
* See http://knpbundles.com/keyword/admin
3333
*
34-
* @IsGranted("ROLE_ADMIN")
35-
*
3634
* @author Ryan Weaver <[email protected]>
3735
* @author Javier Eguiluz <[email protected]>
3836
*/
39-
#[Route('/admin/post')]
37+
#[Route('/admin/post'), IsGranted('ROLE_ADMIN')]
4038
class BlogController extends AbstractController
4139
{
4240
/**
@@ -125,10 +123,9 @@ public function show(Post $post): Response
125123

126124
/**
127125
* Displays a form to edit an existing Post entity.
128-
*
129-
* @IsGranted("edit", subject="post", message="Posts can only be edited by their authors.")
130126
*/
131127
#[Route('/{id<\d+>}/edit', methods: ['GET', 'POST'], name: 'admin_post_edit')]
128+
#[IsGranted('edit', subject: 'post', message: 'Posts can only be edited by their authors.')]
132129
public function edit(Request $request, Post $post): Response
133130
{
134131
$form = $this->createForm(PostType::class, $post);
@@ -150,10 +147,9 @@ public function edit(Request $request, Post $post): Response
150147

151148
/**
152149
* Deletes a Post entity.
153-
*
154-
* @IsGranted("delete", subject="post")
155150
*/
156151
#[Route('/{id}/delete', methods: ['POST'], name: 'admin_post_delete')]
152+
#[IsGranted('delete', subject: 'post')]
157153
public function delete(Request $request, Post $post): Response
158154
{
159155
if (!$this->isCsrfTokenValid('delete', $request->request->get('token'))) {

src/Controller/BlogController.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@
3636
class BlogController extends AbstractController
3737
{
3838
/**
39-
* @Cache(smaxage="10")
40-
*
4139
* NOTE: For standard formats, Symfony will also automatically choose the best
4240
* Content-Type header for the response.
41+
*
4342
* See https://symfony.com/doc/current/routing.html#special-parameters
4443
*/
4544
#[
4645
Route('/', defaults: ['page' => '1', '_format' => 'html'], methods: ['GET'], name: 'blog_index'),
4746
Route('/rss.xml', defaults: ['page' => '1', '_format' => 'xml'], methods: ['GET'], name: 'blog_rss'),
4847
Route('/page/{page<[1-9]\d*>}', defaults: ['_format' => 'html'], methods: ['GET'], name: 'blog_index_paginated'),
4948
]
49+
#[Cache(smaxage: 10)]
5050
public function index(Request $request, int $page, string $_format, PostRepository $posts, TagRepository $tags): Response
5151
{
5252
$tag = null;
@@ -68,6 +68,7 @@ public function index(Request $request, int $page, string $_format, PostReposito
6868
* NOTE: The $post controller argument is automatically injected by Symfony
6969
* after performing a database query looking for a Post with the 'slug'
7070
* value given in the route.
71+
*
7172
* See https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
7273
*/
7374
#[Route('/posts/{slug}', methods: ['GET'], name: 'blog_post')]
@@ -84,14 +85,14 @@ public function postShow(Post $post): Response
8485
}
8586

8687
/**
87-
* @IsGranted("IS_AUTHENTICATED_FULLY")
88-
* @ParamConverter("post", options={"mapping": {"postSlug": "slug"}})
89-
*
9088
* NOTE: The ParamConverter mapping is required because the route parameter
9189
* (postSlug) doesn't match any of the Doctrine entity properties (slug).
90+
*
9291
* See https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html#doctrine-converter
9392
*/
9493
#[Route('/comment/{postSlug}/new', methods: ['POST'], name: 'comment_new')]
94+
#[IsGranted('IS_AUTHENTICATED_FULLY')]
95+
#[ParamConverter('post', options: ['mapping' => ['postSlug' => 'slug']])]
9596
public function commentNew(Request $request, Post $post, EventDispatcherInterface $eventDispatcher): Response
9697
{
9798
$comment = new Comment();

src/Controller/UserController.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@
2323
/**
2424
* Controller used to manage current user.
2525
*
26-
* @IsGranted("ROLE_USER")
27-
*
2826
* @author Romain Monteil <[email protected]>
2927
*/
30-
#[Route('/profile')]
28+
#[Route('/profile'), IsGranted('ROLE_USER')]
3129
class UserController extends AbstractController
3230
{
3331
#[Route('/edit', methods: ['GET', 'POST'], name: 'user_edit')]

0 commit comments

Comments
 (0)