Respect user isolation in global stats queries

Updated LayoutHelper::getGlobalStats to accept a user ID and apply user isolation mode when querying domain statistics. DashboardController and base layout now pass the user ID to ensure stats are scoped per user when isolation is enabled.
This commit is contained in:
Hosteroid
2025-10-20 17:51:27 +03:00
parent 111698cfed
commit 011fab095e
3 changed files with 37 additions and 15 deletions

View File

@@ -35,34 +35,55 @@ class LayoutHelper
}
/**
* Get global stats for sidebar
* Get stats for sidebar (respects user isolation)
*/
public static function getGlobalStats(): array
public static function getGlobalStats(?int $userId = null): array
{
try {
$pdo = \Core\Database::getConnection();
// Check isolation mode
$settingModel = new Setting();
$isolationMode = $settingModel->getValue('user_isolation_mode', 'shared');
// Build WHERE clause based on isolation mode
$whereClause = '';
$params = [];
if ($isolationMode === 'isolated' && $userId) {
$whereClause = ' WHERE user_id = ?';
$params[] = $userId;
}
// Get total domains
$totalStmt = $pdo->query("SELECT COUNT(*) as count FROM domains");
$totalSql = "SELECT COUNT(*) as count FROM domains" . $whereClause;
$totalStmt = $pdo->prepare($totalSql);
$totalStmt->execute($params);
$total = $totalStmt->fetch(\PDO::FETCH_ASSOC)['count'] ?? 0;
// Get active domains
$activeStmt = $pdo->query("SELECT COUNT(*) as count FROM domains WHERE is_active = 1");
$activeSql = "SELECT COUNT(*) as count FROM domains WHERE is_active = 1" . $whereClause;
$activeStmt = $pdo->prepare($activeSql);
$activeStmt->execute($params);
$active = $activeStmt->fetch(\PDO::FETCH_ASSOC)['count'] ?? 0;
// Get expiring soon
$settingModel = new Setting();
$notificationDays = $settingModel->getNotificationDays();
$threshold = !empty($notificationDays) ? max($notificationDays) : 30;
$expiringSoonStmt = $pdo->prepare(
"SELECT COUNT(*) as count FROM domains
WHERE is_active = 1
AND expiration_date IS NOT NULL
AND expiration_date <= DATE_ADD(NOW(), INTERVAL ? DAY)
AND expiration_date >= NOW()"
);
$expiringSoonStmt->execute([$threshold]);
$expiringSql = "SELECT COUNT(*) as count FROM domains
WHERE is_active = 1
AND expiration_date IS NOT NULL
AND expiration_date <= DATE_ADD(NOW(), INTERVAL ? DAY)
AND expiration_date >= NOW()" . $whereClause;
$expiringParams = [$threshold];
if ($isolationMode === 'isolated' && $userId) {
$expiringParams[] = $userId;
}
$expiringSoonStmt = $pdo->prepare($expiringSql);
$expiringSoonStmt->execute($expiringParams);
$expiringSoon = $expiringSoonStmt->fetch(\PDO::FETCH_ASSOC)['count'] ?? 0;
return [