comparison core/modules/user/src/UserStorage.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2
3 namespace Drupal\user;
4
5 use Drupal\Core\Entity\ContentEntityInterface;
6 use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
7 use Drupal\Core\Session\AccountInterface;
8
9 /**
10 * Controller class for users.
11 *
12 * This extends the Drupal\Core\Entity\Sql\SqlContentEntityStorage class,
13 * adding required special handling for user objects.
14 */
15 class UserStorage extends SqlContentEntityStorage implements UserStorageInterface {
16
17 /**
18 * {@inheritdoc}
19 */
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();
26 }
27 return parent::doSaveFieldItems($entity, $names);
28 }
29
30 /**
31 * {@inheritdoc}
32 */
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;
36 }
37
38 /**
39 * {@inheritdoc}
40 */
41 public function updateLastLoginTimestamp(UserInterface $account) {
42 $this->database->update('users_field_data')
43 ->fields(['login' => $account->getLastLoginTime()])
44 ->condition('uid', $account->id())
45 ->execute();
46 // Ensure that the entity cache is cleared.
47 $this->resetCache([$account->id()]);
48 }
49
50 /**
51 * {@inheritdoc}
52 */
53 public function updateLastAccessTimestamp(AccountInterface $account, $timestamp) {
54 $this->database->update('users_field_data')
55 ->fields([
56 'access' => $timestamp,
57 ])
58 ->condition('uid', $account->id())
59 ->execute();
60 // Ensure that the entity cache is cleared.
61 $this->resetCache([$account->id()]);
62 }
63
64 /**
65 * {@inheritdoc}
66 */
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)
71 ->execute();
72
73 $this->resetCache();
74 }
75
76 }