UserRequestSubscriber.php

Same filename in other branches
  1. 9 core/modules/user/src/EventSubscriber/UserRequestSubscriber.php
  2. 8.9.x core/modules/user/src/EventSubscriber/UserRequestSubscriber.php
  3. 10 core/modules/user/src/EventSubscriber/UserRequestSubscriber.php

Namespace

Drupal\user\EventSubscriber

File

core/modules/user/src/EventSubscriber/UserRequestSubscriber.php

View source
<?php

namespace Drupal\user\EventSubscriber;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Site\Settings;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\TerminateEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
 * Updates the current user's last access time.
 */
class UserRequestSubscriber implements EventSubscriberInterface {
    
    /**
     * The current account.
     *
     * @var \Drupal\Core\Session\AccountInterface
     */
    protected $account;
    
    /**
     * The entity type manager service.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * Constructs a new UserRequestSubscriber.
     *
     * @param \Drupal\Core\Session\AccountInterface $account
     *   The current user.
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
     *   The entity type manager service.
     * @param \Drupal\Component\Datetime\TimeInterface $time
     *   The time service.
     */
    public function __construct(AccountInterface $account, EntityTypeManagerInterface $entity_type_manager, TimeInterface $time) {
        $this->account = $account;
        $this->entityTypeManager = $entity_type_manager;
    }
    
    /**
     * Updates the current user's last access time.
     *
     * @param \Symfony\Component\HttpKernel\Event\TerminateEvent $event
     *   The event to process.
     */
    public function onKernelTerminate(TerminateEvent $event) {
        if ($this->account
            ->isAuthenticated() && $this->time
            ->getRequestTime() - $this->account
            ->getLastAccessedTime() > Settings::get('session_write_interval', 180)) {
            // Do that no more than once per 180 seconds.
            
            /** @var \Drupal\user\UserStorageInterface $storage */
            $storage = $this->entityTypeManager
                ->getStorage('user');
            $storage->updateLastAccessTimestamp($this->account, $this->time
                ->getRequestTime());
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() : array {
        // Should go before other subscribers start to write their caches.
        $events[KernelEvents::TERMINATE][] = [
            'onKernelTerminate',
            300,
        ];
        return $events;
    }

}

Classes

Title Deprecated Summary
UserRequestSubscriber Updates the current user's last access time.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.