Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\user\EventSubscriber;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Entity\EntityManagerInterface;
|
Chris@0
|
6 use Drupal\Core\Session\AccountInterface;
|
Chris@0
|
7 use Drupal\Core\Site\Settings;
|
Chris@0
|
8 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
Chris@0
|
9 use Symfony\Component\HttpKernel\Event\PostResponseEvent;
|
Chris@0
|
10 use Symfony\Component\HttpKernel\KernelEvents;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Updates the current user's last access time.
|
Chris@0
|
14 */
|
Chris@0
|
15 class UserRequestSubscriber implements EventSubscriberInterface {
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * The current account.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @var \Drupal\Core\Session\AccountInterface
|
Chris@0
|
21 */
|
Chris@0
|
22 protected $account;
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * The entity manager.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @var \Drupal\Core\Entity\EntityManagerInterface
|
Chris@0
|
28 */
|
Chris@0
|
29 protected $entityManager;
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * Constructs a new UserRequestSubscriber.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @param \Drupal\Core\Session\AccountInterface $account
|
Chris@0
|
35 * The current user.
|
Chris@0
|
36 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
|
Chris@0
|
37 * The entity manager.
|
Chris@0
|
38 */
|
Chris@0
|
39 public function __construct(AccountInterface $account, EntityManagerInterface $entity_manager) {
|
Chris@0
|
40 $this->account = $account;
|
Chris@0
|
41 $this->entityManager = $entity_manager;
|
Chris@0
|
42 }
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@0
|
45 * Updates the current user's last access time.
|
Chris@0
|
46 *
|
Chris@0
|
47 * @param \Symfony\Component\HttpKernel\Event\PostResponseEvent $event
|
Chris@0
|
48 * The event to process.
|
Chris@0
|
49 */
|
Chris@0
|
50 public function onKernelTerminate(PostResponseEvent $event) {
|
Chris@0
|
51 if ($this->account->isAuthenticated() && REQUEST_TIME - $this->account->getLastAccessedTime() > Settings::get('session_write_interval', 180)) {
|
Chris@0
|
52 // Do that no more than once per 180 seconds.
|
Chris@0
|
53 /** @var \Drupal\user\UserStorageInterface $storage */
|
Chris@0
|
54 $storage = $this->entityManager->getStorage('user');
|
Chris@0
|
55 $storage->updateLastAccessTimestamp($this->account, REQUEST_TIME);
|
Chris@0
|
56 }
|
Chris@0
|
57 }
|
Chris@0
|
58
|
Chris@0
|
59 /**
|
Chris@0
|
60 * {@inheritdoc}
|
Chris@0
|
61 */
|
Chris@0
|
62 public static function getSubscribedEvents() {
|
Chris@0
|
63 // Should go before other subscribers start to write their caches. Notably
|
Chris@0
|
64 // before \Drupal\Core\EventSubscriber\KernelDestructionSubscriber to
|
Chris@0
|
65 // prevent instantiation of destructed services.
|
Chris@0
|
66 $events[KernelEvents::TERMINATE][] = ['onKernelTerminate', 300];
|
Chris@0
|
67 return $events;
|
Chris@0
|
68 }
|
Chris@0
|
69
|
Chris@0
|
70 }
|