Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the TokenDeauthenticatedListener (see #5788)
Description ----------- Implements #475 Commits ------- 070250c Add TokenDeauthenticatedListener 3455097 Add missing test e455ea7 No PHP 8 features please a0e7278 CS 1f675a6 Merge branch '5.1' into feature/deauthenticated-event 00e46ad Merge branch '5.1' into feature/deauthenticated-event 4e3a8cc CS Co-authored-by: Leo Feyer <1192057+leofeyer@users.noreply.github.com>
- Loading branch information
Showing
4 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
core-bundle/src/EventListener/Security/TokenDeauthenticatedListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Contao. | ||
* | ||
* (c) Leo Feyer | ||
* | ||
* @license LGPL-3.0-or-later | ||
*/ | ||
|
||
namespace Contao\CoreBundle\EventListener\Security; | ||
|
||
use Contao\CoreBundle\Repository\RememberMeRepository; | ||
use Contao\User; | ||
use Symfony\Component\Security\Http\Event\TokenDeauthenticatedEvent; | ||
|
||
class TokenDeauthenticatedListener | ||
{ | ||
public function __construct(private RememberMeRepository $rememberMeRepository) | ||
{ | ||
} | ||
|
||
public function __invoke(TokenDeauthenticatedEvent $tokenDeauthenticatedEvent): void | ||
{ | ||
$user = $tokenDeauthenticatedEvent->getOriginalToken()->getUser(); | ||
|
||
if (!$user instanceof User) { | ||
return; | ||
} | ||
|
||
$this->rememberMeRepository->deleteByUserIdentifier($user->getUserIdentifier()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
core-bundle/tests/EventListener/Security/TokenDeauthenticatedListenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Contao. | ||
* | ||
* (c) Leo Feyer | ||
* | ||
* @license LGPL-3.0-or-later | ||
*/ | ||
|
||
namespace Contao\CoreBundle\Tests\EventListener\Security; | ||
|
||
use Contao\CoreBundle\EventListener\Security\TokenDeauthenticatedListener; | ||
use Contao\CoreBundle\Repository\RememberMeRepository; | ||
use Contao\TestCase\ContaoTestCase; | ||
use Contao\User; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Http\Event\TokenDeauthenticatedEvent; | ||
|
||
class TokenDeauthenticatedListenerTest extends ContaoTestCase | ||
{ | ||
public function testDeletesRelatedRememberMeRecords(): void | ||
{ | ||
$user = $this->createMock(User::class); | ||
$user | ||
->expects($this->exactly(2)) | ||
->method('getUserIdentifier') | ||
->willReturn('foobar') | ||
; | ||
|
||
$token = $this->createMock(TokenInterface::class); | ||
$token | ||
->expects($this->once()) | ||
->method('getUser') | ||
->willReturn($user) | ||
; | ||
|
||
$repository = $this->createMock(RememberMeRepository::class); | ||
$repository | ||
->expects($this->once()) | ||
->method('deleteByUserIdentifier') | ||
->with($user->getUserIdentifier()) | ||
; | ||
|
||
$event = new TokenDeauthenticatedEvent($token, $this->createMock(Request::class)); | ||
|
||
$listener = new TokenDeauthenticatedListener($repository); | ||
$listener($event); | ||
} | ||
|
||
public function testDoesNothingIfThereIsNoContaoUser(): void | ||
{ | ||
$token = $this->createMock(TokenInterface::class); | ||
$repository = $this->createMock(RememberMeRepository::class); | ||
|
||
$event = new TokenDeauthenticatedEvent($token, $this->createMock(Request::class)); | ||
|
||
$listener = new TokenDeauthenticatedListener($repository); | ||
$listener($event); | ||
|
||
$this->expectNotToPerformAssertions(); | ||
} | ||
} |