-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUsers.php
281 lines (242 loc) · 6.99 KB
/
Users.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
/**
* PHP Wrapper to Interact with Vultr 2.0 API
*
* @version 2.0
*
* @license http://www.opensource.org/licenses/mit-license.php MIT
*
* @see https://github.com/dutche027/vultr-php
* @see https://packagist.org/packages/dutchie027/vultr
* @see https://www.vultr.com/api/v2
*/
namespace dutchie027\Vultr;
use dutchie027\Vultr\Exceptions\InvalidParameterException;
use dutchie027\Vultr\Exceptions\VultrAPIException;
class Users
{
/**
* Reference to \API object
*
* @var API
*/
protected $api;
/**
* Array containing user data
*
* @var array<string>
*/
protected $users = [];
/**
* Array containing User IDs
*
* @var array<int>
*/
protected $ids = [];
/**
* Total Number of Users
*
* @var int
*/
protected $total_users = 0;
/**
* Default ACL for creaging a user
*
* @var array<mixed>
*/
protected $d_acl = [
'acls' => [
'subscriptions_view',
],
];
/**
* Default Value for API Enabled when Creating
* A User
*
* @var bool
*/
protected $d_api_enabled = false;
/**
* Array containing valid ACLs when creating a user
*
* @var array<string>
*/
private $valid_acls = [
'abuse',
'alerts',
'billing',
'dns',
'firewall',
'loadbalancer',
'manage_users',
'objstore',
'provisioning',
'subscriptions',
'subscriptions_view',
'support',
'upgrade',
];
/**
* __construct
* Takes reference from \API
*/
public function __construct(API $api)
{
$this->api = $api;
$this->loadUsers();
}
/**
* getUser
* Gets information on a specific user based on their ID
*/
public function getUser(string $id): string
{
if (in_array($id, $this->ids, true)) {
return $this->api->makeAPICall('GET', $this->api::USERS_URL . '/' . $id);
}
throw new InvalidParameterException("That User ID isn't associated with your account");
}
/**
* getUsers
* Gets All users
*/
public function getUsers(): string
{
return $this->api->makeAPICall('GET', $this->api::USERS_URL);
}
/**
* loadUsers
* Loads User Information in to arrays
*/
public function loadUsers(): void
{
$ua = json_decode($this->getUsers(), true);
foreach ($ua['users'] as $usr) {
$id = $usr['id'];
$this->ids[] = $id;
$this->users[$id] = $usr;
}
$this->total_users = $ua['meta']['total'];
}
/**
* updateuser
* Updates A User
* @param array<string,string> $oa
*/
public function updateUser(array $oa): string
{
if (in_array($oa['id'], $this->ids, true)) {
$url = $this->api::USERS_URL . '/' . $oa['id'];
} else {
throw new InvalidParameterException("That User ID doesn't exist");
}
$cr = false;
$ba = [];
if (isset($oa['email'])) {
if (filter_var($oa['email'], FILTER_VALIDATE_EMAIL)) {
$cr = true;
$ba['email'] = $oa['email'];
} else {
throw new InvalidParameterException('Email included but is invalid');
}
}
if (isset($oa['password'])) {
if (strlen($oa['password']) < 8) {
throw new InvalidParameterException('Password included but is less than 8 characters');
}
$cr = true;
$ba['password'] = $oa['password'];
}
if (isset($oa['name'])) {
if (strlen($oa['name']) < 4) {
throw new InvalidParameterException('Name included but is less than 4 characters');
}
$cr = true;
$ba['name'] = $oa['name'];
}
if (isset($oa['api_enabled'])) {
if (!is_bool($oa['api_enabled'])) {
throw new InvalidParameterException('API Enabled Flag Is Invalid');
}
$cr = true;
$ba['api_enabled'] = $oa['api_enabled'];
}
if (isset($oa['acls'])) {
if (is_array($oa['acls'])) {
$acl_valid = true;
foreach ($oa['acls'] as $acl) {
if (!in_array($acl, $this->valid_acls, true)) {
$acl_valid = false;
}
}
if (!$acl_valid) {
throw new InvalidParameterException('Invalid ACLS');
}
$ba['acls'] = $oa['acls'];
$cr = true;
} else {
throw new InvalidParameterException('ACL Value(s) passed invalid. Must be an array');
}
}
if ($cr) {
$body = $this->api->returnJSONBody($ba);
return $this->api->makeAPICall('PATCH', $url, $body);
}
throw new VultrAPIException('Nothing to PATCH on line ' . __LINE__);
}
/**
* createUser
* Creates a User
* @param array<string,string> $oa
*/
public function createUser(array $oa): string
{
$ba['api_enabled'] = $this->d_api_enabled;
$ba['acls'] = $this->d_acl;
if (!isset($oa['email']) || !filter_var($oa['email'], FILTER_VALIDATE_EMAIL)) {
throw new InvalidParameterException('Email required or is invalid');
}
$ba['email'] = $oa['email'];
if (!isset($oa['password']) || strlen($oa['password']) < 8) {
throw new InvalidParameterException('Password Required or is less than 8 characters');
}
$ba['password'] = $oa['password'];
if (!isset($oa['name']) || strlen($oa['name']) < 4) {
throw new InvalidParameterException('Name Required or is less than 4 characters');
}
$ba['name'] = $oa['name'];
if (isset($oa['acls']) && is_array($oa['acls'])) {
$acl_valid = true;
foreach ($oa['acls'] as $acl) {
if (!in_array($acl, $this->valid_acls, true)) {
$acl_valid = false;
}
}
if (!$acl_valid) {
throw new InvalidParameterException('Invalid ACLS');
}
$ba['acls'] = $oa['acls'];
}
$body = $this->api->returnJSONBody($ba);
return $this->api->makeAPICall('POST', $this->api::USERS_URL, $body);
}
/**
* deleteUser
* Deletes User
*/
public function deleteUser(string $id): string
{
if (in_array($id, $this->ids, true)) {
return $this->api->makeAPICall('DELETE', $this->api::USERS_URL . '/' . $id);
}
throw new InvalidParameterException("That User ID isn't associated with your account");
}
/**
* getNumberOfUsers
* Returns total number of users
*/
public function getNumberOfUsers(): int
{
return $this->total_users;
}
}