Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\user;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Entity\ContentEntityInterface;
|
Chris@0
|
6 use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
|
Chris@0
|
7 use Drupal\Core\Session\AccountInterface;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * Controller class for users.
|
Chris@0
|
11 *
|
Chris@0
|
12 * This extends the Drupal\Core\Entity\Sql\SqlContentEntityStorage class,
|
Chris@0
|
13 * adding required special handling for user objects.
|
Chris@0
|
14 */
|
Chris@0
|
15 class UserStorage extends SqlContentEntityStorage implements UserStorageInterface {
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * {@inheritdoc}
|
Chris@0
|
19 */
|
Chris@0
|
20 protected function doSaveFieldItems(ContentEntityInterface $entity, array $names = []) {
|
Chris@0
|
21 // The anonymous user account is saved with the fixed user ID of 0.
|
Chris@0
|
22 // Therefore we need to check for NULL explicitly.
|
Chris@0
|
23 if ($entity->id() === NULL) {
|
Chris@17
|
24 $entity->uid->value = $this->database->nextId($this->database->query('SELECT MAX(uid) FROM {' . $this->getBaseTable() . '}')->fetchField());
|
Chris@0
|
25 $entity->enforceIsNew();
|
Chris@0
|
26 }
|
Chris@0
|
27 return parent::doSaveFieldItems($entity, $names);
|
Chris@0
|
28 }
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * {@inheritdoc}
|
Chris@0
|
32 */
|
Chris@0
|
33 protected function isColumnSerial($table_name, $schema_name) {
|
Chris@0
|
34 // User storage does not use a serial column for the user id.
|
Chris@0
|
35 return $table_name == $this->revisionTable && $schema_name == $this->revisionKey;
|
Chris@0
|
36 }
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * {@inheritdoc}
|
Chris@0
|
40 */
|
Chris@0
|
41 public function updateLastLoginTimestamp(UserInterface $account) {
|
Chris@17
|
42 $this->database->update($this->getDataTable())
|
Chris@0
|
43 ->fields(['login' => $account->getLastLoginTime()])
|
Chris@0
|
44 ->condition('uid', $account->id())
|
Chris@0
|
45 ->execute();
|
Chris@0
|
46 // Ensure that the entity cache is cleared.
|
Chris@0
|
47 $this->resetCache([$account->id()]);
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@0
|
50 /**
|
Chris@0
|
51 * {@inheritdoc}
|
Chris@0
|
52 */
|
Chris@0
|
53 public function updateLastAccessTimestamp(AccountInterface $account, $timestamp) {
|
Chris@17
|
54 $this->database->update($this->getDataTable())
|
Chris@0
|
55 ->fields([
|
Chris@0
|
56 'access' => $timestamp,
|
Chris@0
|
57 ])
|
Chris@0
|
58 ->condition('uid', $account->id())
|
Chris@0
|
59 ->execute();
|
Chris@0
|
60 // Ensure that the entity cache is cleared.
|
Chris@0
|
61 $this->resetCache([$account->id()]);
|
Chris@0
|
62 }
|
Chris@0
|
63
|
Chris@0
|
64 /**
|
Chris@0
|
65 * {@inheritdoc}
|
Chris@0
|
66 */
|
Chris@0
|
67 public function deleteRoleReferences(array $rids) {
|
Chris@0
|
68 // Remove the role from all users.
|
Chris@0
|
69 $this->database->delete('user__roles')
|
Chris@0
|
70 ->condition('roles_target_id', $rids)
|
Chris@0
|
71 ->execute();
|
Chris@0
|
72
|
Chris@0
|
73 $this->resetCache();
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 }
|