comparison core/modules/user/src/EventSubscriber/UserRequestSubscriber.php @ 0:4c8ae668cc8c

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