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