5 use Drupal\Core\Entity\ContentEntityInterface;
6 use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
7 use Drupal\Core\Session\AccountInterface;
10 * Controller class for users.
12 * This extends the Drupal\Core\Entity\Sql\SqlContentEntityStorage class,
13 * adding required special handling for user objects.
15 class UserStorage extends SqlContentEntityStorage implements UserStorageInterface {
20 protected function doSaveFieldItems(ContentEntityInterface $entity, array $names = []) {
21 // The anonymous user account is saved with the fixed user ID of 0.
22 // Therefore we need to check for NULL explicitly.
23 if ($entity->id() === NULL) {
24 $entity->uid->value = $this->database->nextId($this->database->query('SELECT MAX(uid) FROM {users}')->fetchField());
25 $entity->enforceIsNew();
27 return parent::doSaveFieldItems($entity, $names);
33 protected function isColumnSerial($table_name, $schema_name) {
34 // User storage does not use a serial column for the user id.
35 return $table_name == $this->revisionTable && $schema_name == $this->revisionKey;
41 public function updateLastLoginTimestamp(UserInterface $account) {
42 $this->database->update('users_field_data')
43 ->fields(['login' => $account->getLastLoginTime()])
44 ->condition('uid', $account->id())
46 // Ensure that the entity cache is cleared.
47 $this->resetCache([$account->id()]);
53 public function updateLastAccessTimestamp(AccountInterface $account, $timestamp) {
54 $this->database->update('users_field_data')
56 'access' => $timestamp,
58 ->condition('uid', $account->id())
60 // Ensure that the entity cache is cleared.
61 $this->resetCache([$account->id()]);
67 public function deleteRoleReferences(array $rids) {
68 // Remove the role from all users.
69 $this->database->delete('user__roles')
70 ->condition('roles_target_id', $rids)