|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\EventSubscriber; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\User; |
|
6
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
9
|
|
|
use Symfony\Component\HttpKernel\Event\RequestEvent; |
|
10
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
11
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
12
|
|
|
|
|
13
|
|
|
class MustChangePasswordRedirectEventSubscriber implements EventSubscriberInterface { |
|
14
|
|
|
|
|
15
|
|
|
private const RedirectRoute = 'profile'; |
|
16
|
|
|
|
|
17
|
|
|
private $urlGenerator; |
|
18
|
|
|
private $tokenStorage; |
|
19
|
|
|
|
|
20
|
9 |
|
public function __construct(UrlGeneratorInterface $urlGenerator, TokenStorageInterface $tokenStorage) { |
|
21
|
9 |
|
$this->urlGenerator = $urlGenerator; |
|
22
|
9 |
|
$this->tokenStorage = $tokenStorage; |
|
23
|
9 |
|
} |
|
24
|
|
|
|
|
25
|
7 |
|
public function onRequest(RequestEvent $event) { |
|
26
|
7 |
|
if($event->isMasterRequest() !== true) { |
|
27
|
|
|
return; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
7 |
|
$token = $this->tokenStorage->getToken(); |
|
31
|
|
|
|
|
32
|
7 |
|
if($token === null) { |
|
33
|
|
|
return; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
7 |
|
$user = $token->getUser(); |
|
37
|
|
|
|
|
38
|
7 |
|
if(!$user instanceof User) { |
|
39
|
7 |
|
return; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
5 |
|
if($user->isMustChangePassword() === false) { |
|
43
|
5 |
|
return; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
$currentRoute = $event->getRequest()->attributes->get('_route'); |
|
47
|
|
|
|
|
48
|
1 |
|
if($currentRoute === static::RedirectRoute) { |
|
49
|
|
|
// Do not create redirect loops |
|
50
|
1 |
|
return; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
$response = new RedirectResponse( |
|
54
|
1 |
|
$this->urlGenerator->generate(static::RedirectRoute), |
|
55
|
1 |
|
Response::HTTP_FOUND |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
1 |
|
$event->setResponse($response); |
|
59
|
1 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @inheritDoc |
|
63
|
|
|
*/ |
|
64
|
|
|
public static function getSubscribedEvents() { |
|
65
|
|
|
return [ |
|
66
|
|
|
RequestEvent::class => 'onRequest' |
|
67
|
|
|
]; |
|
68
|
|
|
} |
|
69
|
|
|
} |