Elgg  Version 4.3
UsersRememberMeCookiesTable.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
5 use Elgg\Database;
7 
15 
16  use TimeUsing;
17 
21  const TABLE_NAME = 'users_remember_me_cookies';
22 
26  protected $database;
27 
33  public function __construct(Database $database) {
34  $this->database = $database;
35  }
36 
45  public function insertHash(\ElggUser $user, string $hash): int {
46  // This prevents inserting the same hash twice, which seems to be happening in some rare cases
47  // and for unknown reasons. See https://github.com/Elgg/Elgg/issues/8104
48  $this->deleteHash($hash);
49 
50  $insert = Insert::intoTable(self::TABLE_NAME);
51  $insert->values([
52  'code' => $insert->param($hash, ELGG_VALUE_STRING),
53  'guid' => $insert->param($user->guid, ELGG_VALUE_GUID),
54  'timestamp' => $insert->param($this->getCurrentTime()->getTimestamp(), ELGG_VALUE_TIMESTAMP),
55  ]);
56 
57  return $this->database->insertData($insert);
58  }
59 
67  public function getRowFromHash(string $hash): ?\stdClass {
68  $select = Select::fromTable(self::TABLE_NAME);
69  $select->select('*')
70  ->where($select->compare('code', '=', $hash, ELGG_VALUE_STRING));
71 
72  return $this->database->getDataRow($select) ?: null;
73  }
74 
83  public function updateHash(\ElggUser $user, string $hash): bool {
84  $update = Update::table(self::TABLE_NAME);
85  $update->set('timestamp', $update->param($this->getCurrentTime()->getTimestamp(), ELGG_VALUE_TIMESTAMP))
86  ->where($update->compare('guid', '=', $user->guid, ELGG_VALUE_GUID))
87  ->andWhere($update->compare('code', '=', $hash, ELGG_VALUE_STRING));
88 
89  // not interested in number of updated rows, as an update in the same second won't update the row
90  return $this->database->updateData($update);
91  }
92 
100  public function deleteHash(string $hash): int {
101  $delete = Delete::fromTable(self::TABLE_NAME);
102  $delete->where($delete->compare('code', '=', $hash, ELGG_VALUE_STRING));
103 
104  return $this->database->deleteData($delete);
105  }
106 
114  public function deleteAllHashes(\ElggUser $user): int {
115  $delete = Delete::fromTable(self::TABLE_NAME);
116  $delete->where($delete->compare('guid', '=', $user->guid, ELGG_VALUE_GUID));
117 
118  return $this->database->deleteData($delete);
119  }
120 
128  public function deleteExpiredHashes(int $expiration): int {
129  $delete = Delete::fromTable(self::TABLE_NAME);
130  $delete->where($delete->compare('timestamp', '<', $expiration, ELGG_VALUE_TIMESTAMP));
131 
132  return $this->database->deleteData($delete);
133  }
134 }
deleteExpiredHashes(int $expiration)
Remove all expired hashes from the database.
static table($table, $alias=null)
{}
Definition: Update.php:13
The Elgg database.
Definition: Database.php:25
deleteAllHashes(\ElggUser $user)
Remove all the hashes associated with a user.
const ELGG_VALUE_GUID
Definition: constants.php:128
$delete
trait TimeUsing
Adds methods for setting the current time (for testing)
Definition: TimeUsing.php:10
Manage the users_remember_me_cookies table.
getCurrentTime($modifier= '')
Get the (cloned) time.
Definition: TimeUsing.php:25
static intoTable($table)
{}
Definition: Insert.php:13
insertHash(\ElggUser $user, string $hash)
Store a hash in the DB.
$user
Definition: ban.php:7
getRowFromHash(string $hash)
Get the database row for a hash.
const ELGG_VALUE_TIMESTAMP
Definition: constants.php:130
static fromTable($table, $alias=null)
{}
Definition: Select.php:13
const ELGG_VALUE_STRING
Definition: constants.php:127
updateHash(\ElggUser $user, string $hash)
Update the timestamp of a used hash.
deleteHash(string $hash)
Remove a hash from the DB.
static fromTable($table, $alias=null)
{}
Definition: Delete.php:13
__construct(Database $database)
Create a new service.