|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
/** |
|
4
|
|
|
* /src/EventSubscriber/AuthenticationSuccessSubscriber.php |
|
5
|
|
|
* |
|
6
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace App\EventSubscriber; |
|
10
|
|
|
|
|
11
|
|
|
use App\Doctrine\DBAL\Types\EnumLogLoginType; |
|
12
|
|
|
use App\Repository\UserRepository; |
|
13
|
|
|
use App\Utils\LoginLogger; |
|
14
|
|
|
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent; |
|
15
|
|
|
use Lexik\Bundle\JWTAuthenticationBundle\Events; |
|
16
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
17
|
|
|
use Throwable; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class AuthenticationSuccessSubscriber |
|
21
|
|
|
* |
|
22
|
|
|
* @package App\EventSubscriber |
|
23
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class AuthenticationSuccessSubscriber implements EventSubscriberInterface |
|
26
|
|
|
{ |
|
27
|
|
|
public function __construct( |
|
28
|
|
|
private LoginLogger $loginLogger, |
|
29
|
|
|
private UserRepository $userRepository, |
|
30
|
|
|
) { |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
* |
|
36
|
|
|
* @return array<string, string> |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function getSubscribedEvents(): array |
|
39
|
|
|
{ |
|
40
|
|
|
return [ |
|
41
|
|
|
AuthenticationSuccessEvent::class => 'onAuthenticationSuccess', |
|
42
|
|
|
Events::AUTHENTICATION_SUCCESS => 'onAuthenticationSuccess', |
|
43
|
|
|
]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Method to log user successfully login to database. |
|
48
|
|
|
* |
|
49
|
|
|
* This method is called when following event is broadcast |
|
50
|
|
|
* - lexik_jwt_authentication.on_authentication_success |
|
51
|
|
|
* |
|
52
|
|
|
* @throws Throwable |
|
53
|
|
|
*/ |
|
54
|
15 |
|
public function onAuthenticationSuccess(AuthenticationSuccessEvent $event): void |
|
55
|
|
|
{ |
|
56
|
15 |
|
$this->loginLogger |
|
57
|
15 |
|
->setUser($this->userRepository->loadUserByIdentifier($event->getUser()->getUserIdentifier(), true)) |
|
58
|
15 |
|
->process(EnumLogLoginType::TYPE_SUCCESS); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|