104 lines
3.9 KiB
PHP
104 lines
3.9 KiB
PHP
<?php
|
|
require_once '../include/init.php';
|
|
require_once '../include/auth.php';
|
|
|
|
// --- Security Check: Only Admin can access this API ---
|
|
if (!isAdmin()) {
|
|
http_response_code(403); // Forbidden
|
|
echo json_encode(['error' => 'Permission denied.']);
|
|
exit;
|
|
}
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
try {
|
|
$details = [];
|
|
|
|
// 1. Check current state
|
|
$stmt = $pdo->query("SELECT id, name, user_id FROM tabs ORDER BY id");
|
|
$all_tabs = $stmt->fetchAll();
|
|
|
|
$details[] = "Found " . count($all_tabs) . " total tabs in database";
|
|
|
|
// 2. Find tabs with empty string user_id (should be public)
|
|
$stmt = $pdo->query("SELECT id, name FROM tabs WHERE user_id = ''");
|
|
$empty_user_tabs = $stmt->fetchAll();
|
|
|
|
if (empty($empty_user_tabs)) {
|
|
$details[] = "No tabs found with empty string user_id - all tabs are properly configured";
|
|
$message = "No public tabs need fixing. All tabs are properly configured.";
|
|
} else {
|
|
$details[] = "Found " . count($empty_user_tabs) . " tab(s) with empty string user_id that should be public:";
|
|
foreach ($empty_user_tabs as $tab) {
|
|
$details[] = " - Tab ID {$tab['id']}: '{$tab['name']}'";
|
|
}
|
|
|
|
// 3. Fix the tabs
|
|
$stmt = $pdo->prepare("UPDATE tabs SET user_id = NULL WHERE user_id = ''");
|
|
$stmt->execute();
|
|
$affected_rows = $stmt->rowCount();
|
|
|
|
$details[] = "Updated {$affected_rows} tab(s) to be truly public (set user_id to NULL)";
|
|
|
|
// 4. Verify the fix
|
|
$stmt = $pdo->query("SELECT id, name FROM tabs WHERE user_id IS NULL");
|
|
$public_tabs = $stmt->fetchAll();
|
|
|
|
$details[] = "Verification: Found " . count($public_tabs) . " public tab(s) after fix:";
|
|
foreach ($public_tabs as $tab) {
|
|
$details[] = " - Tab ID {$tab['id']}: '{$tab['name']}' (PUBLIC)";
|
|
}
|
|
|
|
$message = "Successfully fixed {$affected_rows} public tab(s). Users and viewers can now see public tabs properly.";
|
|
}
|
|
|
|
// 5. Test visibility for a user (if users exist)
|
|
$stmt = $pdo->query("SELECT id, username, role FROM users WHERE role IN ('user', 'viewer') LIMIT 1");
|
|
$test_user = $stmt->fetch();
|
|
|
|
if ($test_user) {
|
|
$test_user_id = $test_user['id'];
|
|
$details[] = "Testing visibility for user: {$test_user['username']} (ID: {$test_user_id}, Role: {$test_user['role']})";
|
|
|
|
// Test the query
|
|
$tabs_stmt_sql = "SELECT DISTINCT t.* FROM tabs t
|
|
LEFT JOIN tab_viewer_access tva ON t.id = tva.tab_id
|
|
LEFT JOIN user_tab_visibility utv ON t.id = utv.tab_id AND utv.user_id = :current_user_id
|
|
WHERE utv.user_id IS NULL
|
|
AND (t.user_id IS NULL OR t.user_id = :current_user_id OR tva.user_id = :current_user_id)
|
|
ORDER BY t.id";
|
|
|
|
$tabs_stmt = $pdo->prepare($tabs_stmt_sql);
|
|
$tabs_stmt->execute([':current_user_id' => $test_user_id]);
|
|
$visible_tabs = $tabs_stmt->fetchAll();
|
|
|
|
$details[] = "Visible tabs for this user: " . count($visible_tabs);
|
|
foreach ($visible_tabs as $tab) {
|
|
$user_info = $tab['user_id'] === null ? 'PUBLIC' : "User ID: {$tab['user_id']}";
|
|
$details[] = " - Tab ID {$tab['id']}: '{$tab['name']}' ({$user_info})";
|
|
}
|
|
} else {
|
|
$details[] = "No users found to test visibility with";
|
|
}
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'message' => $message,
|
|
'details' => $details
|
|
]);
|
|
|
|
} catch (PDOException $e) {
|
|
http_response_code(500);
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => 'Database error: ' . $e->getMessage()
|
|
]);
|
|
} catch (Exception $e) {
|
|
http_response_code(500);
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => 'Error: ' . $e->getMessage()
|
|
]);
|
|
}
|
|
?>
|