Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,28 @@ function intropage_device_scope($user_id): array {
return ['simple' => false, 'allowed' => intropage_get_allowed_devices($user_id)];
}

/**
* Resolve a user's device scope together with the host condition a panel
* appends to its queries.
*
* Builds on intropage_device_scope(). 'cond' is the "<column> IN (...)" text
* for a restricted user and an empty string when the user sees every device or
* has no visible devices (callers check 'allowed' before running the query).
*
* @param int|string $user_id Cacti user id whose permissions to resolve.
* @param string $column Column expression with any leading keyword,
* for example "AND host.id" or "WHERE id".
*
* @return array{simple: bool, allowed: string|false, cond: string}
*/
function intropage_device_filter($user_id, string $column): array {
$scope = intropage_device_scope($user_id);

$scope['cond'] = $scope['simple'] ? '' : $column . ' IN (' . $scope['allowed'] . ')';

return $scope;
}

/**
* Top-level request dispatcher for this page: sets the default action,
* then routes to the appropriate handler based on which request
Expand Down
29 changes: 10 additions & 19 deletions panellib/analyze.php
Original file line number Diff line number Diff line change
Expand Up @@ -626,14 +626,11 @@ function analyse_tree_host_graph($panel, $user_id): void {
$total_errors = 0;
$data = [];

$simple_perms = get_simple_device_perms($user_id);
['simple' => $simple_perms, 'allowed' => $allowed_devices] = intropage_device_scope($user_id);
$q_host_cond = '';

if (!$simple_perms) {
$allowed_devices = intropage_get_allowed_devices($user_id);
$host_cond = ' IN (' . $allowed_devices . ')';
} else {
$allowed_devices = false;
$q_host_cond = '';
$host_cond = ' IN (' . $allowed_devices . ')';
}

if ($allowed_devices !== false || $simple_perms) {
Expand Down Expand Up @@ -1565,25 +1562,19 @@ function analyse_tree_host_graph_detail(): array {
global $config, $console_access;

if (isset($_SESSION['sess_user_id'])) {
$simple_perms = get_simple_device_perms($_SESSION['sess_user_id']);
['simple' => $simple_perms, 'allowed' => $allowed_devices] = intropage_device_scope($_SESSION['sess_user_id']);
$q_host_cond = '';

if (!$simple_perms) {
$allowed_devices = intropage_get_allowed_devices($_SESSION['sess_user_id']);
$host_cond = ' IN (' . $allowed_devices . ')';
} else {
$allowed_devices = false;
$q_host_cond = '';
$host_cond = ' IN (' . $allowed_devices . ')';
}
} else {
$admin_user = read_config_option('admin_user');
$simple_perms = get_simple_device_perms($admin_user);
$admin_user = read_config_option('admin_user');
['simple' => $simple_perms, 'allowed' => $allowed_devices] = intropage_device_scope($admin_user);
$q_host_cond = '';

if (!$simple_perms) {
$allowed_devices = intropage_get_allowed_devices($admin_user);
$host_cond = ' IN (' . $allowed_devices . ')';
} else {
$allowed_devices = false;
$q_host_cond = '';
$host_cond = ' IN (' . $allowed_devices . ')';
}
}

Expand Down
94 changes: 7 additions & 87 deletions panellib/graphs.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,7 @@ function register_graphs(): array {
function graph_data_source($panel, $user_id): void {
global $config, $input_types, $run_from_poller;

$simple_perms = get_simple_device_perms($user_id);

if (!$simple_perms) {
$allowed_devices = intropage_get_allowed_devices($user_id);
$host_cond = 'IN (' . $allowed_devices . ')';
} else {
$allowed_devices = false;
$q_host_cond = '';
}
['simple' => $simple_perms, 'allowed' => $allowed_devices, 'cond' => $q_host_cond] = intropage_device_filter($user_id, 'AND data_local.host_id');

if ($allowed_devices !== false || $simple_perms) {
$graph = [
Expand All @@ -140,10 +132,6 @@ function graph_data_source($panel, $user_id): void {
],
];

if (!$simple_perms) {
$q_host_cond = 'AND data_local.host_id ' . $host_cond;
}

$sql_ds = db_fetch_assoc("SELECT data_input.type_id, COUNT(data_input.type_id) AS total
FROM data_local
INNER JOIN data_template_data
Expand Down Expand Up @@ -195,15 +183,7 @@ function graph_data_source($panel, $user_id): void {
function graph_host_template($panel, $user_id): void {
global $config;

$simple_perms = get_simple_device_perms($user_id);

if (!$simple_perms) {
$allowed_devices = intropage_get_allowed_devices($user_id);
$host_cond = 'IN (' . $allowed_devices . ')';
} else {
$allowed_devices = false;
$q_host_cond = '';
}
['simple' => $simple_perms, 'allowed' => $allowed_devices, 'cond' => $q_host_cond] = intropage_device_filter($user_id, 'WHERE host.id');

if (defined('CACTI_VERSION')) {
$cv = CACTI_VERSION;
Expand All @@ -212,10 +192,6 @@ function graph_host_template($panel, $user_id): void {
}

if ($allowed_devices !== false || $simple_perms) {
if (!$simple_perms) {
$q_host_cond = 'WHERE host.id ' . $host_cond;
}

// 1.2.24+ has billboard.js with treemap
if (cacti_version_compare($cv, '1.2.24', '>=')) {
$graph = [
Expand Down Expand Up @@ -304,15 +280,7 @@ function graph_host($panel, $user_id, $timespan = 0): void {

$panel['alarm'] = 'green';

$simple_perms = get_simple_device_perms($user_id);

if (!$simple_perms) {
$allowed_devices = intropage_get_allowed_devices($user_id);
$host_cond = 'IN (' . $allowed_devices . ')';
} else {
$allowed_devices = false;
$q_host_cond = '';
}
['simple' => $simple_perms, 'allowed' => $allowed_devices] = intropage_device_scope($user_id);

if ($allowed_devices !== false || $simple_perms) {
$graph = [
Expand Down Expand Up @@ -453,19 +421,7 @@ function graph_data_source_detail(): array {
'detail' => ''
];

$simple_perms = get_simple_device_perms($_SESSION['sess_user_id']);

if (!$simple_perms) {
$allowed_devices = intropage_get_allowed_devices($_SESSION['sess_user_id']);
$host_cond = 'IN (' . $allowed_devices . ')';
} else {
$allowed_devices = false;
$q_host_cond = '';
}

if (!$simple_perms) {
$q_host_cond = 'AND dl.host_id ' . $host_cond;
}
['simple' => $simple_perms, 'allowed' => $allowed_devices, 'cond' => $q_host_cond] = intropage_device_filter($_SESSION['sess_user_id'], 'AND dl.host_id');

$sql_ds = db_fetch_assoc("SELECT di.type_id, COUNT(di.type_id) AS total
FROM data_local AS dl
Expand Down Expand Up @@ -532,19 +488,7 @@ function graph_host_detail(): array {
'detail' => '',
];

$simple_perms = get_simple_device_perms($_SESSION['sess_user_id']);

if (!$simple_perms) {
$allowed_devices = intropage_get_allowed_devices($_SESSION['sess_user_id']);
$host_cond = 'IN (' . $allowed_devices . ')';
} else {
$allowed_devices = false;
$q_host_cond = '';
}

if (!$simple_perms) {
$q_host_cond = 'AND id ' . $host_cond;
}
['simple' => $simple_perms, 'allowed' => $allowed_devices, 'cond' => $q_host_cond] = intropage_device_filter($_SESSION['sess_user_id'], 'AND id');

$h_all = db_fetch_cell("SELECT COUNT(id) FROM host WHERE $q_host_cond");

Expand Down Expand Up @@ -689,19 +633,7 @@ function graph_host_template_detail(): array {
'detail' => '',
];

$simple_perms = get_simple_device_perms($_SESSION['sess_user_id']);

if (!$simple_perms) {
$allowed_devices = intropage_get_allowed_devices($_SESSION['sess_user_id']);
$host_cond = 'IN (' . $allowed_devices . ')';
} else {
$allowed_devices = false;
$q_host_cond = '';
}

if (!$simple_perms) {
$q_host_cond = 'WHERE host.id ' . $host_cond;
}
['simple' => $simple_perms, 'allowed' => $allowed_devices, 'cond' => $q_host_cond] = intropage_device_filter($_SESSION['sess_user_id'], 'WHERE host.id');

$rows = db_fetch_assoc("SELECT host_template.id as id, name, COUNT(host.host_template_id) AS total
FROM host_template
Expand Down Expand Up @@ -760,19 +692,7 @@ function host_collect(): void {
$users = get_user_list();

foreach ($users as $user) {
$simple_perms = get_simple_device_perms($user['id']);

if (!$simple_perms) {
$allowed_devices = intropage_get_allowed_devices($user['id']);
$host_cond = 'IN (' . $allowed_devices . ')';
} else {
$allowed_devices = false;
$q_host_cond = '';
}

if (!$simple_perms) {
$q_host_cond = 'AND id ' . $host_cond;
}
['simple' => $simple_perms, 'allowed' => $allowed_devices, 'cond' => $q_host_cond] = intropage_device_filter($user['id'], 'AND id');

if ($allowed_devices !== false || $simple_perms) {
db_execute_prepared("INSERT INTO plugin_intropage_trends
Expand Down
14 changes: 1 addition & 13 deletions panellib/system.php
Original file line number Diff line number Diff line change
Expand Up @@ -662,19 +662,7 @@ function extrem_trend(): void {

foreach ($users as $user) {
if (is_panel_allowed('extrem', $user['id'])) {
$simple_perms = get_simple_device_perms($user['id']);

if (!$simple_perms) {
$allowed_devices = intropage_get_allowed_devices($user['id']);
$host_cond = 'IN (' . $allowed_devices . ')';
} else {
$allowed_devices = false;
$q_host_cond = '';
}

if (!$simple_perms) {
$q_host_cond = 'WHERE id ' . $host_cond;
}
['simple' => $simple_perms, 'allowed' => $allowed_devices, 'cond' => $q_host_cond] = intropage_device_filter($user['id'], 'WHERE id');

if ($allowed_devices !== false || $simple_perms) {
$count = db_fetch_cell("SELECT SUM(failed_polls)
Expand Down
Loading
Loading