refactor(security): type firewall rule lifecycle events
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXC\Security\Event;
|
||||
|
||||
final class FirewallRuleCreatedEvent extends FirewallRuleEvent
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXC\Security\Event;
|
||||
|
||||
final class FirewallRuleDisabledEvent extends FirewallRuleEvent
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXC\Security\Event;
|
||||
|
||||
final class FirewallRuleEnabledEvent extends FirewallRuleEvent
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXC\Security\Event;
|
||||
|
||||
use KTXC\Models\Firewall\FirewallRuleObject;
|
||||
use KTXF\Event\Event;
|
||||
|
||||
abstract class FirewallRuleEvent extends Event implements SecurityEventInterface
|
||||
{
|
||||
final protected function __construct(
|
||||
private readonly string $ruleId,
|
||||
private readonly string $ruleScope,
|
||||
private readonly string $ruleType,
|
||||
private readonly string $ruleAction,
|
||||
private readonly string $ruleValue,
|
||||
private readonly ?string $reason,
|
||||
private readonly string $origin,
|
||||
private readonly ?string $expiresAt,
|
||||
private readonly array $details,
|
||||
?string $tenantId,
|
||||
?string $identityId,
|
||||
) {
|
||||
if ($ruleId === '') {
|
||||
throw new \InvalidArgumentException('Firewall rule events require a rule ID.');
|
||||
}
|
||||
foreach ([
|
||||
'scope' => $ruleScope,
|
||||
'type' => $ruleType,
|
||||
'action' => $ruleAction,
|
||||
'value' => $ruleValue,
|
||||
'origin' => $origin,
|
||||
] as $field => $value) {
|
||||
if ($value === '') {
|
||||
throw new \InvalidArgumentException("Firewall rule events require a rule {$field}.");
|
||||
}
|
||||
}
|
||||
foreach ([
|
||||
'scope' => $ruleScope,
|
||||
'type' => $ruleType,
|
||||
'action' => $ruleAction,
|
||||
'value' => $ruleValue,
|
||||
'origin' => $origin,
|
||||
] as $field => $value) {
|
||||
if ($value === '') {
|
||||
throw new \InvalidArgumentException("Firewall rule events require a rule {$field}.");
|
||||
}
|
||||
}
|
||||
|
||||
parent::__construct(
|
||||
static::class,
|
||||
[
|
||||
'ruleId' => $ruleId,
|
||||
'ruleScope' => $ruleScope,
|
||||
'ruleType' => $ruleType,
|
||||
'ruleAction' => $ruleAction,
|
||||
'ruleValue' => $ruleValue,
|
||||
'reason' => $reason,
|
||||
'origin' => $origin,
|
||||
'expiresAt' => $expiresAt,
|
||||
...$details,
|
||||
],
|
||||
$tenantId,
|
||||
$identityId,
|
||||
);
|
||||
}
|
||||
|
||||
public static function fromRule(
|
||||
FirewallRuleObject $rule,
|
||||
?string $actorId = null,
|
||||
array $change = [],
|
||||
): static {
|
||||
$metadata = $rule->getMetadata() ?? [];
|
||||
$details = [...$metadata, ...$change];
|
||||
foreach ([
|
||||
'ruleId',
|
||||
'ruleScope',
|
||||
'ruleType',
|
||||
'ruleAction',
|
||||
'ruleValue',
|
||||
'reason',
|
||||
'origin',
|
||||
'expiresAt',
|
||||
] as $reservedKey) {
|
||||
unset($details[$reservedKey]);
|
||||
}
|
||||
|
||||
return new static(
|
||||
ruleId: (string) $rule->getId(),
|
||||
ruleScope: (string) $rule->getScope(),
|
||||
ruleType: (string) $rule->getType(),
|
||||
ruleAction: (string) $rule->getAction(),
|
||||
ruleValue: (string) $rule->getValue(),
|
||||
reason: $rule->getReason(),
|
||||
origin: (string) ($metadata['origin'] ?? 'manual'),
|
||||
expiresAt: $rule->getExpiresAt()?->format(\DateTimeInterface::ATOM),
|
||||
details: $details,
|
||||
tenantId: $rule->getTenantId(),
|
||||
identityId: $actorId ?? $rule->getCreatedBy(),
|
||||
);
|
||||
}
|
||||
|
||||
public function getRuleId(): string
|
||||
{
|
||||
return $this->ruleId;
|
||||
}
|
||||
|
||||
public function getRuleScope(): string
|
||||
{
|
||||
return $this->ruleScope;
|
||||
}
|
||||
|
||||
public function getRuleType(): string
|
||||
{
|
||||
return $this->ruleType;
|
||||
}
|
||||
|
||||
public function getRuleAction(): string
|
||||
{
|
||||
return $this->ruleAction;
|
||||
}
|
||||
|
||||
public function getRuleValue(): string
|
||||
{
|
||||
return $this->ruleValue;
|
||||
}
|
||||
|
||||
public function getOrigin(): string
|
||||
{
|
||||
return $this->origin;
|
||||
}
|
||||
|
||||
public function getExpiresAt(): ?string
|
||||
{
|
||||
return $this->expiresAt;
|
||||
}
|
||||
|
||||
public function getDetails(): array
|
||||
{
|
||||
return $this->details;
|
||||
}
|
||||
|
||||
public function getUserId(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getReason(): ?string
|
||||
{
|
||||
return $this->reason;
|
||||
}
|
||||
|
||||
public function getSeverity(): int
|
||||
{
|
||||
return SecurityEvent::SEVERITY_INFO;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXC\Security\Event;
|
||||
|
||||
final class FirewallRuleExtendedEvent extends FirewallRuleEvent
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXC\Security\Event;
|
||||
|
||||
final class FirewallRuleRemovedEvent extends FirewallRuleEvent
|
||||
{
|
||||
}
|
||||
@@ -21,11 +21,6 @@ final class SecurityEvent extends Event implements SecurityRequestEventInterface
|
||||
public const IP_BLOCKED = 'security.ip.blocked';
|
||||
public const IP_ALLOWED = 'security.ip.allowed';
|
||||
public const DEVICE_BLOCKED = 'security.device.blocked';
|
||||
public const FIREWALL_RULE_CREATED = 'security.firewall.rule.created';
|
||||
public const FIREWALL_RULE_EXTENDED = 'security.firewall.rule.extended';
|
||||
public const FIREWALL_RULE_ENABLED = 'security.firewall.rule.enabled';
|
||||
public const FIREWALL_RULE_DISABLED = 'security.firewall.rule.disabled';
|
||||
public const FIREWALL_RULE_REMOVED = 'security.firewall.rule.removed';
|
||||
public const FIREWALL_SETTINGS_UPDATED = 'security.firewall.settings.updated';
|
||||
|
||||
// Severity levels
|
||||
|
||||
Reference in New Issue
Block a user