UserStorage.php 3.72 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\user\UserStorage.
6 7 8 9
 */

namespace Drupal\user;

10
use Drupal\Core\Database\Connection;
11
use Drupal\Core\Cache\CacheBackendInterface;
12
use Drupal\Core\Entity\EntityInterface;
13
use Drupal\Core\Entity\EntityManagerInterface;
14
use Drupal\Core\Entity\EntityTypeInterface;
15
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
16
use Drupal\Core\Password\PasswordInterface;
17
use Drupal\Core\Session\AccountInterface;
18
use Drupal\Core\Language\LanguageManagerInterface;
19
use Symfony\Component\DependencyInjection\ContainerInterface;
20 21 22 23

/**
 * Controller class for users.
 *
24
 * This extends the Drupal\Core\Entity\Sql\SqlContentEntityStorage class,
25
 * adding required special handling for user objects.
26
 */
27
class UserStorage extends SqlContentEntityStorage implements UserStorageInterface {
28

29 30 31 32 33 34 35 36
  /**
   * Provides the password hashing service object.
   *
   * @var \Drupal\Core\Password\PasswordInterface
   */
  protected $password;

  /**
37
   * Constructs a new UserStorage object.
38
   *
39 40
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
41 42
   * @param \Drupal\Core\Database\Connection $database
   *   The database connection to be used.
43 44
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
45 46
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
   *   Cache backend instance to use.
47 48
   * @param \Drupal\Core\Password\PasswordInterface $password
   *   The password hashing service.
49 50
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
51
   */
52 53
  public function __construct(EntityTypeInterface $entity_type, Connection $database, EntityManagerInterface $entity_manager, CacheBackendInterface $cache, PasswordInterface $password, LanguageManagerInterface $language_manager) {
    parent::__construct($entity_type, $database, $entity_manager, $cache, $language_manager);
54 55 56 57 58 59 60

    $this->password = $password;
  }

  /**
   * {@inheritdoc}
   */
61
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
62
    return new static(
63
      $entity_type,
64
      $container->get('database'),
65
      $container->get('entity.manager'),
66
      $container->get('cache.entity'),
67 68
      $container->get('password'),
      $container->get('language_manager')
69 70 71
    );
  }

72
  /**
73
   * {@inheritdoc}
74
   */
75
  public function save(EntityInterface $entity) {
76 77 78
    // The anonymous user account is saved with the fixed user ID of 0.
    // Therefore we need to check for NULL explicitly.
    if ($entity->id() === NULL) {
79
      $entity->uid->value = $this->database->nextId($this->database->query('SELECT MAX(uid) FROM {users}')->fetchField());
80 81 82 83 84
      $entity->enforceIsNew();
    }
    parent::save($entity);
  }

85 86 87 88 89 90 91 92
  /**
   * {@inheritdoc}
   */
  protected function isColumnSerial($table_name, $schema_name) {
    // User storage does not use a serial column for the user id.
    return $table_name == $this->revisionTable && $schema_name == $this->revisionKey;
  }

93 94 95 96
  /**
   * {@inheritdoc}
   */
  public function updateLastLoginTimestamp(UserInterface $account) {
97
    $this->database->update('users_field_data')
98 99 100
      ->fields(array('login' => $account->getLastLoginTime()))
      ->condition('uid', $account->id())
      ->execute();
101 102
    // Ensure that the entity cache is cleared.
    $this->resetCache(array($account->id()));
103 104
  }

105 106 107 108 109 110 111 112 113 114 115 116 117 118
  /**
   * {@inheritdoc}
   */
  public function updateLastAccessTimestamp(AccountInterface $account, $timestamp) {
    $this->database->update('users_field_data')
      ->fields(array(
        'access' => $timestamp,
      ))
      ->condition('uid', $account->id())
      ->execute();
    // Ensure that the entity cache is cleared.
    $this->resetCache(array($account->id()));
  }

119
}