Add soft deletes via status column

- Add status ENUM('active','disabled','deleted') to 8 tables
- Change all physical DELETE to UPDATE status='deleted'
- All SELECT queries filter by status='active'
- toggleActive now alternates between active/disabled status
- Exceptions: monitoring_history, rate_limits, sessions keep physical purge
- audit_logs remains immutable
This commit is contained in:
2026-06-07 17:26:29 -04:00
parent 921d98bf60
commit 1741545318
6 changed files with 138 additions and 60 deletions

View File

@@ -23,7 +23,7 @@ class Server
public function findById(int $id): ?array
{
$server = $this->db->fetch('SELECT * FROM servers WHERE id = ?', [$id]);
$server = $this->db->fetch('SELECT * FROM servers WHERE id = ? AND status = \'active\'', [$id]);
if ($server) {
$server = $this->encryption->decryptArray($server, $this->encryptedFields);
}
@@ -32,11 +32,11 @@ class Server
public function findAll(bool $activeOnly = false): array
{
$sql = 'SELECT * FROM servers';
$sql = 'SELECT * FROM servers WHERE status = \'active\'';
$params = [];
if ($activeOnly) {
$sql .= ' WHERE is_active = 1';
$sql .= ' AND is_active = 1';
}
$sql .= ' ORDER BY name ASC';
@@ -49,7 +49,7 @@ class Server
public function getAll(int $page = 1, int $perPage = 20, array $filters = []): array
{
$where = [];
$where = ["s.status = 'active'"];
$params = [];
if (!empty($filters['search'])) {
@@ -162,18 +162,27 @@ class Server
public function delete(int $id): bool
{
return $this->db->delete('servers', 'id = ?', [$id]) > 0;
return $this->db->update('servers', [
'status' => 'deleted',
'is_active' => 0,
'updated_at' => date('Y-m-d H:i:s'),
], 'id = ?', [$id]) > 0;
}
public function toggleActive(int $id): ?bool
{
$server = $this->db->fetch('SELECT is_active FROM servers WHERE id = ?', [$id]);
$server = $this->db->fetch('SELECT is_active, status FROM servers WHERE id = ?', [$id]);
if (!$server) {
return null;
}
$newState = $server['is_active'] ? 0 : 1;
$this->db->update('servers', ['is_active' => $newState, 'updated_at' => date('Y-m-d H:i:s')], 'id = ?', [$id]);
$newStatus = $newState ? 'active' : 'disabled';
$this->db->update('servers', [
'is_active' => $newState,
'status' => $newStatus,
'updated_at' => date('Y-m-d H:i:s'),
], 'id = ?', [$id]);
return (bool) $newState;
}
@@ -181,28 +190,28 @@ class Server
public function getGroups(): array
{
$groups = $this->db->fetchAll(
'SELECT DISTINCT group_name FROM servers WHERE group_name IS NOT NULL AND group_name != "" ORDER BY group_name'
'SELECT DISTINCT group_name FROM servers WHERE status = \'active\' AND group_name IS NOT NULL AND group_name != "" ORDER BY group_name'
);
return array_column($groups, 'group_name');
}
public function countAll(): int
{
$result = $this->db->fetch("SELECT COUNT(*) as total FROM servers");
$result = $this->db->fetch("SELECT COUNT(*) as total FROM servers WHERE status = 'active'");
return (int) ($result['total'] ?? 0);
}
public function countOnline(): int
{
$result = $this->db->fetch(
"SELECT COUNT(*) as total FROM servers WHERE current_status = 'online' AND is_active = 1"
"SELECT COUNT(*) as total FROM servers WHERE current_status = 'online' AND status = 'active' AND is_active = 1"
);
return (int) ($result['total'] ?? 0);
}
public function getUserRole(int $serverId, int $userId): ?string
{
$server = $this->db->fetch('SELECT created_by FROM servers WHERE id = ?', [$serverId]);
$server = $this->db->fetch('SELECT created_by FROM servers WHERE id = ? AND status = \'active\'', [$serverId]);
if (!$server) {
return null;
}
@@ -212,14 +221,14 @@ class Server
}
$direct = $this->db->fetch(
'SELECT role FROM server_user WHERE server_id = ? AND user_id = ?',
'SELECT role FROM server_user WHERE server_id = ? AND user_id = ? AND status = \'active\'',
[$serverId, $userId]
);
$teamRole = $this->db->fetch(
'SELECT ts.role FROM team_user tu
JOIN team_server ts ON tu.team_id = ts.team_id
WHERE tu.user_id = ? AND ts.server_id = ?
WHERE tu.user_id = ? AND ts.server_id = ? AND tu.status = \'active\' AND ts.status = \'active\'
ORDER BY FIELD(ts.role, \'manager\', \'viewer\')
LIMIT 1',
[$userId, $serverId]
@@ -241,19 +250,19 @@ class Server
public function getAccessibleServerIds(int $userId): array
{
$owned = $this->db->fetchAll(
'SELECT id FROM servers WHERE created_by = ?',
'SELECT id FROM servers WHERE created_by = ? AND status = \'active\'',
[$userId]
);
$assigned = $this->db->fetchAll(
'SELECT server_id FROM server_user WHERE user_id = ?',
'SELECT server_id FROM server_user WHERE user_id = ? AND status = \'active\'',
[$userId]
);
$teamServerIds = $this->db->fetchAll(
'SELECT DISTINCT ts.server_id FROM team_user tu
JOIN team_server ts ON tu.team_id = ts.team_id
WHERE tu.user_id = ?',
WHERE tu.user_id = ? AND tu.status = \'active\' AND ts.status = \'active\'',
[$userId]
);
@@ -273,7 +282,7 @@ class Server
public function isOwner(int $serverId, int $userId): bool
{
$server = $this->db->fetch('SELECT created_by FROM servers WHERE id = ?', [$serverId]);
$server = $this->db->fetch('SELECT created_by FROM servers WHERE id = ? AND status = \'active\'', [$serverId]);
return $server && (int) $server['created_by'] === $userId;
}
@@ -294,7 +303,7 @@ class Server
'SELECT su.*, u.username, u.email
FROM server_user su
JOIN users u ON su.user_id = u.id
WHERE su.server_id = ?
WHERE su.server_id = ? AND su.status = \'active\' AND u.status = \'active\'
ORDER BY su.created_at ASC',
[$serverId]
);
@@ -303,7 +312,7 @@ class Server
'SELECT u.id, u.username, u.email
FROM servers s
JOIN users u ON s.created_by = u.id
WHERE s.id = ?',
WHERE s.id = ? AND s.status = \'active\' AND u.status = \'active\'',
[$serverId]
);
@@ -334,7 +343,7 @@ class Server
}
$existing = $this->db->fetch(
'SELECT id FROM server_user WHERE server_id = ? AND user_id = ?',
'SELECT id FROM server_user WHERE server_id = ? AND user_id = ? AND status = \'active\'',
[$serverId, $userId]
);
if ($existing) {
@@ -351,8 +360,9 @@ class Server
public function removeUser(int $serverId, int $userId): bool
{
return $this->db->delete(
return $this->db->update(
'server_user',
['status' => 'deleted'],
'server_id = ? AND user_id = ?',
[$serverId, $userId]
) > 0;
@@ -367,7 +377,7 @@ class Server
return $this->db->update(
'server_user',
['role' => $role],
'server_id = ? AND user_id = ?',
'server_id = ? AND user_id = ? AND status = \'active\'',
[$serverId, $userId]
) > 0;
}
@@ -375,7 +385,7 @@ class Server
public function getOwnedServers(int $userId): array
{
return $this->db->fetchAll(
'SELECT * FROM servers WHERE created_by = ? ORDER BY name ASC',
'SELECT * FROM servers WHERE created_by = ? AND status = \'active\' ORDER BY name ASC',
[$userId]
);
}