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