7aa8a27b1b
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
84 lines
2.8 KiB
PHP
84 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KTXC\Service;
|
|
|
|
use KTXC\Context\IdentityContextInterface;
|
|
use KTXC\Context\TenantContextInterface;
|
|
use KTXC\Models\Firewall\FirewallRuleObject;
|
|
|
|
final class TenantFirewallRuleService
|
|
{
|
|
public const PERMISSION_READ = 'firewall.tenant.rules.read';
|
|
public const PERMISSION_MANAGE = 'firewall.tenant.rules.manage';
|
|
|
|
public function __construct(
|
|
private readonly FirewallRuleManager $rules,
|
|
private readonly TenantContextInterface $tenant,
|
|
private readonly IdentityContextInterface $identity,
|
|
) {
|
|
}
|
|
|
|
public function listRules(bool $activeOnly = true): array
|
|
{
|
|
$this->requirePermission(self::PERMISSION_READ);
|
|
return $this->rules->list($this->scope(), $activeOnly);
|
|
}
|
|
|
|
public function blockIp(string $ip, ?string $reason = null, ?int $durationSeconds = null): FirewallRuleObject
|
|
{
|
|
$this->requirePermission(self::PERMISSION_MANAGE);
|
|
return $this->rules->blockIp(
|
|
$this->scope(), $ip, $reason, $this->identity->identifier(), $durationSeconds
|
|
);
|
|
}
|
|
|
|
public function allowIp(string $ip, ?string $reason = null): FirewallRuleObject
|
|
{
|
|
$this->requirePermission(self::PERMISSION_MANAGE);
|
|
return $this->rules->allowIp($this->scope(), $ip, $reason, $this->identity->identifier());
|
|
}
|
|
|
|
public function blockIpRange(string $cidr, ?string $reason = null): FirewallRuleObject
|
|
{
|
|
$this->requirePermission(self::PERMISSION_MANAGE);
|
|
return $this->rules->blockIpRange($this->scope(), $cidr, $reason, $this->identity->identifier());
|
|
}
|
|
|
|
public function blockDevice(
|
|
string $fingerprint,
|
|
?string $reason = null,
|
|
?int $durationSeconds = null
|
|
): FirewallRuleObject {
|
|
$this->requirePermission(self::PERMISSION_MANAGE);
|
|
return $this->rules->blockDevice(
|
|
$this->scope(), $fingerprint, $reason, $this->identity->identifier(), $durationSeconds
|
|
);
|
|
}
|
|
|
|
public function disableRule(string $ruleId): bool
|
|
{
|
|
$this->requirePermission(self::PERMISSION_MANAGE);
|
|
return $this->rules->disable($this->scope(), $ruleId, $this->identity->identifier());
|
|
}
|
|
|
|
public function removeRule(string $ruleId): bool
|
|
{
|
|
$this->requirePermission(self::PERMISSION_MANAGE);
|
|
return $this->rules->remove($this->scope(), $ruleId, $this->identity->identifier());
|
|
}
|
|
|
|
private function scope(): FirewallRuleScope
|
|
{
|
|
return FirewallRuleScope::tenant($this->tenant->requireIdentifier());
|
|
}
|
|
|
|
private function requirePermission(string $permission): void
|
|
{
|
|
if (!$this->identity->hasPermission($permission)) {
|
|
throw new \RuntimeException("Missing required permission: {$permission}");
|
|
}
|
|
}
|
|
}
|