Elgg  Version 6.1
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  public const TABLE_NAME = 'users_remember_me_cookies';
22 
28  public function __construct(protected Database $database) {
29  }
30 
39  public function insertHash(\ElggUser $user, string $hash): int {
40  // This prevents inserting the same hash twice, which seems to be happening in some rare cases
41  // and for unknown reasons. See https://github.com/Elgg/Elgg/issues/8104
42  $this->deleteHash($hash);
43 
44  $insert = Insert::intoTable(self::TABLE_NAME);
45  $insert->values([
46  'code' => $insert->param($hash, ELGG_VALUE_STRING),
47  'guid' => $insert->param($user->guid, ELGG_VALUE_GUID),
48  'timestamp' => $insert->param($this->getCurrentTime()->getTimestamp(), ELGG_VALUE_TIMESTAMP),
49  ]);
50 
51  return $this->database->insertData($insert);
52  }
53 
61  public function getRowFromHash(string $hash): ?\stdClass {
62  $select = Select::fromTable(self::TABLE_NAME);
63  $select->select('*')
64  ->where($select->compare('code', '=', $hash, ELGG_VALUE_STRING));
65 
66  return $this->database->getDataRow($select) ?: null;
67  }
68 
77  public function updateHash(\ElggUser $user, string $hash): bool {
78  $update = Update::table(self::TABLE_NAME);
79  $update->set('timestamp', $update->param($this->getCurrentTime()->getTimestamp(), ELGG_VALUE_TIMESTAMP))
80  ->where($update->compare('guid', '=', $user->guid, ELGG_VALUE_GUID))
81  ->andWhere($update->compare('code', '=', $hash, ELGG_VALUE_STRING));
82 
83  // not interested in number of updated rows, as an update in the same second won't update the row
84  return $this->database->updateData($update);
85  }
86 
94  public function deleteHash(string $hash): int {
95  $delete = Delete::fromTable(self::TABLE_NAME);
96  $delete->where($delete->compare('code', '=', $hash, ELGG_VALUE_STRING));
97 
98  return $this->database->deleteData($delete);
99  }
100 
108  public function deleteAllHashes(\ElggUser $user): int {
109  $delete = Delete::fromTable(self::TABLE_NAME);
110  $delete->where($delete->compare('guid', '=', $user->guid, ELGG_VALUE_GUID));
111 
112  return $this->database->deleteData($delete);
113  }
114 
122  public function deleteExpiredHashes(int $expiration): int {
123  $delete = Delete::fromTable(self::TABLE_NAME);
124  $delete->where($delete->compare('timestamp', '<', $expiration, ELGG_VALUE_TIMESTAMP));
125 
126  return $this->database->deleteData($delete);
127  }
128 }
static table(string $table)
Returns a QueryBuilder for updating data in a given table.
Definition: Update.php:17
deleteExpiredHashes(int $expiration)
Remove all expired hashes from the database.
__construct(protected Database $database)
Create a new service.
The Elgg database.
Definition: Database.php:26
deleteAllHashes(\ElggUser $user)
Remove all the hashes associated with a user.
const ELGG_VALUE_GUID
Definition: constants.php:113
$delete
trait TimeUsing
Adds methods for setting the current time (for testing)
Definition: TimeUsing.php:10
static intoTable(string $table)
Returns a QueryBuilder for inserting data in a given table.
Definition: Insert.php:17
Manage the users_remember_me_cookies table.
getCurrentTime($modifier= '')
Get the (cloned) time.
Definition: TimeUsing.php:25
insertHash(\ElggUser $user, string $hash)
Store a hash in the DB.
$user
Definition: ban.php:7
static fromTable(string $table)
Returns a QueryBuilder for deleting data from a given table.
Definition: Delete.php:17
getRowFromHash(string $hash)
Get the database row for a hash.
const ELGG_VALUE_TIMESTAMP
Definition: constants.php:115
const ELGG_VALUE_STRING
Definition: constants.php:112
static fromTable(string $table, string $alias=null)
Returns a QueryBuilder for selecting data from a given table.
Definition: Select.php:18
updateHash(\ElggUser $user, string $hash)
Update the timestamp of a used hash.
deleteHash(string $hash)
Remove a hash from the DB.