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 CloudServiceCompensationService())->page( $request->getQueryParams() ) ); } public function detail($request, $response, $args) { $actor = $this->actor($request); $payload = (new CloudServiceCompensationService())->detail( (string) ($args['compensation_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 CloudServiceCompensationService())->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', 'incident_key', 'reason_text', 'incident_starts_at', 'incident_ends_at', 'compensation_seconds', 'notify_users', 'audience_digest', ]); $payload = (new CloudServiceCompensationService())->create( (int) $actor['user_id'], (string) $actor['request_idempotency_key'], $body ); return $this->json($response, $payload, 201); } 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); } return $payload; } }