|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* (c) FSi sp. z o.o. <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace FSi\Bundle\AdminSecurityBundle\EventListener; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
15
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
|
16
|
|
|
use FSi\Bundle\AdminSecurityBundle\Event\ActivationEvent; |
|
17
|
|
|
use FSi\Bundle\AdminSecurityBundle\Event\AdminSecurityEvents; |
|
18
|
|
|
use FSi\Bundle\AdminSecurityBundle\Event\ChangePasswordEvent; |
|
19
|
|
|
use FSi\Bundle\AdminSecurityBundle\Event\ResetPasswordRequestEvent; |
|
20
|
|
|
use FSi\Bundle\AdminSecurityBundle\Event\UserEvent; |
|
21
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
22
|
|
|
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; |
|
23
|
|
|
use Symfony\Component\Security\Http\SecurityEvents; |
|
24
|
|
|
|
|
25
|
|
|
class PersistDoctrineUserListener implements EventSubscriberInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var ManagerRegistry |
|
29
|
|
|
*/ |
|
30
|
|
|
private $registry; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct(ManagerRegistry $registry) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->registry = $registry; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public static function getSubscribedEvents(): array |
|
38
|
|
|
{ |
|
39
|
|
|
return [ |
|
40
|
|
|
AdminSecurityEvents::CHANGE_PASSWORD => 'onChangePassword', |
|
41
|
|
|
AdminSecurityEvents::RESET_PASSWORD_REQUEST => 'onResetPasswordRequest', |
|
42
|
|
|
AdminSecurityEvents::ACTIVATION => 'onActivation', |
|
43
|
|
|
AdminSecurityEvents::RESEND_ACTIVATION_TOKEN => 'onActivationResend', |
|
44
|
|
|
AdminSecurityEvents::DEACTIVATION => 'onDeactivation', |
|
45
|
|
|
AdminSecurityEvents::USER_CREATED => 'onUserCreated', |
|
46
|
|
|
AdminSecurityEvents::PROMOTE_USER => 'onPromoteUser', |
|
47
|
|
|
AdminSecurityEvents::DEMOTE_USER => 'onDemoteUser', |
|
48
|
|
|
SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin' |
|
49
|
|
|
]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function onActivation(ActivationEvent $event): void |
|
53
|
|
|
{ |
|
54
|
|
|
$this->flushUserObjectManager($event->getUser()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function onActivationResend(ActivationEvent $event): void |
|
58
|
|
|
{ |
|
59
|
|
|
$this->flushUserObjectManager($event->getUser()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function onDeactivation(ActivationEvent $event): void |
|
63
|
|
|
{ |
|
64
|
|
|
$this->flushUserObjectManager($event->getUser()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function onChangePassword(ChangePasswordEvent $event): void |
|
68
|
|
|
{ |
|
69
|
|
|
$this->flushUserObjectManager($event->getUser()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function onResetPasswordRequest(ResetPasswordRequestEvent $event): void |
|
73
|
|
|
{ |
|
74
|
|
|
$this->flushUserObjectManager($event->getUser()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function onUserCreated(UserEvent $event): void |
|
78
|
|
|
{ |
|
79
|
|
|
$this->flushUserObjectManager($event->getUser()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function onPromoteUser(UserEvent $event): void |
|
83
|
|
|
{ |
|
84
|
|
|
$this->flushUserObjectManager($event->getUser()); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function onDemoteUser(UserEvent $event): void |
|
88
|
|
|
{ |
|
89
|
|
|
$this->flushUserObjectManager($event->getUser()); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function onInteractiveLogin(InteractiveLoginEvent $event): void |
|
93
|
|
|
{ |
|
94
|
|
|
$this->flushUserObjectManager($event->getAuthenticationToken()->getUser()); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param object $user |
|
99
|
|
|
*/ |
|
100
|
|
|
private function flushUserObjectManager($user): void |
|
101
|
|
|
{ |
|
102
|
|
|
$objectManager = $this->registry->getManagerForClass(get_class($user)); |
|
103
|
|
|
if (false === $objectManager instanceof ObjectManager) { |
|
104
|
|
|
return; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$objectManager->persist($user); |
|
108
|
|
|
$objectManager->flush(); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|