1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ArangoClient\Schema; |
4
|
|
|
|
5
|
|
|
use ArangoClient\Connector; |
6
|
|
|
use ArangoClient\Exceptions\ArangoException; |
7
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
8
|
|
|
|
9
|
|
|
/* |
10
|
|
|
* @see https://www.arangodb.com/docs/stable/http/views.html |
11
|
|
|
*/ |
12
|
|
|
trait ManagesUsers |
13
|
|
|
{ |
14
|
|
|
protected Connector $connector; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param string $username |
18
|
|
|
* @return array<mixed> |
19
|
|
|
* @throws ArangoException |
20
|
|
|
* @throws GuzzleException |
21
|
|
|
*/ |
22
|
1 |
|
public function getUser(string $username): array |
23
|
|
|
{ |
24
|
1 |
|
$uri = '/_api/user/' . $username; |
25
|
|
|
|
26
|
1 |
|
return (array) $this->connector->request('get', $uri); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return array<mixed> |
31
|
|
|
* @throws ArangoException |
32
|
|
|
* @throws GuzzleException |
33
|
|
|
*/ |
34
|
9 |
|
public function getUsers(): array |
35
|
|
|
{ |
36
|
9 |
|
return (array) $this->connector->request('get', '/_api/user'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $username |
41
|
|
|
* @return bool |
42
|
|
|
* @throws ArangoException |
43
|
|
|
* @throws GuzzleException |
44
|
|
|
*/ |
45
|
9 |
|
public function hasUser(string $username): bool |
46
|
|
|
{ |
47
|
9 |
|
$users = $this->getUsers(); |
48
|
|
|
|
49
|
9 |
|
return array_search($username, array_column($users, 'user'), true) !== false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param array<mixed> $user |
54
|
|
|
* @return array<mixed> |
55
|
|
|
* @throws ArangoException |
56
|
|
|
* @throws GuzzleException |
57
|
|
|
*/ |
58
|
9 |
|
public function createUser(array $user): array |
59
|
|
|
{ |
60
|
9 |
|
$body = json_encode((object) $user); |
61
|
|
|
|
62
|
9 |
|
return (array) $this->connector->request('post', '/_api/user', ['body' => $body]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $username |
67
|
|
|
* @param array<mixed> $properties |
68
|
|
|
* @return array<mixed> |
69
|
|
|
* @throws ArangoException |
70
|
|
|
* @throws GuzzleException |
71
|
|
|
*/ |
72
|
1 |
|
public function updateUser(string $username, array $properties): array |
73
|
|
|
{ |
74
|
1 |
|
$uri = '/_api/user/' . $username; |
75
|
|
|
|
76
|
1 |
|
$properties = json_encode((object) $properties); |
77
|
1 |
|
$options = ['body' => $properties]; |
78
|
|
|
|
79
|
1 |
|
return (array) $this->connector->request('patch', $uri, $options); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $username |
84
|
|
|
* @param array<mixed> $user |
85
|
|
|
* @return array<mixed> |
86
|
|
|
* @throws ArangoException |
87
|
|
|
* @throws GuzzleException |
88
|
|
|
*/ |
89
|
1 |
|
public function replaceUser(string $username, array $user): array |
90
|
|
|
{ |
91
|
1 |
|
$uri = '/_api/user/' . $username; |
92
|
|
|
|
93
|
1 |
|
$user = json_encode((object) $user); |
94
|
1 |
|
$options = ['body' => $user]; |
95
|
|
|
|
96
|
1 |
|
return (array) $this->connector->request('put', $uri, $options); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $username |
101
|
|
|
* @return bool |
102
|
|
|
* @throws ArangoException |
103
|
|
|
* @throws GuzzleException |
104
|
|
|
*/ |
105
|
9 |
|
public function deleteUser(string $username): bool |
106
|
|
|
{ |
107
|
9 |
|
$uri = '/_api/user/' . $username; |
108
|
|
|
|
109
|
9 |
|
return (bool) $this->connector->request('delete', $uri); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $username |
114
|
|
|
* @param string $database |
115
|
|
|
* @return string |
116
|
|
|
* @throws ArangoException |
117
|
|
|
* @throws GuzzleException |
118
|
|
|
*/ |
119
|
3 |
|
public function getDatabaseAccessLevel(string $username, string $database): string |
120
|
|
|
{ |
121
|
3 |
|
$uri = '/_api/user/' . $username . '/database/' . $database; |
122
|
|
|
|
123
|
3 |
|
return (string) $this->connector->request('get', $uri); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $username |
128
|
|
|
* @param string $database |
129
|
|
|
* @param string $grant |
130
|
|
|
* @return array<mixed> |
131
|
|
|
* @throws ArangoException |
132
|
|
|
* @throws GuzzleException |
133
|
|
|
*/ |
134
|
2 |
|
public function setDatabaseAccessLevel(string $username, string $database, string $grant): array |
135
|
|
|
{ |
136
|
2 |
|
$uri = '/_api/user/' . $username . '/database/' . $database; |
137
|
|
|
|
138
|
2 |
|
$grant = json_encode((object) ['grant' => $grant]); |
139
|
2 |
|
$options = ['body' => $grant]; |
140
|
|
|
|
141
|
2 |
|
return (array) $this->connector->request('put', $uri, $options); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $username |
146
|
|
|
* @param string $database |
147
|
|
|
* @return bool |
148
|
|
|
* @throws ArangoException |
149
|
|
|
* @throws GuzzleException |
150
|
|
|
*/ |
151
|
1 |
|
public function clearDatabaseAccessLevel(string $username, string $database) |
152
|
|
|
{ |
153
|
1 |
|
$uri = '/_api/user/' . $username . '/database/' . $database; |
154
|
|
|
|
155
|
1 |
|
$result = (array) $this->connector->request('delete', $uri); |
156
|
|
|
|
157
|
1 |
|
return ((int) $result['code'] === 202); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|