|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Application\EventListener; |
|
4
|
|
|
|
|
5
|
|
|
use Application\Entity\UserActionEntity; |
|
6
|
|
|
use Symfony\Component\Security\Http\SecurityEvents; |
|
7
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
8
|
|
|
use Silex\Application; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author Borut Balažek <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class SecurityEventsListener implements EventSubscriberInterface |
|
14
|
|
|
{ |
|
15
|
|
|
protected $app; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(Application $app) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->app = $app; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function onInteractiveLogin($event) |
|
23
|
|
|
{ |
|
24
|
|
|
$app = $this->app; |
|
25
|
|
|
|
|
26
|
|
|
$token = $event->getAuthenticationToken(); |
|
27
|
|
|
$user = $token->getUser(); |
|
28
|
|
|
|
|
29
|
|
|
$userActionEntity = new UserActionEntity(); |
|
30
|
|
|
$userActionEntity |
|
31
|
|
|
->setUser($user) |
|
32
|
|
|
->setKey('user.login') |
|
33
|
|
|
->setType('event') |
|
34
|
|
|
->setMessage('User has been logged in!') |
|
35
|
|
|
->setIp($app['request']->getClientIp()) |
|
36
|
|
|
->setUserAgent($app['request']->headers->get('User-Agent')) |
|
37
|
|
|
; |
|
38
|
|
|
|
|
39
|
|
|
$app['orm.em']->persist($userActionEntity); |
|
40
|
|
|
$app['orm.em']->flush(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function onSwitchUser($event) |
|
44
|
|
|
{ |
|
45
|
|
|
$app = $this->app; |
|
46
|
|
|
|
|
47
|
|
|
$user = $app['security']->getToken()->getUser(); |
|
48
|
|
|
$targetUser = $event->getTargetUser(); |
|
49
|
|
|
|
|
50
|
|
|
if ($app['security']->isGranted('ROLE_PREVIOUS_ADMIN')) { |
|
51
|
|
|
$targetUser = $app['orm.em'] |
|
52
|
|
|
->find( |
|
53
|
|
|
'Application\Entity\UserEntity', |
|
54
|
|
|
$targetUser->getId() |
|
55
|
|
|
) |
|
56
|
|
|
; |
|
57
|
|
|
$userActionEntity = new UserActionEntity(); |
|
58
|
|
|
$userActionEntity |
|
59
|
|
|
->setUser($targetUser) |
|
60
|
|
|
->setKey('user.switch.back') |
|
61
|
|
|
->setType('event') |
|
62
|
|
|
->setMessage( |
|
63
|
|
|
'User has switched back to own user (from user with ID "'.$user->getId().'")!' |
|
64
|
|
|
) |
|
65
|
|
|
->setData(array( |
|
66
|
|
|
'user_id' => $targetUser->getId(), |
|
67
|
|
|
'from_user_id' => $user->getId(), |
|
68
|
|
|
)) |
|
69
|
|
|
->setIp($app['request']->getClientIp()) |
|
70
|
|
|
->setUserAgent($app['request']->headers->get('User-Agent')) |
|
71
|
|
|
; |
|
72
|
|
|
|
|
73
|
|
|
$app['orm.em']->persist($userActionEntity); |
|
74
|
|
|
$app['orm.em']->flush(); |
|
75
|
|
|
} else { |
|
76
|
|
|
$userActionEntity = new UserActionEntity(); |
|
77
|
|
|
$userActionEntity |
|
78
|
|
|
->setUser($user) |
|
79
|
|
|
->setKey('user.switch') |
|
80
|
|
|
->setType('event') |
|
81
|
|
|
->setMessage( |
|
82
|
|
|
'User has switched to user with ID "'.$targetUser->getId().'"!' |
|
83
|
|
|
) |
|
84
|
|
|
->setData(array( |
|
85
|
|
|
'user_id' => $user->getId(), |
|
86
|
|
|
'to_user_id' => $targetUser->getId(), |
|
87
|
|
|
)) |
|
88
|
|
|
->setIp($app['request']->getClientIp()) |
|
89
|
|
|
->setUserAgent($app['request']->headers->get('User-Agent')) |
|
90
|
|
|
; |
|
91
|
|
|
|
|
92
|
|
|
$app['orm.em']->persist($userActionEntity); |
|
93
|
|
|
$app['orm.em']->flush(); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public static function getSubscribedEvents() |
|
98
|
|
|
{ |
|
99
|
|
|
return array( |
|
100
|
|
|
SecurityEvents::INTERACTIVE_LOGIN => array('onInteractiveLogin'), |
|
101
|
|
|
SecurityEvents::SWITCH_USER => array('onSwitchUser'), |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|