|
| 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 | + public function getUser(string $username): array |
| 23 | + { |
| 24 | + $uri = '/_api/user/' . $username; |
| 25 | + |
| 26 | + return (array) $this->connector->request('get', $uri); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @return array<mixed> |
| 31 | + * @throws ArangoException |
| 32 | + * @throws GuzzleException |
| 33 | + */ |
| 34 | + public function getUsers(): array |
| 35 | + { |
| 36 | + 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 | + public function hasUser(string $username): bool |
| 46 | + { |
| 47 | + $users = $this->getUsers(); |
| 48 | + |
| 49 | + 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 | + public function createUser(array $user): array |
| 59 | + { |
| 60 | + $body = json_encode((object) $user); |
| 61 | + |
| 62 | + 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 | + public function updateUser(string $username, array $properties): array |
| 73 | + { |
| 74 | + $uri = '/_api/user/' . $username; |
| 75 | + |
| 76 | + $properties = json_encode((object) $properties); |
| 77 | + $options = ['body' => $properties]; |
| 78 | + |
| 79 | + 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 | + public function replaceUser(string $username, array $user): array |
| 90 | + { |
| 91 | + $uri = '/_api/user/' . $username; |
| 92 | + |
| 93 | + $user = json_encode((object) $user); |
| 94 | + $options = ['body' => $user]; |
| 95 | + |
| 96 | + 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 | + public function deleteUser(string $username): bool |
| 106 | + { |
| 107 | + $uri = '/_api/user/' . $username; |
| 108 | + |
| 109 | + 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 | + public function getDatabaseAccessLevel(string $username, string $database): string |
| 120 | + { |
| 121 | + $uri = '/_api/user/' . $username . '/database/' . $database; |
| 122 | + |
| 123 | + 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 | + public function setDatabaseAccessLevel(string $username, string $database, string $grant): array |
| 135 | + { |
| 136 | + $uri = '/_api/user/' . $username . '/database/' . $database; |
| 137 | + |
| 138 | + $grant = json_encode((object) ['grant' => $grant]); |
| 139 | + $options = ['body' => $grant]; |
| 140 | + |
| 141 | + 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 | + public function clearDatabaseAccessLevel(string $username, string $database) |
| 152 | + { |
| 153 | + $uri = '/_api/user/' . $username . '/database/' . $database; |
| 154 | + |
| 155 | + $result = (array) $this->connector->request('delete', $uri); |
| 156 | + |
| 157 | + return ((int) $result['code'] === 202); |
| 158 | + } |
| 159 | +} |
0 commit comments