You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Controllers/CloudAdminFabricController.php

69 lines
2.1 KiB
PHP

<?php
namespace App\Controllers;
use App\Services\Cloud\CloudAccess;
use App\Services\Cloud\CloudAdminFabricService;
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudInput;
/** Administrator-only Fabric topology and reconciliation API. */
final class CloudAdminFabricController
{
private function actor($request, bool $write = false): array
{
$actor = $request->getAttribute('cloud_actor') ?? CloudAccess::actor(false);
if (!($actor['is_admin'] ?? false)) {
throw new CloudError('需要管理员权限', 403);
}
if ($write) {
CloudAccess::manage($actor);
if (!($actor['is_super_admin'] ?? false)) {
throw new CloudError('仅超级管理员可以修改 Fabric', 403);
}
}
return $actor;
}
private function json($response, array $payload, int $status = 200)
{
return $response->withJson($payload, $status)
->withHeader('Cache-Control', 'private, no-store');
}
public function index($request, $response)
{
return $this->json($response,
CloudAdminFabricService::listing($this->actor($request)));
}
public function detail($request, $response, $args)
{
return $this->json($response,
CloudAdminFabricService::detail(
(string) ($args['fabric_id'] ?? ''), $this->actor($request)
));
}
public function apply($request, $response)
{
$actor = $this->actor($request, true);
$body = CloudInput::object($request, 131072);
return $this->json($response,
CloudAdminFabricService::apply($body, $actor), 200);
}
public function reconcile($request, $response, $args)
{
$actor = $this->actor($request, true);
$body = trim((string) $request->getBody());
if ($body !== '' && $body !== '{}') {
throw new CloudError('Fabric reconcile 不接受请求体', 422);
}
return $this->json($response,
CloudAdminFabricService::reconcile(
(string) ($args['fabric_id'] ?? ''), $actor
));
}
}