feat(firewall): add scoped rule administration reads
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -46,6 +46,10 @@ class FirewallStore
|
||||
['scope' => 1, 'tenantId' => 1, 'type' => 1, 'value' => 1, 'action' => 1, 'enabled' => 1, 'expiresAt' => 1],
|
||||
['name' => 'rules_exact_lookup']
|
||||
),
|
||||
$rules->createIndex(
|
||||
['scope' => 1, 'tenantId' => 1, 'createdAt' => -1],
|
||||
['name' => 'rules_browse']
|
||||
),
|
||||
$logs->createIndex(
|
||||
['tenantId' => 1, 'ipAddress' => 1, 'eventType' => 1, 'timestamp' => -1],
|
||||
['name' => 'logs_auth_failures']
|
||||
@@ -73,6 +77,61 @@ class FirewallStore
|
||||
// Rule Operations
|
||||
// ========================================
|
||||
|
||||
/**
|
||||
* Query rules within one ownership scope.
|
||||
*
|
||||
* @return array{items: FirewallRuleObject[], total: int, limit: int, offset: int}
|
||||
*/
|
||||
public function queryRules(
|
||||
string $scope,
|
||||
?string $tenantId,
|
||||
string $status,
|
||||
?string $type,
|
||||
?string $action,
|
||||
int $limit,
|
||||
int $offset
|
||||
): array {
|
||||
$filter = [
|
||||
'scope' => $scope,
|
||||
'tenantId' => $scope === FirewallRuleObject::SCOPE_SYSTEM ? null : $tenantId,
|
||||
];
|
||||
$now = self::bsonDate(new \DateTimeImmutable());
|
||||
if ($status === 'active') {
|
||||
$filter['enabled'] = true;
|
||||
$filter['$or'] = [
|
||||
['expiresAt' => null],
|
||||
['expiresAt' => ['$gt' => $now]],
|
||||
];
|
||||
} elseif ($status === 'disabled') {
|
||||
$filter['enabled'] = false;
|
||||
} elseif ($status === 'expired') {
|
||||
$filter['expiresAt'] = ['$ne' => null, '$lte' => $now];
|
||||
}
|
||||
if ($type !== null) {
|
||||
$filter['type'] = $type;
|
||||
}
|
||||
if ($action !== null) {
|
||||
$filter['action'] = $action;
|
||||
}
|
||||
|
||||
$collection = $this->dataStore->selectCollection(self::RULES_COLLECTION);
|
||||
$items = [];
|
||||
foreach ($collection->find($filter, [
|
||||
'sort' => ['createdAt' => -1, '_id' => -1],
|
||||
'limit' => $limit,
|
||||
'skip' => $offset,
|
||||
]) as $entry) {
|
||||
$items[] = (new FirewallRuleObject())->jsonDeserialize((array)$entry);
|
||||
}
|
||||
|
||||
return [
|
||||
'items' => $items,
|
||||
'total' => $collection->countDocuments($filter),
|
||||
'limit' => $limit,
|
||||
'offset' => $offset,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* List all rules for a tenant
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user