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/CloudAdminMaintenanceNotice...

155 lines
5.2 KiB
PHP

<?php
namespace App\Controllers;
use App\Services\Cloud\CloudAccess;
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudInput;
use App\Services\Cloud\CloudMaintenanceNoticeService;
/** Database-owned administrator API for reviewed maintenance notices. */
final class CloudAdminMaintenanceNoticeController
{
private function actor($request, bool $manage = false,
bool $idempotency = false): array
{
$actor = $request->getAttribute('cloud_actor')
?? CloudAccess::actor(false);
if (!($actor['is_admin'] ?? false)) {
throw new CloudError('需要管理员权限', 403);
}
if ($manage) {
CloudAccess::manage($actor);
}
if ($idempotency) {
$key = trim((string) $request->getHeaderLine('Idempotency-Key'));
if (preg_match('/^[A-Za-z0-9._:-]{16,128}$/D', $key) !== 1) {
throw new CloudError('管理员写操作需要有效的幂等标识', 400);
}
$actor['request_idempotency_key'] = $key;
}
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)
{
$this->actor($request);
return $this->json(
$response,
(new CloudMaintenanceNoticeService())->page(
$request->getQueryParams()
)
);
}
public function detail($request, $response, $args)
{
$actor = $this->actor($request);
$payload = (new CloudMaintenanceNoticeService())->detail(
(string) ($args['notice_key'] ?? '')
);
return $this->json($response, self::mask($payload, $actor));
}
public function preview($request, $response)
{
$this->actor($request, true);
$body = CloudInput::object($request, 4096);
CloudInput::only($body, ['scope_type', 'scope_id']);
$scopeId = filter_var($body['scope_id'] ?? null,
FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
if ($scopeId === false) {
throw new CloudError('维护通知范围无效', 422);
}
return $this->json(
$response,
(new CloudMaintenanceNoticeService())->preview(
(string) ($body['scope_type'] ?? ''),
(int) $scopeId
)
);
}
public function create($request, $response)
{
$actor = $this->actor($request, true, true);
$body = CloudInput::object($request, 16384);
CloudInput::only($body, [
'scope_type', 'scope_id', 'subject', 'message_text', 'impact',
'maintenance_starts_at', 'maintenance_ends_at', 'scheduled_at',
'audience_digest',
]);
$payload = (new CloudMaintenanceNoticeService())->schedule(
(int) $actor['user_id'],
(string) $actor['request_idempotency_key'],
$body
);
return $this->json($response, $payload, 201);
}
public function cancel($request, $response, $args)
{
$this->actor($request, true, true);
$body = CloudInput::object($request, 1024);
CloudInput::only($body, []);
return $this->json(
$response,
(new CloudMaintenanceNoticeService())->cancel(
(string) ($args['notice_key'] ?? '')
)
);
}
private static function mask(array $payload, array $actor): array
{
if (($actor['is_super_admin'] ?? false)
&& ($actor['can_manage'] ?? false)) {
return $payload;
}
if (isset($payload['recipients'])
&& is_array($payload['recipients'])) {
foreach ($payload['recipients'] as &$recipient) {
$email = (string) ($recipient['email'] ?? '');
$parts = explode('@', $email, 2);
$recipient['email'] = count($parts) === 2
? substr($parts[0], 0, 1) . '***@' . $parts[1]
: '***';
}
unset($recipient);
}
if (isset($payload['instances']) && is_array($payload['instances'])) {
foreach ($payload['instances'] as &$instance) {
$instance['ipv4'] = self::maskIp(
(string) ($instance['ipv4'] ?? '')
);
$instance['ipv6'] = self::maskIp(
(string) ($instance['ipv6'] ?? '')
);
unset($instance['evidence']);
}
unset($instance);
}
return $payload;
}
private static function maskIp(string $value): string
{
if (filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$parts = explode('.', $value);
$parts[3] = '*';
return implode('.', $parts);
}
if (filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
$groups = explode(':', $value);
return implode(':', array_slice($groups, 0, 2)) . ':*';
}
return $value === '' ? '' : '*';
}
}