Skip to content

Commit 340cee0

Browse files
committed
refactor: Use IsCsrfTokenValid Attribute
1 parent 5058252 commit 340cee0

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

src/Controller/Admin/BlogController.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Symfony\Component\Routing\Attribute\Route;
2626
use Symfony\Component\Routing\Requirement\Requirement;
2727
use Symfony\Component\Security\Http\Attribute\CurrentUser;
28+
use Symfony\Component\Security\Http\Attribute\IsCsrfTokenValid;
2829
use Symfony\Component\Security\Http\Attribute\IsGranted;
2930

3031
/**
@@ -159,15 +160,9 @@ public function edit(Request $request, Post $post, EntityManagerInterface $entit
159160
*/
160161
#[Route('/{id:post}/delete', name: 'admin_post_delete', requirements: ['id' => Requirement::POSITIVE_INT], methods: ['POST'])]
161162
#[IsGranted('delete', subject: 'post')]
162-
public function delete(Request $request, Post $post, EntityManagerInterface $entityManager): Response
163+
#[IsCsrfTokenValid('delete', tokenKey: 'token')]
164+
public function delete(Post $post, EntityManagerInterface $entityManager): Response
163165
{
164-
/** @var string|null $token */
165-
$token = $request->getPayload()->get('token');
166-
167-
if (!$this->isCsrfTokenValid('delete', $token)) {
168-
return $this->redirectToRoute('admin_post_index', [], Response::HTTP_SEE_OTHER);
169-
}
170-
171166
// Delete the tags associated with this blog post. This is done automatically
172167
// by Doctrine, except for SQLite (the database used in this application)
173168
// because foreign key support is not enabled by default in SQLite

0 commit comments

Comments
 (0)