class UserRequestSubscriber

Same name in other branches
  1. 8.9.x core/modules/user/src/EventSubscriber/UserRequestSubscriber.php \Drupal\user\EventSubscriber\UserRequestSubscriber
  2. 10 core/modules/user/src/EventSubscriber/UserRequestSubscriber.php \Drupal\user\EventSubscriber\UserRequestSubscriber
  3. 11.x core/modules/user/src/EventSubscriber/UserRequestSubscriber.php \Drupal\user\EventSubscriber\UserRequestSubscriber

Updates the current user's last access time.

Hierarchy

  • class \Drupal\user\EventSubscriber\UserRequestSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of UserRequestSubscriber

1 string reference to 'UserRequestSubscriber'
user.services.yml in core/modules/user/user.services.yml
core/modules/user/user.services.yml
1 service uses UserRequestSubscriber
user_last_access_subscriber in core/modules/user/user.services.yml
Drupal\user\EventSubscriber\UserRequestSubscriber

File

core/modules/user/src/EventSubscriber/UserRequestSubscriber.php, line 15

Namespace

Drupal\user\EventSubscriber
View source
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.
     */
    public function __construct(AccountInterface $account, EntityTypeManagerInterface $entity_type_manager) {
        $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() && REQUEST_TIME - $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, REQUEST_TIME);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() {
        // Should go before other subscribers start to write their caches. Notably
        // before \Drupal\Core\EventSubscriber\KernelDestructionSubscriber to
        // prevent instantiation of destructed services.
        $events[KernelEvents::TERMINATE][] = [
            'onKernelTerminate',
            300,
        ];
        return $events;
    }

}

Members

Title Sort descending Modifiers Object type Summary
UserRequestSubscriber::$account protected property The current account.
UserRequestSubscriber::$entityTypeManager protected property The entity type manager service.
UserRequestSubscriber::getSubscribedEvents public static function
UserRequestSubscriber::onKernelTerminate public function Updates the current user's last access time.
UserRequestSubscriber::__construct public function Constructs a new UserRequestSubscriber.

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