UserStorage.php 3.47 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 Symfony\Component\DependencyInjection\ContainerInterface;
19 20 21 22

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

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

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

    $this->password = $password;
  }

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

68
  /**
69
   * {@inheritdoc}
70
   */
71
  public function save(EntityInterface $entity) {
72 73 74
    // 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) {
75
      $entity->uid->value = $this->database->nextId($this->database->query('SELECT MAX(uid) FROM {users}')->fetchField());
76 77 78 79 80
      $entity->enforceIsNew();
    }
    parent::save($entity);
  }

81 82 83 84 85 86 87 88
  /**
   * {@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;
  }

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

101 102 103 104 105 106 107 108 109 110 111 112 113 114
  /**
   * {@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()));
  }

115
}