Elgg  Version master
ApiUsersTable.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
5 use Elgg\Database;
7 
15 
19  public const TABLE_NAME = 'api_users';
20 
27  public function __construct(protected Database $database, protected Crypto $crypto) {
28  }
29 
35  public function createApiUser() {
36  $public = $this->crypto->getRandomString(40, Crypto::CHARS_HEX);
37  $secret = $this->crypto->getRandomString(40, Crypto::CHARS_HEX);
38 
39  $insert = Insert::intoTable(self::TABLE_NAME);
40  $insert->values([
41  'api_key' => $insert->param($public, ELGG_VALUE_STRING),
42  'secret' => $insert->param($secret, ELGG_VALUE_STRING),
43  ]);
44 
45  if ($this->database->insertData($insert) === false) {
46  return false;
47  }
48 
49  return $this->getApiUser($public);
50  }
51 
61  public function getApiUser(string $public_api_key, bool $only_active = true) {
62  $select = Select::fromTable(self::TABLE_NAME);
63  $select->select('*')
64  ->where($select->compare('api_key', '=', $public_api_key, ELGG_VALUE_STRING));
65 
66  if ($only_active) {
67  $select->andWhere($select->compare('active', '=', 1, ELGG_VALUE_INTEGER));
68  }
69 
70  return $this->database->getDataRow($select) ?: false;
71  }
72 
80  public function removeApiUser(string $public_api_key): bool {
81  $row = $this->getApiUser($public_api_key);
82  if (empty($row)) {
83  return false;
84  }
85 
86  $delete = Delete::fromTable(self::TABLE_NAME);
87  $delete->where($delete->compare('id', '=', $row->id, ELGG_VALUE_ID));
88 
89  return (bool) $this->database->deleteData($delete);
90  }
91 
99  public function enableAPIUser(string $public_api_key): bool {
100  $update = Update::table(self::TABLE_NAME);
101  $update->set('active', $update->param(1, ELGG_VALUE_INTEGER))
102  ->where($update->compare('api_key', '=', $public_api_key, ELGG_VALUE_STRING));
103 
104  return (bool) $this->database->updateData($update);
105  }
106 
114  public function disableAPIUser(string $public_api_key): bool {
115  $update = Update::table(self::TABLE_NAME);
116  $update->set('active', $update->param(0, ELGG_VALUE_INTEGER))
117  ->where($update->compare('api_key', '=', $public_api_key, ELGG_VALUE_STRING));
118 
119  return (bool) $this->database->updateData($update);
120  }
121 }
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
$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
__construct(protected Database $database, protected Crypto $crypto)
Create a new table handler.
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.