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.
108 lines
3.3 KiB
PHP
108 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
/** Administrator-facing application boundary for the immutable policy. */
|
|
final class CloudAdminAbusePolicyService
|
|
{
|
|
public function __construct(
|
|
private readonly ?CloudAbusePolicyService $policies = null
|
|
) {
|
|
}
|
|
|
|
/** @param array<string,mixed> $actor @return array<string,mixed> */
|
|
public function current(array $actor): array
|
|
{
|
|
self::assertAdministrator($actor);
|
|
return [
|
|
'active' => $this->policyService()->activeSnapshot()->toArray(),
|
|
'can_manage' => self::canManage($actor),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param array<string,mixed> $actor
|
|
* @param array<string,mixed> $definition
|
|
* @return array<string,mixed>
|
|
*/
|
|
public function preview(
|
|
array $actor,
|
|
int $expectedRevision,
|
|
array $definition
|
|
): array {
|
|
self::assertAdministrator($actor);
|
|
CloudAccess::manage($actor);
|
|
$current = $this->policyService()->activeSnapshot();
|
|
if ($current->revision !== $expectedRevision) {
|
|
throw new CloudError(
|
|
'网络滥用检测策略已被其他管理员更新,请刷新后重试',
|
|
409
|
|
);
|
|
}
|
|
try {
|
|
$candidate = CloudAbuseDetectionPolicy::fromDefinition($definition);
|
|
} catch (\InvalidArgumentException $error) {
|
|
throw new CloudError($error->getMessage(), 422);
|
|
}
|
|
$changed = !hash_equals($current->policyHash, $candidate->hash());
|
|
if ($changed && $current->revision === PHP_INT_MAX) {
|
|
throw new CloudError('网络滥用检测策略版本已耗尽', 409);
|
|
}
|
|
return [
|
|
'current' => $current->toArray(),
|
|
'candidate' => [
|
|
'policy_hash' => $candidate->hash(),
|
|
'definition' => $candidate->definition(),
|
|
],
|
|
'changed' => $changed,
|
|
'result_revision' => $changed
|
|
? $current->revision + 1 : $current->revision,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param array<string,mixed> $actor
|
|
* @param array<string,mixed> $definition
|
|
* @return array<string,mixed>
|
|
*/
|
|
public function publish(
|
|
array $actor,
|
|
int $expectedRevision,
|
|
array $definition
|
|
): array {
|
|
self::assertAdministrator($actor);
|
|
CloudAccess::manage($actor);
|
|
$snapshot = $this->policyService()->publish(
|
|
$definition,
|
|
$expectedRevision,
|
|
(int) ($actor['user_id'] ?? 0)
|
|
);
|
|
return [
|
|
'active' => $snapshot->toArray(),
|
|
'published' => $snapshot->revision !== $expectedRevision,
|
|
'can_manage' => true,
|
|
];
|
|
}
|
|
|
|
/** @param array<string,mixed> $actor */
|
|
private static function assertAdministrator(array $actor): void
|
|
{
|
|
if (!($actor['is_admin'] ?? false)) {
|
|
throw new CloudError('需要管理员权限', 403);
|
|
}
|
|
}
|
|
|
|
/** @param array<string,mixed> $actor */
|
|
private static function canManage(array $actor): bool
|
|
{
|
|
return ($actor['is_admin'] ?? false) === true
|
|
&& ($actor['is_super_admin'] ?? false) === true
|
|
&& ($actor['can_manage'] ?? false) === true;
|
|
}
|
|
|
|
private function policyService(): CloudAbusePolicyService
|
|
{
|
|
return $this->policies ?? new CloudAbusePolicyService();
|
|
}
|
|
}
|