|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ProjetNormandie\UserBundle\EventSubscriber; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
|
6
|
|
|
use ProjetNormandie\UserBundle\Event\PasswordChangedEvent; |
|
7
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
8
|
|
|
use Symfony\Component\Mailer\MailerInterface; |
|
9
|
|
|
use Symfony\Component\Mime\Email; |
|
10
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
12
|
|
|
|
|
13
|
|
|
class PasswordChangeSubscriber implements EventSubscriberInterface |
|
14
|
|
|
{ |
|
15
|
|
|
public function __construct( |
|
16
|
|
|
private readonly LoggerInterface $logger, |
|
17
|
|
|
private readonly MailerInterface $mailer, |
|
18
|
|
|
private readonly TranslatorInterface $translator, |
|
19
|
|
|
private readonly RequestStack $requestStack |
|
20
|
|
|
) { |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @return array<string, array<int, array<int, int|string>>> |
|
25
|
|
|
*/ |
|
26
|
|
|
public static function getSubscribedEvents(): array |
|
27
|
|
|
{ |
|
28
|
|
|
return [ |
|
29
|
|
|
PasswordChangedEvent::class => [ |
|
30
|
|
|
['logPasswordChange', 100], |
|
31
|
|
|
['notifyUser', 90], |
|
32
|
|
|
['enforceSecurityMeasures', 80], |
|
33
|
|
|
], |
|
34
|
|
|
]; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function logPasswordChange(PasswordChangedEvent $event): void |
|
38
|
|
|
{ |
|
39
|
|
|
$user = $event->getUser(); |
|
40
|
|
|
$request = $this->requestStack->getCurrentRequest(); |
|
41
|
|
|
$ip = $request ? $request->getClientIp() : 'unknown'; |
|
42
|
|
|
|
|
43
|
|
|
$this->logger->info( |
|
44
|
|
|
sprintf( |
|
45
|
|
|
'##PASSWORD_CHANGED##[IP=%s/user=%s/id=%d]', |
|
46
|
|
|
$ip, |
|
47
|
|
|
$user->getUsername(), |
|
48
|
|
|
$user->getId() |
|
49
|
|
|
), |
|
50
|
|
|
[ |
|
51
|
|
|
'user_id' => $user->getId(), |
|
52
|
|
|
'username' => $user->getUsername(), |
|
53
|
|
|
'ip' => $ip, |
|
54
|
|
|
'action' => 'password_change', |
|
55
|
|
|
'timestamp' => new \DateTime() |
|
56
|
|
|
] |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function notifyUser(PasswordChangedEvent $event): void |
|
61
|
|
|
{ |
|
62
|
|
|
$user = $event->getUser(); |
|
63
|
|
|
|
|
64
|
|
|
$this->logger->debug('Sending password change notification', [ |
|
65
|
|
|
'user_id' => $user->getId(), |
|
66
|
|
|
'username' => $user->getUsername() |
|
67
|
|
|
]); |
|
68
|
|
|
|
|
69
|
|
|
try { |
|
70
|
|
|
$locale = $user->getLanguage(); |
|
71
|
|
|
|
|
72
|
|
|
$emailBody = $this->translator->trans( |
|
73
|
|
|
'password_change.notification', |
|
74
|
|
|
[ |
|
75
|
|
|
'%username%' => $user->getUsername(), |
|
76
|
|
|
'%date%' => (new \DateTime())->format('d/m/Y H:i:s') |
|
77
|
|
|
], |
|
78
|
|
|
'email', |
|
79
|
|
|
$locale |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
|
|
$email = (new Email()) |
|
83
|
|
|
->to($user->getEmail()) |
|
84
|
|
|
->subject($this->translator->trans('password_change.subject', [], 'email', $locale)) |
|
85
|
|
|
->text($emailBody) |
|
86
|
|
|
->html($emailBody); |
|
87
|
|
|
|
|
88
|
|
|
$this->mailer->send($email); |
|
89
|
|
|
|
|
90
|
|
|
$this->logger->info('Password change notification sent successfully', [ |
|
91
|
|
|
'user_id' => $user->getId(), |
|
92
|
|
|
'username' => $user->getUsername(), |
|
93
|
|
|
'email' => $user->getEmail() |
|
94
|
|
|
]); |
|
95
|
|
|
} catch (\Exception $e) { |
|
96
|
|
|
$this->logger->error('Error sending password change notification', [ |
|
97
|
|
|
'user_id' => $user->getId(), |
|
98
|
|
|
'username' => $user->getUsername(), |
|
99
|
|
|
'error' => $e->getMessage(), |
|
100
|
|
|
'trace' => $e->getTraceAsString() |
|
101
|
|
|
]); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
public function enforceSecurityMeasures(PasswordChangedEvent $event): void |
|
107
|
|
|
{ |
|
108
|
|
|
$user = $event->getUser(); |
|
109
|
|
|
|
|
110
|
|
|
$this->logger->debug('Applying security measures after password change', [ |
|
111
|
|
|
'user_id' => $user->getId(), |
|
112
|
|
|
'username' => $user->getUsername() |
|
113
|
|
|
]); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|