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.
95 lines
2.8 KiB
PHP
95 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Services\Cloud\CloudAccess;
|
|
use App\Services\Cloud\CloudAdminAbusePolicyService;
|
|
use App\Services\Cloud\CloudError;
|
|
use App\Services\Cloud\CloudInput;
|
|
|
|
/** Database-owned administrator API for immutable abuse policy revisions. */
|
|
final class CloudAdminAbusePolicyController
|
|
{
|
|
public function index($request, $response)
|
|
{
|
|
$actor = self::actor($request);
|
|
return self::json(
|
|
$response,
|
|
(new CloudAdminAbusePolicyService())->current($actor)
|
|
);
|
|
}
|
|
|
|
public function preview($request, $response)
|
|
{
|
|
$actor = self::actor($request, true);
|
|
$body = self::body($request);
|
|
return self::json(
|
|
$response,
|
|
(new CloudAdminAbusePolicyService())->preview(
|
|
$actor,
|
|
self::expectedRevision($body['expected_revision'] ?? null),
|
|
self::definition($body['definition'] ?? null)
|
|
)
|
|
);
|
|
}
|
|
|
|
public function update($request, $response)
|
|
{
|
|
$actor = self::actor($request, true);
|
|
$body = self::body($request);
|
|
return self::json(
|
|
$response,
|
|
(new CloudAdminAbusePolicyService())->publish(
|
|
$actor,
|
|
self::expectedRevision($body['expected_revision'] ?? null),
|
|
self::definition($body['definition'] ?? null)
|
|
)
|
|
);
|
|
}
|
|
|
|
/** @return array<string,mixed> */
|
|
private static function actor($request, bool $manage = false): array
|
|
{
|
|
$actor = $request->getAttribute('cloud_actor')
|
|
?? CloudAccess::actor(false);
|
|
if (!($actor['is_admin'] ?? false)) {
|
|
throw new CloudError('需要管理员权限', 403);
|
|
}
|
|
if ($manage) {
|
|
CloudAccess::manage($actor);
|
|
}
|
|
return $actor;
|
|
}
|
|
|
|
/** @return array<string,mixed> */
|
|
private static function body($request): array
|
|
{
|
|
$body = CloudInput::object($request, 32768);
|
|
CloudInput::only($body, ['expected_revision', 'definition']);
|
|
return $body;
|
|
}
|
|
|
|
private static function expectedRevision(mixed $value): int
|
|
{
|
|
if (!is_int($value) || $value < 1) {
|
|
throw new CloudError('网络滥用检测策略修订号无效', 422);
|
|
}
|
|
return $value;
|
|
}
|
|
|
|
/** @return array<string,mixed> */
|
|
private static function definition(mixed $value): array
|
|
{
|
|
if (!is_array($value) || array_is_list($value)) {
|
|
throw new CloudError('网络滥用检测策略定义无效', 422);
|
|
}
|
|
return $value;
|
|
}
|
|
|
|
private static function json($response, array $payload)
|
|
{
|
|
return $response->withJson($payload)
|
|
->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
}
|