Elgg  Version 5.1
ApiUsersTable.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
5 use Elgg\Database;
7 
15 
19  const TABLE_NAME = 'api_users';
20 
21  protected Database $database;
22 
23  protected Crypto $crypto;
24 
31  public function __construct(Database $database, Crypto $crypto) {
32  $this->database = $database;
33  $this->crypto = $crypto;
34  }
35 
41  public function createApiUser() {
42  $public = $this->crypto->getRandomString(40, Crypto::CHARS_HEX);
43  $secret = $this->crypto->getRandomString(40, Crypto::CHARS_HEX);
44 
45  $insert = Insert::intoTable(self::TABLE_NAME);
46  $insert->values([
47  'api_key' => $insert->param($public, ELGG_VALUE_STRING),
48  'secret' => $insert->param($secret, ELGG_VALUE_STRING),
49  ]);
50 
51  if ($this->database->insertData($insert) === false) {
52  return false;
53  }
54 
55  return $this->getApiUser($public);
56  }
57 
67  public function getApiUser(string $public_api_key, bool $only_active = true) {
68  $select = Select::fromTable(self::TABLE_NAME);
69  $select->select('*')
70  ->where($select->compare('api_key', '=', $public_api_key, ELGG_VALUE_STRING));
71 
72  if ($only_active) {
73  $select->andWhere($select->compare('active', '=', 1, ELGG_VALUE_INTEGER));
74  }
75 
76  return $this->database->getDataRow($select) ?: false;
77  }
78 
86  public function removeApiUser(string $public_api_key): bool {
87  $row = $this->getApiUser($public_api_key);
88  if (empty($row)) {
89  return false;
90  }
91 
92  $delete = Delete::fromTable(self::TABLE_NAME);
93  $delete->where($delete->compare('id', '=', $row->id, ELGG_VALUE_ID));
94 
95  return (bool) $this->database->deleteData($delete);
96  }
97 
105  public function enableAPIUser(string $public_api_key): bool {
106  $update = Update::table(self::TABLE_NAME);
107  $update->set('active', $update->param(1, ELGG_VALUE_INTEGER))
108  ->where($update->compare('api_key', '=', $public_api_key, ELGG_VALUE_STRING));
109 
110  return (bool) $this->database->updateData($update);
111  }
112 
120  public function disableAPIUser(string $public_api_key): bool {
121  $update = Update::table(self::TABLE_NAME);
122  $update->set('active', $update->param(0, ELGG_VALUE_INTEGER))
123  ->where($update->compare('api_key', '=', $public_api_key, ELGG_VALUE_STRING));
124 
125  return (bool) $this->database->updateData($update);
126  }
127 }
Cryptographic services.
Definition: Crypto.php:12
static table($table, $alias=null)
{}
Definition: Update.php:13
The Elgg database.
Definition: Database.php:25
const ELGG_VALUE_INTEGER
Value types.
Definition: constants.php:111
__construct(Database $database, Crypto $crypto)
Create a new table handler.
$delete
Manage the contents of the api_users table.
const ELGG_VALUE_ID
Definition: constants.php:114
enableAPIUser(string $public_api_key)
Enable an api user key.
static intoTable($table)
{}
Definition: Insert.php:13
getApiUser(string $public_api_key, bool $only_active=true)
Find an API User&#39;s details based on the provided public api key.
disableAPIUser(string $public_api_key)
Disable an api user key.
static fromTable($table, $alias=null)
{}
Definition: Select.php:13
const ELGG_VALUE_STRING
Definition: constants.php:112
removeApiUser(string $public_api_key)
Revoke an api user key.
static fromTable($table, $alias=null)
{}
Definition: Delete.php:13
createApiUser()
Generate a new API user for a site, returning a new keypair on success.