1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class to store API key information in |
4
|
|
|
* |
5
|
|
|
* @property string public_key the public key for this api object |
6
|
|
|
* |
7
|
|
|
* @since 3.2 |
8
|
|
|
*/ |
9
|
|
|
class ElggApiKey extends ElggObject { |
10
|
|
|
|
11
|
|
|
const SUBTYPE = 'api_key'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* {@inheritdoc} |
15
|
|
|
*/ |
16
|
5 |
|
protected function initializeAttributes() { |
17
|
5 |
|
parent::initializeAttributes(); |
18
|
|
|
|
19
|
5 |
|
$site = elgg_get_site_entity(); |
20
|
|
|
|
21
|
5 |
|
$this->attributes['access_id'] = ACCESS_PUBLIC; |
22
|
5 |
|
$this->attributes['container_guid'] = $site->guid; |
23
|
5 |
|
$this->attributes['owner_guid'] = $site->guid; |
24
|
5 |
|
$this->attributes['subtype'] = self::SUBTYPE; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
5 |
|
protected function create() { |
31
|
5 |
|
$result = parent::create(); |
32
|
|
|
|
33
|
5 |
|
if ($result !== false) { |
34
|
5 |
|
$this->generateKeys(); |
35
|
|
|
} |
36
|
|
|
|
37
|
5 |
|
return $result; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
5 |
|
public function delete(bool $recursive = true): bool { |
44
|
5 |
|
$public_key = $this->public_key; |
45
|
|
|
|
46
|
5 |
|
if (!parent::delete($recursive)) { |
47
|
|
|
return false; |
48
|
|
|
} |
49
|
|
|
|
50
|
5 |
|
if (isset($public_key)) { |
51
|
5 |
|
_elgg_services()->apiUsersTable->removeApiUser($public_key); |
52
|
|
|
} |
53
|
|
|
|
54
|
5 |
|
return true; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the api keys |
59
|
|
|
* |
60
|
|
|
* @return false|stdClass |
61
|
|
|
*/ |
62
|
5 |
|
public function getKeys() { |
63
|
|
|
|
64
|
5 |
|
if (empty($this->public_key)) { |
65
|
|
|
return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
5 |
|
return _elgg_services()->apiUsersTable->getApiUser($this->public_key, false); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the public key for this api object |
73
|
|
|
* |
74
|
|
|
* @return null|string |
75
|
|
|
*/ |
76
|
4 |
|
public function getPublicKey(): ?string { |
77
|
4 |
|
return $this->public_key; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the secret key for this api object |
82
|
|
|
* |
83
|
|
|
* @return false|string |
84
|
|
|
*/ |
85
|
2 |
|
public function getSecretKey() { |
86
|
2 |
|
$keys = $this->getKeys(); |
87
|
2 |
|
if (empty($keys)) { |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
return $keys->secret; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Generate API keys |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
5 |
|
public function generateKeys(): bool { |
100
|
|
|
|
101
|
5 |
|
$keys = _elgg_services()->apiUsersTable->createApiUser(); |
102
|
5 |
|
if (empty($keys)) { |
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// save new key |
107
|
5 |
|
$this->public_key = $keys->api_key; |
108
|
|
|
|
109
|
5 |
|
return true; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Regenerate API keys |
114
|
|
|
* |
115
|
|
|
* NOTE: this will remove the old keys from the database, therefor the old keys no longer work |
116
|
|
|
* |
117
|
|
|
* @return bool |
118
|
|
|
*/ |
119
|
1 |
|
public function regenerateKeys(): bool { |
120
|
1 |
|
$current_public = $this->getPublicKey(); |
121
|
|
|
|
122
|
1 |
|
if (!$this->generateKeys()) { |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// remove old keys from DB |
127
|
1 |
|
_elgg_services()->apiUsersTable->removeApiUser($current_public); |
128
|
|
|
|
129
|
1 |
|
return true; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Check if the API keys are active |
134
|
|
|
* |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
2 |
|
public function hasActiveKeys(): bool { |
138
|
2 |
|
$keys = $this->getKeys(); |
139
|
2 |
|
if (empty($keys)) { |
140
|
|
|
return false; |
141
|
|
|
} |
142
|
|
|
|
143
|
2 |
|
return (bool) $keys->active; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Enables the API key for use by API requests |
148
|
|
|
* |
149
|
|
|
* @return bool |
150
|
|
|
*/ |
151
|
1 |
|
public function enableKeys(): bool { |
152
|
1 |
|
return _elgg_services()->apiUsersTable->enableAPIUser($this->getPublicKey()); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Disables the API key for use by API requests |
157
|
|
|
* |
158
|
|
|
* @return bool |
159
|
|
|
*/ |
160
|
2 |
|
public function disableKeys(): bool { |
161
|
2 |
|
return _elgg_services()->apiUsersTable->disableAPIUser($this->getPublicKey()); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|