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.
65 lines
2.0 KiB
PHP
65 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Services\Cloud\CloudAccess;
|
|
use App\Services\Cloud\CloudAgentSettingsService;
|
|
use App\Services\Cloud\CloudError;
|
|
use App\Services\Cloud\CloudInput;
|
|
|
|
final class CloudAdminAgentController
|
|
{
|
|
public function index($request, $response)
|
|
{
|
|
$this->actor($request, false);
|
|
return $response->withJson(CloudAgentSettingsService::listing())
|
|
->withHeader('Cache-Control', 'no-store');
|
|
}
|
|
|
|
public function settings($request, $response, $args)
|
|
{
|
|
$write = $request->getMethod() !== 'GET';
|
|
$this->actor($request, $write);
|
|
$hostId = $this->hostId($args['host_id'] ?? null);
|
|
$payload = $write
|
|
? CloudAgentSettingsService::save($hostId,
|
|
CloudInput::object($request, 4096))
|
|
: CloudAgentSettingsService::read($hostId);
|
|
return $response->withJson($payload)
|
|
->withHeader('Cache-Control', 'no-store');
|
|
}
|
|
|
|
public function test($request, $response, $args)
|
|
{
|
|
$actor = $this->actor($request, true);
|
|
$payload = CloudAgentSettingsService::test(
|
|
$this->hostId($args['host_id'] ?? null),
|
|
$actor,
|
|
CloudInput::object($request, 1024)
|
|
);
|
|
return $response->withJson($payload)
|
|
->withHeader('Cache-Control', 'no-store');
|
|
}
|
|
|
|
private function actor($request, bool $write): array
|
|
{
|
|
$actor = $request->getAttribute('cloud_actor') ?? CloudAccess::actor(false);
|
|
if (!($actor['is_admin'] ?? false)) {
|
|
throw new CloudError('需要管理员权限', 403);
|
|
}
|
|
if ($write) {
|
|
CloudAccess::manage($actor);
|
|
}
|
|
return $actor;
|
|
}
|
|
|
|
private function hostId($value): int
|
|
{
|
|
if (!is_scalar($value)
|
|
|| preg_match('/^[1-9][0-9]{0,18}$/D', (string) $value) !== 1) {
|
|
throw new CloudError('宿主机不存在', 404);
|
|
}
|
|
return (int) $value;
|
|
}
|
|
}
|