Elgg  Version 5.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  const TABLE_NAME = 'users_remember_me_cookies';
22 
23  protected Database $database;
24 
30  public function __construct(Database $database) {
31  $this->database = $database;
32  }
33 
42  public function insertHash(\ElggUser $user, string $hash): int {
43  // This prevents inserting the same hash twice, which seems to be happening in some rare cases
44  // and for unknown reasons. See https://github.com/Elgg/Elgg/issues/8104
45  $this->deleteHash($hash);
46 
47  $insert = Insert::intoTable(self::TABLE_NAME);
48  $insert->values([
49  'code' => $insert->param($hash, ELGG_VALUE_STRING),
50  'guid' => $insert->param($user->guid, ELGG_VALUE_GUID),
51  'timestamp' => $insert->param($this->getCurrentTime()->getTimestamp(), ELGG_VALUE_TIMESTAMP),
52  ]);
53 
54  return $this->database->insertData($insert);
55  }
56 
64  public function getRowFromHash(string $hash): ?\stdClass {
65  $select = Select::fromTable(self::TABLE_NAME);
66  $select->select('*')
67  ->where($select->compare('code', '=', $hash, ELGG_VALUE_STRING));
68 
69  return $this->database->getDataRow($select) ?: null;
70  }
71 
80  public function updateHash(\ElggUser $user, string $hash): bool {
81  $update = Update::table(self::TABLE_NAME);
82  $update->set('timestamp', $update->param($this->getCurrentTime()->getTimestamp(), ELGG_VALUE_TIMESTAMP))
83  ->where($update->compare('guid', '=', $user->guid, ELGG_VALUE_GUID))
84  ->andWhere($update->compare('code', '=', $hash, ELGG_VALUE_STRING));
85 
86  // not interested in number of updated rows, as an update in the same second won't update the row
87  return $this->database->updateData($update);
88  }
89 
97  public function deleteHash(string $hash): int {
98  $delete = Delete::fromTable(self::TABLE_NAME);
99  $delete->where($delete->compare('code', '=', $hash, ELGG_VALUE_STRING));
100 
101  return $this->database->deleteData($delete);
102  }
103 
111  public function deleteAllHashes(\ElggUser $user): int {
112  $delete = Delete::fromTable(self::TABLE_NAME);
113  $delete->where($delete->compare('guid', '=', $user->guid, ELGG_VALUE_GUID));
114 
115  return $this->database->deleteData($delete);
116  }
117 
125  public function deleteExpiredHashes(int $expiration): int {
126  $delete = Delete::fromTable(self::TABLE_NAME);
127  $delete->where($delete->compare('timestamp', '<', $expiration, ELGG_VALUE_TIMESTAMP));
128 
129  return $this->database->deleteData($delete);
130  }
131 }
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:113
$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:115
static fromTable($table, $alias=null)
{}
Definition: Select.php:13
const ELGG_VALUE_STRING
Definition: constants.php:112
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.