AdminClient.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\ApiClient;
  4. use App\ApiClient\DTOs\DashboardStatsDto;
  5. use App\ApiClient\DTOs\IpDetailDto;
  6. use App\ApiClient\DTOs\IpListDto;
  7. use App\ApiClient\DTOs\UserDto;
  8. /**
  9. * Wraps the api's `/api/v1/admin/*` endpoints. Calls go out with the
  10. * service token plus `X-Acting-User-Id` from the current session — the
  11. * api uses that to resolve the impersonated user's role and enforce
  12. * RBAC.
  13. *
  14. * For most CRUD endpoints (manual_blocks, allowlist, policies,
  15. * reporters, consumers, tokens, categories) we return raw associative
  16. * arrays mirroring the api's JSON shape. Templates bind onto these
  17. * directly. The richer DTO pattern is reserved for endpoints whose
  18. * response shape benefits from a typed accessor (`UserDto`, the
  19. * IP-detail / dashboard payloads).
  20. *
  21. * Throws the typed `ApiException` subclasses on non-2xx; controllers
  22. * catch them to render validation messages or "API unreachable" states.
  23. */
  24. final class AdminClient
  25. {
  26. public function __construct(private readonly ApiClient $api)
  27. {
  28. }
  29. // ---- identity ----
  30. public function getMe(int $actingUserId): UserDto
  31. {
  32. $payload = $this->api->request('GET', '/api/v1/admin/me', [], $actingUserId);
  33. return UserDto::fromArray($payload);
  34. }
  35. // ---- IPs / dashboard (M09) ----
  36. /**
  37. * @param array<string, mixed> $filters
  38. */
  39. public function searchIps(int $actingUserId, array $filters, int $page = 1, int $pageSize = 25): IpListDto
  40. {
  41. $query = ['page' => $page, 'page_size' => $pageSize];
  42. foreach (['q', 'category', 'min_score', 'max_score', 'country', 'asn', 'status'] as $key) {
  43. if (isset($filters[$key]) && $filters[$key] !== '' && $filters[$key] !== null) {
  44. $query[$key] = $filters[$key];
  45. }
  46. }
  47. $payload = $this->api->request('GET', '/api/v1/admin/ips', ['query' => $query], $actingUserId);
  48. return IpListDto::fromArray($payload);
  49. }
  50. public function getIp(int $actingUserId, string $ip): IpDetailDto
  51. {
  52. $payload = $this->api->request('GET', '/api/v1/admin/ips/' . rawurlencode($ip), [], $actingUserId);
  53. return IpDetailDto::fromArray($payload);
  54. }
  55. public function getDashboardStats(int $actingUserId): DashboardStatsDto
  56. {
  57. $payload = $this->api->request('GET', '/api/v1/admin/stats/dashboard', [], $actingUserId);
  58. return DashboardStatsDto::fromArray($payload);
  59. }
  60. /**
  61. * @return list<array{code: string, count: int}>
  62. */
  63. public function listCountries(int $actingUserId): array
  64. {
  65. $payload = $this->api->request('GET', '/api/v1/admin/ips/countries', [], $actingUserId);
  66. $items = $payload['items'] ?? [];
  67. if (!is_array($items)) {
  68. return [];
  69. }
  70. $out = [];
  71. foreach ($items as $item) {
  72. if (!is_array($item)) {
  73. continue;
  74. }
  75. $out[] = [
  76. 'code' => (string) ($item['code'] ?? ''),
  77. 'count' => (int) ($item['count'] ?? 0),
  78. ];
  79. }
  80. return $out;
  81. }
  82. // ---- manual blocks (M10) ----
  83. /**
  84. * @return array<string, mixed>
  85. */
  86. public function listManualBlocks(int $actingUserId, ?string $kind = null): array
  87. {
  88. $query = ['limit' => 200];
  89. if ($kind !== null && $kind !== '') {
  90. $query['kind'] = $kind;
  91. }
  92. return $this->api->request('GET', '/api/v1/admin/manual-blocks', ['query' => $query], $actingUserId);
  93. }
  94. /**
  95. * @param array<string, mixed> $body
  96. * @return array<string, mixed>
  97. */
  98. public function createManualBlock(int $actingUserId, array $body): array
  99. {
  100. return $this->api->request('POST', '/api/v1/admin/manual-blocks', ['json' => $body], $actingUserId);
  101. }
  102. public function deleteManualBlock(int $actingUserId, int $id): void
  103. {
  104. $this->api->request('DELETE', '/api/v1/admin/manual-blocks/' . $id, [], $actingUserId);
  105. }
  106. // ---- allowlist (M10) ----
  107. /**
  108. * @return array<string, mixed>
  109. */
  110. public function listAllowlist(int $actingUserId, ?string $kind = null): array
  111. {
  112. $query = ['limit' => 200];
  113. if ($kind !== null && $kind !== '') {
  114. $query['kind'] = $kind;
  115. }
  116. return $this->api->request('GET', '/api/v1/admin/allowlist', ['query' => $query], $actingUserId);
  117. }
  118. /**
  119. * @param array<string, mixed> $body
  120. * @return array<string, mixed>
  121. */
  122. public function createAllowlist(int $actingUserId, array $body): array
  123. {
  124. return $this->api->request('POST', '/api/v1/admin/allowlist', ['json' => $body], $actingUserId);
  125. }
  126. public function deleteAllowlist(int $actingUserId, int $id): void
  127. {
  128. $this->api->request('DELETE', '/api/v1/admin/allowlist/' . $id, [], $actingUserId);
  129. }
  130. // ---- policies (M10) ----
  131. /**
  132. * @return array<string, mixed>
  133. */
  134. public function listPolicies(int $actingUserId): array
  135. {
  136. return $this->api->request('GET', '/api/v1/admin/policies', [], $actingUserId);
  137. }
  138. /**
  139. * @return array<string, mixed>
  140. */
  141. public function getPolicy(int $actingUserId, int $id): array
  142. {
  143. return $this->api->request('GET', '/api/v1/admin/policies/' . $id, [], $actingUserId);
  144. }
  145. /**
  146. * @param array<string, mixed> $body
  147. * @return array<string, mixed>
  148. */
  149. public function createPolicy(int $actingUserId, array $body): array
  150. {
  151. return $this->api->request('POST', '/api/v1/admin/policies', ['json' => $body], $actingUserId);
  152. }
  153. /**
  154. * @param array<string, mixed> $body
  155. * @return array<string, mixed>
  156. */
  157. public function updatePolicy(int $actingUserId, int $id, array $body): array
  158. {
  159. return $this->api->request('PATCH', '/api/v1/admin/policies/' . $id, ['json' => $body], $actingUserId);
  160. }
  161. public function deletePolicy(int $actingUserId, int $id): void
  162. {
  163. $this->api->request('DELETE', '/api/v1/admin/policies/' . $id, [], $actingUserId);
  164. }
  165. /**
  166. * @return array<string, mixed>
  167. */
  168. public function previewPolicy(int $actingUserId, int $id): array
  169. {
  170. return $this->api->request('GET', '/api/v1/admin/policies/' . $id . '/preview', [], $actingUserId);
  171. }
  172. // ---- reporters (M10) ----
  173. /**
  174. * @return array<string, mixed>
  175. */
  176. public function listReporters(int $actingUserId): array
  177. {
  178. return $this->api->request('GET', '/api/v1/admin/reporters', ['query' => ['limit' => 200]], $actingUserId);
  179. }
  180. /**
  181. * @return array<string, mixed>
  182. */
  183. public function getReporter(int $actingUserId, int $id): array
  184. {
  185. return $this->api->request('GET', '/api/v1/admin/reporters/' . $id, [], $actingUserId);
  186. }
  187. /**
  188. * @param array<string, mixed> $body
  189. * @return array<string, mixed>
  190. */
  191. public function createReporter(int $actingUserId, array $body): array
  192. {
  193. return $this->api->request('POST', '/api/v1/admin/reporters', ['json' => $body], $actingUserId);
  194. }
  195. /**
  196. * @param array<string, mixed> $body
  197. * @return array<string, mixed>
  198. */
  199. public function updateReporter(int $actingUserId, int $id, array $body): array
  200. {
  201. return $this->api->request('PATCH', '/api/v1/admin/reporters/' . $id, ['json' => $body], $actingUserId);
  202. }
  203. public function deleteReporter(int $actingUserId, int $id): void
  204. {
  205. $this->api->request('DELETE', '/api/v1/admin/reporters/' . $id, [], $actingUserId);
  206. }
  207. // ---- consumers (M10) ----
  208. /**
  209. * @return array<string, mixed>
  210. */
  211. public function listConsumers(int $actingUserId): array
  212. {
  213. return $this->api->request('GET', '/api/v1/admin/consumers', ['query' => ['limit' => 200]], $actingUserId);
  214. }
  215. /**
  216. * @return array<string, mixed>
  217. */
  218. public function getConsumer(int $actingUserId, int $id): array
  219. {
  220. return $this->api->request('GET', '/api/v1/admin/consumers/' . $id, [], $actingUserId);
  221. }
  222. /**
  223. * @param array<string, mixed> $body
  224. * @return array<string, mixed>
  225. */
  226. public function createConsumer(int $actingUserId, array $body): array
  227. {
  228. return $this->api->request('POST', '/api/v1/admin/consumers', ['json' => $body], $actingUserId);
  229. }
  230. /**
  231. * @param array<string, mixed> $body
  232. * @return array<string, mixed>
  233. */
  234. public function updateConsumer(int $actingUserId, int $id, array $body): array
  235. {
  236. return $this->api->request('PATCH', '/api/v1/admin/consumers/' . $id, ['json' => $body], $actingUserId);
  237. }
  238. public function deleteConsumer(int $actingUserId, int $id): void
  239. {
  240. $this->api->request('DELETE', '/api/v1/admin/consumers/' . $id, [], $actingUserId);
  241. }
  242. // ---- tokens (M10) ----
  243. /**
  244. * @return array<string, mixed>
  245. */
  246. public function listTokens(int $actingUserId): array
  247. {
  248. return $this->api->request('GET', '/api/v1/admin/tokens', ['query' => ['limit' => 200]], $actingUserId);
  249. }
  250. /**
  251. * @param array<string, mixed> $body
  252. * @return array<string, mixed>
  253. */
  254. public function createToken(int $actingUserId, array $body): array
  255. {
  256. return $this->api->request('POST', '/api/v1/admin/tokens', ['json' => $body], $actingUserId);
  257. }
  258. public function deleteToken(int $actingUserId, int $id): void
  259. {
  260. $this->api->request('DELETE', '/api/v1/admin/tokens/' . $id, [], $actingUserId);
  261. }
  262. // ---- categories (M10) ----
  263. /**
  264. * @return array<string, mixed>
  265. */
  266. public function listCategories(int $actingUserId): array
  267. {
  268. return $this->api->request('GET', '/api/v1/admin/categories', [], $actingUserId);
  269. }
  270. /**
  271. * @return array<string, mixed>
  272. */
  273. public function getCategory(int $actingUserId, int $id): array
  274. {
  275. return $this->api->request('GET', '/api/v1/admin/categories/' . $id, [], $actingUserId);
  276. }
  277. /**
  278. * @param array<string, mixed> $body
  279. * @return array<string, mixed>
  280. */
  281. public function createCategory(int $actingUserId, array $body): array
  282. {
  283. return $this->api->request('POST', '/api/v1/admin/categories', ['json' => $body], $actingUserId);
  284. }
  285. /**
  286. * @param array<string, mixed> $body
  287. * @return array<string, mixed>
  288. */
  289. public function updateCategory(int $actingUserId, int $id, array $body): array
  290. {
  291. return $this->api->request('PATCH', '/api/v1/admin/categories/' . $id, ['json' => $body], $actingUserId);
  292. }
  293. public function deleteCategory(int $actingUserId, int $id): void
  294. {
  295. $this->api->request('DELETE', '/api/v1/admin/categories/' . $id, [], $actingUserId);
  296. }
  297. // ---- audit / settings (M12) ----
  298. /**
  299. * @param array<string, mixed> $filters
  300. * @return array<string, mixed>
  301. */
  302. public function listAuditLog(int $actingUserId, array $filters, int $page = 1, int $pageSize = 50): array
  303. {
  304. $query = ['page' => $page, 'page_size' => $pageSize];
  305. foreach (['actor_kind', 'actor_id', 'action', 'entity_type', 'entity_id', 'from', 'to'] as $key) {
  306. if (isset($filters[$key]) && $filters[$key] !== '' && $filters[$key] !== null) {
  307. $query[$key] = $filters[$key];
  308. }
  309. }
  310. return $this->api->request('GET', '/api/v1/admin/audit-log', ['query' => $query], $actingUserId);
  311. }
  312. /**
  313. * @return array<string, mixed>
  314. */
  315. public function getJobsStatus(int $actingUserId): array
  316. {
  317. return $this->api->request('GET', '/api/v1/admin/jobs/status', [], $actingUserId);
  318. }
  319. /**
  320. * @param array<string, mixed> $params
  321. * @return array<string, mixed>
  322. */
  323. public function triggerJob(int $actingUserId, string $name, array $params = []): array
  324. {
  325. return $this->api->request(
  326. 'POST',
  327. '/api/v1/admin/jobs/trigger/' . rawurlencode($name),
  328. $params === [] ? [] : ['json' => $params],
  329. $actingUserId,
  330. );
  331. }
  332. /**
  333. * @return array<string, mixed>
  334. */
  335. public function getConfig(int $actingUserId): array
  336. {
  337. return $this->api->request('GET', '/api/v1/admin/config', [], $actingUserId);
  338. }
  339. /**
  340. * Wipe operational data on the api side. The API requires
  341. * `confirm: "PURGE"` in the body — anything else returns 400.
  342. *
  343. * @return array<string, mixed>
  344. */
  345. public function purgeData(int $actingUserId): array
  346. {
  347. return $this->api->request(
  348. 'POST',
  349. '/api/v1/admin/maintenance/purge',
  350. ['json' => ['confirm' => 'PURGE']],
  351. $actingUserId,
  352. );
  353. }
  354. /**
  355. * Load the demo dataset. Returns 409 if data is already seeded.
  356. *
  357. * @return array<string, mixed>
  358. */
  359. public function seedDemo(int $actingUserId): array
  360. {
  361. return $this->api->request(
  362. 'POST',
  363. '/api/v1/admin/maintenance/seed-demo',
  364. [],
  365. $actingUserId,
  366. );
  367. }
  368. }