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:
@@ -20,17 +20,17 @@ class User
|
||||
|
||||
public function findById(int $id): ?array
|
||||
{
|
||||
return $this->db->fetch('SELECT * FROM users WHERE id = ?', [$id]);
|
||||
return $this->db->fetch('SELECT * FROM users WHERE id = ? AND status = \'active\'', [$id]);
|
||||
}
|
||||
|
||||
public function findByUsername(string $username): ?array
|
||||
{
|
||||
return $this->db->fetch('SELECT * FROM users WHERE username = ?', [$username]);
|
||||
return $this->db->fetch('SELECT * FROM users WHERE username = ? AND status = \'active\'', [$username]);
|
||||
}
|
||||
|
||||
public function findByEmail(string $email): ?array
|
||||
{
|
||||
return $this->db->fetch('SELECT * FROM users WHERE email = ?', [$email]);
|
||||
return $this->db->fetch('SELECT * FROM users WHERE email = ? AND status = \'active\'', [$email]);
|
||||
}
|
||||
|
||||
public function authenticate(string $username, string $password): ?array
|
||||
@@ -41,7 +41,7 @@ class User
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!$user['is_active']) {
|
||||
if ($user['status'] !== 'active' || !$user['is_active']) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -83,17 +83,21 @@ class User
|
||||
|
||||
public function delete(int $id): bool
|
||||
{
|
||||
return $this->db->delete('users', 'id = ?', [$id]) > 0;
|
||||
return $this->db->update('users', [
|
||||
'status' => 'deleted',
|
||||
'is_active' => 0,
|
||||
'updated_at' => date('Y-m-d H:i:s'),
|
||||
], 'id = ?', [$id]) > 0;
|
||||
}
|
||||
|
||||
public function getAll(int $page = 1, int $perPage = 20): array
|
||||
{
|
||||
$offset = ($page - 1) * $perPage;
|
||||
|
||||
$total = $this->db->fetch("SELECT COUNT(*) as total FROM users");
|
||||
$total = $this->db->fetch("SELECT COUNT(*) as total FROM users WHERE status = 'active'");
|
||||
$users = $this->db->fetchAll(
|
||||
'SELECT id, username, email, role, is_active, last_login_at, created_at
|
||||
FROM users ORDER BY created_at DESC LIMIT ? OFFSET ?',
|
||||
'SELECT id, username, email, role, status, is_active, last_login_at, created_at
|
||||
FROM users WHERE status = \'active\' ORDER BY created_at DESC LIMIT ? OFFSET ?',
|
||||
[$perPage, $offset]
|
||||
);
|
||||
|
||||
@@ -108,7 +112,7 @@ class User
|
||||
|
||||
public function findByApiToken(string $token): ?array
|
||||
{
|
||||
return $this->db->fetch('SELECT * FROM users WHERE api_token = ? AND is_active = 1', [$token]);
|
||||
return $this->db->fetch('SELECT * FROM users WHERE api_token = ? AND status = \'active\' AND is_active = 1', [$token]);
|
||||
}
|
||||
|
||||
public function updateLastLogin(int $id): void
|
||||
@@ -118,7 +122,7 @@ class User
|
||||
|
||||
public function countAll(): int
|
||||
{
|
||||
$result = $this->db->fetch("SELECT COUNT(*) as total FROM users");
|
||||
$result = $this->db->fetch("SELECT COUNT(*) as total FROM users WHERE status = 'active'");
|
||||
return (int) ($result['total'] ?? 0);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user