|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
// --------------------------------------------------------------------- |
|
4
|
|
|
// |
|
5
|
|
|
// Copyright (C) 2018-2024 Artem Rodygin |
|
6
|
|
|
// |
|
7
|
|
|
// You should have received a copy of the MIT License along with |
|
8
|
|
|
// this file. If not, see <https://opensource.org/licenses/MIT>. |
|
9
|
|
|
// |
|
10
|
|
|
// --------------------------------------------------------------------- |
|
11
|
|
|
|
|
12
|
|
|
namespace Linode\Account\Repository; |
|
13
|
|
|
|
|
14
|
|
|
use Linode\Account\GrantsResponse; |
|
15
|
|
|
use Linode\Account\User; |
|
16
|
|
|
use Linode\Account\UserRepositoryInterface; |
|
17
|
|
|
use Linode\Entity; |
|
18
|
|
|
use Linode\Internal\AbstractRepository; |
|
19
|
|
|
use Linode\LinodeClient; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @codeCoverageIgnore This class was autogenerated. |
|
23
|
|
|
*/ |
|
24
|
|
|
class UserRepository extends AbstractRepository implements UserRepositoryInterface |
|
25
|
|
|
{ |
|
26
|
|
|
public function createUser(array $parameters = []): User |
|
27
|
|
|
{ |
|
28
|
|
|
$response = $this->client->post($this->getBaseUri(), $parameters); |
|
29
|
|
|
$contents = $response->getBody()->getContents(); |
|
30
|
|
|
$json = json_decode($contents, true); |
|
31
|
|
|
|
|
32
|
|
|
return new User($this->client, $json); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function updateUser(string $username, array $parameters = []): User |
|
36
|
|
|
{ |
|
37
|
|
|
$response = $this->client->put(sprintf('%s/%s', $this->getBaseUri(), $username), $parameters); |
|
38
|
|
|
$contents = $response->getBody()->getContents(); |
|
39
|
|
|
$json = json_decode($contents, true); |
|
40
|
|
|
|
|
41
|
|
|
return new User($this->client, $json); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function deleteUser(string $username): void |
|
45
|
|
|
{ |
|
46
|
|
|
$this->client->delete(sprintf('%s/%s', $this->getBaseUri(), $username)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function getUserGrants(string $username): ?GrantsResponse |
|
50
|
|
|
{ |
|
51
|
|
|
$response = $this->client->get(sprintf('%s/%s/grants', $this->getBaseUri(), $username)); |
|
52
|
|
|
|
|
53
|
|
|
if (LinodeClient::SUCCESS_NO_CONTENT === $response->getStatusCode()) { |
|
54
|
|
|
return null; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$contents = $response->getBody()->getContents(); |
|
58
|
|
|
$json = json_decode($contents, true); |
|
59
|
|
|
|
|
60
|
|
|
return new GrantsResponse($this->client, $json); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function updateUserGrants(string $username, array $parameters = []): GrantsResponse |
|
64
|
|
|
{ |
|
65
|
|
|
$response = $this->client->put(sprintf('%s/%s/grants', $this->getBaseUri(), $username), $parameters); |
|
66
|
|
|
$contents = $response->getBody()->getContents(); |
|
67
|
|
|
$json = json_decode($contents, true); |
|
68
|
|
|
|
|
69
|
|
|
return new GrantsResponse($this->client, $json); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function getBaseUri(): string |
|
73
|
|
|
{ |
|
74
|
|
|
return '/account/users'; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
protected function getSupportedFields(): array |
|
78
|
|
|
{ |
|
79
|
|
|
return [ |
|
80
|
|
|
User::FIELD_USERNAME, |
|
81
|
|
|
User::FIELD_EMAIL, |
|
82
|
|
|
User::FIELD_RESTRICTED, |
|
83
|
|
|
User::FIELD_SSH_KEYS, |
|
84
|
|
|
]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function jsonToEntity(array $json): Entity |
|
88
|
|
|
{ |
|
89
|
|
|
return new User($this->client, $json); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|