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
|
|
|
->setMessage('User has been logged in!') |
34
|
|
|
->setIp($app['request']->getClientIp()) |
35
|
|
|
->setUserAgent($app['request']->headers->get('User-Agent')) |
36
|
|
|
; |
37
|
|
|
|
38
|
|
|
$app['orm.em']->persist($userActionEntity); |
39
|
|
|
$app['orm.em']->flush(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function onSwitchUser($event) |
43
|
|
|
{ |
44
|
|
|
$app = $this->app; |
45
|
|
|
|
46
|
|
|
$user = $app['security']->getToken()->getUser(); |
47
|
|
|
$targetUser = $event->getTargetUser(); |
48
|
|
|
|
49
|
|
|
if ($app['security']->isGranted('ROLE_PREVIOUS_ADMIN')) { |
50
|
|
|
$targetUser = $app['orm.em'] |
51
|
|
|
->find( |
52
|
|
|
'Application\Entity\UserEntity', |
53
|
|
|
$targetUser->getId() |
54
|
|
|
) |
55
|
|
|
; |
56
|
|
|
$userActionEntity = new UserActionEntity(); |
57
|
|
|
$userActionEntity |
58
|
|
|
->setUser($targetUser) |
59
|
|
|
->setKey('user.switch.back') |
60
|
|
|
->setMessage( |
61
|
|
|
'User has switched back to own user (from user with ID "'.$user->getId().'")!' |
62
|
|
|
) |
63
|
|
|
->setData(array( |
64
|
|
|
'user_id' => $targetUser->getId(), |
65
|
|
|
'from_user_id' => $user->getId(), |
66
|
|
|
)) |
67
|
|
|
->setIp($app['request']->getClientIp()) |
68
|
|
|
->setUserAgent($app['request']->headers->get('User-Agent')) |
69
|
|
|
; |
70
|
|
|
|
71
|
|
|
$app['orm.em']->persist($userActionEntity); |
72
|
|
|
$app['orm.em']->flush(); |
73
|
|
|
} else { |
74
|
|
|
$userActionEntity = new UserActionEntity(); |
75
|
|
|
$userActionEntity |
76
|
|
|
->setUser($user) |
77
|
|
|
->setKey('user.switch') |
78
|
|
|
->setMessage( |
79
|
|
|
'User has switched to user with ID "'.$targetUser->getId().'"!' |
80
|
|
|
) |
81
|
|
|
->setData(array( |
82
|
|
|
'user_id' => $user->getId(), |
83
|
|
|
'to_user_id' => $targetUser->getId(), |
84
|
|
|
)) |
85
|
|
|
->setIp($app['request']->getClientIp()) |
86
|
|
|
->setUserAgent($app['request']->headers->get('User-Agent')) |
87
|
|
|
; |
88
|
|
|
|
89
|
|
|
$app['orm.em']->persist($userActionEntity); |
90
|
|
|
$app['orm.em']->flush(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public static function getSubscribedEvents() |
95
|
|
|
{ |
96
|
|
|
return array( |
97
|
|
|
SecurityEvents::INTERACTIVE_LOGIN => array('onInteractiveLogin'), |
98
|
|
|
SecurityEvents::SWITCH_USER => array('onSwitchUser'), |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|