Skip to content

Commit d056c3b

Browse files
GromNaNjaviereguiluz
authored andcommitted
Use Security::logout() instead of redirecting to the logout url
1 parent c8bafc3 commit d056c3b

File tree

3 files changed

+8
-9
lines changed

3 files changed

+8
-9
lines changed

config/services.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,3 @@ services:
2929
- '../src/DependencyInjection/'
3030
- '../src/Entity/'
3131
- '../src/Kernel.php'
32-
33-
# this is needed because Symfony doesn't make the 'security.logout_url_generator' service autowirable
34-
Symfony\Component\Security\Http\Logout\LogoutUrlGenerator: '@security.logout_url_generator'

src/Controller/UserController.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
use App\Form\UserType;
1717
use Doctrine\ORM\EntityManagerInterface;
1818
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
19+
use Symfony\Bundle\SecurityBundle\Security;
1920
use Symfony\Component\HttpFoundation\Request;
2021
use Symfony\Component\HttpFoundation\Response;
2122
use Symfony\Component\Routing\Annotation\Route;
2223
use Symfony\Component\Security\Http\Attribute\CurrentUser;
2324
use Symfony\Component\Security\Http\Attribute\IsGranted;
24-
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
2525

2626
/**
2727
* Controller used to manage current user. The #[CurrentUser] attribute
@@ -62,15 +62,17 @@ public function changePassword(
6262
#[CurrentUser] User $user,
6363
Request $request,
6464
EntityManagerInterface $entityManager,
65-
LogoutUrlGenerator $logoutUrlGenerator,
65+
Security $security,
6666
): Response {
6767
$form = $this->createForm(ChangePasswordType::class, $user);
6868
$form->handleRequest($request);
6969

7070
if ($form->isSubmitted() && $form->isValid()) {
7171
$entityManager->flush();
7272

73-
return $this->redirect($logoutUrlGenerator->getLogoutPath());
73+
// The logout method has a protection against CSRF attacks, it's disabled here
74+
// because the form already has a CSRF token validated.
75+
return $security->logout(false);
7476
}
7577

7678
return $this->render('user/change_password.html.twig', [

tests/Controller/UserControllerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ public function testChangePassword(): void
103103
]);
104104

105105
$this->assertResponseRedirects();
106-
$this->assertStringStartsWith(
107-
'/logout',
108-
$client->getResponse()->headers->get('Location') ?? '',
106+
$this->assertResponseRedirects(
107+
'/',
108+
Response::HTTP_FOUND,
109109
'Changing password logout the user.'
110110
);
111111
}

0 commit comments

Comments
 (0)