1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Smart\AuthenticationBundle\Security\EventSubscriber; |
4
|
|
|
|
5
|
|
|
use Smart\AuthenticationBundle\Security\LastLoginInterface; |
6
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
7
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
8
|
|
|
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; |
9
|
|
|
use Symfony\Component\Security\Http\Event\SwitchUserEvent; |
10
|
|
|
use Symfony\Component\Security\Http\SecurityEvents; |
11
|
|
|
use Smart\AuthenticationBundle\Security\Processor\LastLoginProcessor; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Mathieu Ducrot <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class SecuritySubscriber implements EventSubscriberInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var LastLoginProcessor |
20
|
|
|
*/ |
21
|
|
|
private $lastLoginProcessor; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param LastLoginProcessor $lastLoginProcessor |
25
|
|
|
*/ |
26
|
|
|
public function __construct(LastLoginProcessor $lastLoginProcessor) |
27
|
|
|
{ |
28
|
|
|
$this->lastLoginProcessor = $lastLoginProcessor; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @inheritDoc |
33
|
|
|
*/ |
34
|
|
|
public static function getSubscribedEvents() |
35
|
|
|
{ |
36
|
|
|
return [ |
37
|
|
|
SecurityEvents::SWITCH_USER => 'onSwitchUser', |
38
|
|
|
SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin', |
39
|
|
|
]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param InteractiveLoginEvent $event |
44
|
|
|
*/ |
45
|
|
|
public function onInteractiveLogin(InteractiveLoginEvent $event) |
46
|
|
|
{ |
47
|
|
|
$token = $event->getAuthenticationToken(); |
48
|
|
|
|
49
|
|
|
if (!$token instanceof TokenInterface) { |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$user = $token->getUser(); |
54
|
|
|
|
55
|
|
|
if (!$user instanceof LastLoginInterface) { |
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// Impersonnate user must not update last_login date |
60
|
|
|
if ($event->getRequest()->get('switch_user') !== null) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->lastLoginProcessor->process($user); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param SwitchUserEvent $event |
69
|
|
|
*/ |
70
|
|
|
public function onSwitchUser(SwitchUserEvent $event) |
71
|
|
|
{ |
72
|
|
|
$flashBag = $event->getRequest()->getSession()->getFlashBag(); |
73
|
|
|
|
74
|
|
|
if ($event->getRequest()->query->get('_switch_user') == '_exit') { |
75
|
|
|
$flashBag->add('sonata_flash_success', 'impersonate.exit_message'); |
76
|
|
|
} else { |
77
|
|
|
$flashBag->add('sonata_flash_success', 'impersonate.switch_message'); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|