|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ma27\ApiKeyAuthenticationBundle\EventListener; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
6
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Event\AbstractUserEvent; |
|
7
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Event\OnAuthenticationEvent; |
|
8
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Event\OnFirewallAuthenticationEvent; |
|
9
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Ma27ApiKeyAuthenticationEvents; |
|
10
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Service\Mapping\ClassMetadata; |
|
11
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* UpdateLastActionFieldListener. |
|
16
|
|
|
*/ |
|
17
|
|
|
class UpdateLastActionFieldListener implements EventSubscriberInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var ObjectManager |
|
21
|
|
|
*/ |
|
22
|
|
|
private $om; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var ClassMetadata |
|
26
|
|
|
*/ |
|
27
|
|
|
private $classMetadata; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var RequestStack |
|
31
|
|
|
*/ |
|
32
|
|
|
private $requestStack; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param ObjectManager $om |
|
38
|
|
|
* @param ClassMetadata $metadata |
|
39
|
|
|
* @param RequestStack $requestStack |
|
40
|
|
|
*/ |
|
41
|
10 |
|
public function __construct(ObjectManager $om, ClassMetadata $metadata, RequestStack $requestStack) |
|
42
|
|
|
{ |
|
43
|
10 |
|
$this->om = $om; |
|
44
|
10 |
|
$this->classMetadata = $metadata; |
|
45
|
10 |
|
$this->requestStack = $requestStack; |
|
46
|
10 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
16 |
|
public static function getSubscribedEvents() |
|
52
|
|
|
{ |
|
53
|
|
|
return array( |
|
54
|
16 |
|
Ma27ApiKeyAuthenticationEvents::AUTHENTICATION => array('onAuthentication', -20), // late event since custom listeners should be triggered earlier |
|
55
|
|
|
Ma27ApiKeyAuthenticationEvents::FIREWALL_LOGIN => 'onFirewallLogin', |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Modifies the last action property on authentication. |
|
61
|
|
|
* |
|
62
|
|
|
* @param OnAuthenticationEvent $event |
|
63
|
|
|
*/ |
|
64
|
9 |
|
public function onAuthentication(OnAuthenticationEvent $event) |
|
65
|
|
|
{ |
|
66
|
9 |
|
$this->doModify($event); |
|
67
|
9 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Modifies the last action property on firewall authentication. |
|
71
|
|
|
* |
|
72
|
|
|
* @param OnFirewallAuthenticationEvent $event |
|
73
|
|
|
*/ |
|
74
|
7 |
|
public function onFirewallLogin(OnFirewallAuthenticationEvent $event) |
|
75
|
|
|
{ |
|
76
|
7 |
|
$this->doModify($event); |
|
77
|
|
|
|
|
78
|
7 |
|
$this->om->persist($event->getUser()); |
|
79
|
7 |
|
$this->om->flush(); |
|
80
|
7 |
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Modifies the last action property of a user object. |
|
84
|
|
|
* |
|
85
|
|
|
* @param AbstractUserEvent $event |
|
86
|
|
|
*/ |
|
87
|
10 |
|
private function doModify(AbstractUserEvent $event) |
|
88
|
|
|
{ |
|
89
|
10 |
|
$this->classMetadata->modifyProperty( |
|
90
|
10 |
|
$event->getUser(), |
|
91
|
10 |
|
new \DateTime(sprintf('@%s', $this->requestStack->getMasterRequest()->server->get('REQUEST_TIME'))), |
|
92
|
10 |
|
ClassMetadata::LAST_ACTION_PROPERTY |
|
93
|
|
|
); |
|
94
|
10 |
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|