Elgg  Version 4.3
ApiUsersTable.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
5 use Elgg\Database;
7 
15 
19  protected $database;
20 
24  protected $crypto;
25 
29  protected $table = 'api_users';
30 
38  $this->database = $database;
39  $this->crypto = $crypto;
40  }
41 
47  public function createApiUser() {
48  $public = $this->crypto->getRandomString(40, Crypto::CHARS_HEX);
49  $secret = $this->crypto->getRandomString(40, Crypto::CHARS_HEX);
50 
51  $insert = Insert::intoTable($this->table);
52  $insert->values([
53  'api_key' => $insert->param($public, ELGG_VALUE_STRING),
54  'secret' => $insert->param($secret, ELGG_VALUE_STRING),
55  ]);
56 
57  if ($this->database->insertData($insert) === false) {
58  return false;
59  }
60 
61  return $this->getApiUser($public);
62  }
63 
73  public function getApiUser(string $public_api_key, bool $only_active = true) {
74  $select = Select::fromTable($this->table);
75  $select->select('*')
76  ->where($select->compare('api_key', '=', $public_api_key, ELGG_VALUE_STRING));
77 
78  if ($only_active) {
79  $select->andWhere($select->compare('active', '=', 1, ELGG_VALUE_INTEGER));
80  }
81 
82  $row = $this->database->getDataRow($select);
83  if (empty($row)) {
84  return false;
85  }
86 
87  return $row;
88  }
89 
97  public function removeApiUser(string $public_api_key) {
98  $row = $this->getApiUser($public_api_key);
99  if (empty($row)) {
100  return false;
101  }
102 
103  $delete = Delete::fromTable($this->table);
104  $delete->where($delete->compare('id', '=', $row->id, ELGG_VALUE_ID));
105 
106  return (bool) $this->database->deleteData($delete);
107  }
108 
116  public function enableAPIUser(string $public_api_key) {
117  $update = Update::table($this->table);
118  $update->set('active', $update->param(1, ELGG_VALUE_INTEGER))
119  ->where($update->compare('api_key', '=', $public_api_key, ELGG_VALUE_STRING));
120 
121  return (bool) $this->database->updateData($update);
122  }
123 
131  public function disableAPIUser(string $public_api_key) {
132  $update = Update::table($this->table);
133  $update->set('active', $update->param(0, ELGG_VALUE_INTEGER))
134  ->where($update->compare('api_key', '=', $public_api_key, ELGG_VALUE_STRING));
135 
136  return (bool) $this->database->updateData($update);
137  }
138 }
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:126
__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:129
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:127
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.