diff --git a/pandora_console/extensions/agents_modules.php b/pandora_console/extensions/agents_modules.php index 8aa032c9a8..0a3cc2a382 100644 --- a/pandora_console/extensions/agents_modules.php +++ b/pandora_console/extensions/agents_modules.php @@ -668,7 +668,7 @@ function mainAgentsModules() if ($recursion) { $filter_groups['id_grupo'] = array_merge( $group_id, - groups_get_id_recursive($group_id, true) + groups_get_children_ids($group_id, true) ); } else { $filter_groups['id_grupo'] = $group_id; diff --git a/pandora_console/godmode/agentes/modificar_agente.php b/pandora_console/godmode/agentes/modificar_agente.php index d4036bd68c..8238d1636b 100644 --- a/pandora_console/godmode/agentes/modificar_agente.php +++ b/pandora_console/godmode/agentes/modificar_agente.php @@ -408,7 +408,7 @@ if ($ag_group > 0) { $ag_groups = []; $ag_groups = (array) $ag_group; if ($recursion) { - $ag_groups = groups_get_id_recursive($ag_group, true); + $ag_groups = groups_get_children_ids($ag_group, true); } $user_groups_to_sql = implode(',', $ag_groups); diff --git a/pandora_console/godmode/agentes/planned_downtime.editor.php b/pandora_console/godmode/agentes/planned_downtime.editor.php index b3e7029dbd..4aceac701e 100644 --- a/pandora_console/godmode/agentes/planned_downtime.editor.php +++ b/pandora_console/godmode/agentes/planned_downtime.editor.php @@ -877,7 +877,7 @@ if ($id_downtime > 0) { $filter_cond = ''; if ($filter_group > 0) { if ($recursion) { - $rg = groups_get_id_recursive($filter_group, true); + $rg = groups_get_children_ids($filter_group, true); $filter_cond .= ' AND id_grupo IN ('; $i = 0; diff --git a/pandora_console/godmode/massive/massive_delete_alerts.php b/pandora_console/godmode/massive/massive_delete_alerts.php index c9bcda46ba..c5407ed365 100755 --- a/pandora_console/godmode/massive/massive_delete_alerts.php +++ b/pandora_console/godmode/massive/massive_delete_alerts.php @@ -40,7 +40,7 @@ if (is_ajax()) { $keys_prefix = (string) get_parameter('keys_prefix', ''); if ($recursion) { - $groups = groups_get_id_recursive($id_group, true); + $groups = groups_get_children_ids($id_group, true); } else { $groups = [$id_group]; } diff --git a/pandora_console/godmode/reporting/map_builder.php b/pandora_console/godmode/reporting/map_builder.php index 9f8b8f0162..482b995f8f 100644 --- a/pandora_console/godmode/reporting/map_builder.php +++ b/pandora_console/godmode/reporting/map_builder.php @@ -278,7 +278,7 @@ if ($ag_group > 0) { $ag_groups = []; $ag_groups = (array) $ag_group; if ($recursion) { - $ag_groups = groups_get_id_recursive($ag_group, true); + $ag_groups = groups_get_children_ids($ag_group, true); } } diff --git a/pandora_console/godmode/reporting/visual_console_favorite.php b/pandora_console/godmode/reporting/visual_console_favorite.php index b9f4fe28d7..253218431f 100644 --- a/pandora_console/godmode/reporting/visual_console_favorite.php +++ b/pandora_console/godmode/reporting/visual_console_favorite.php @@ -143,7 +143,7 @@ if (!$own_info['is_admin'] && !check_acl($config['id_user'], 0, 'AW')) { $ag_groups = []; $ag_groups = (array) $ag_group; if ($recursion) { - $ag_groups = groups_get_id_recursive($ag_group, true); + $ag_groups = groups_get_children_ids($ag_group, true); } } else if ($own_info['is_admin']) { $returnAllGroups = 1; diff --git a/pandora_console/include/class/CredentialStore.class.php b/pandora_console/include/class/CredentialStore.class.php index 073f0831a3..d14314a6b4 100644 --- a/pandora_console/include/class/CredentialStore.class.php +++ b/pandora_console/include/class/CredentialStore.class.php @@ -248,7 +248,7 @@ class CredentialStore extends Wizard ); } else { $groups = [ $filter['filter_id_group'] ]; - $childrens = groups_get_childrens($id_group, null, true); + $childrens = groups_get_children($id_group, null, true); if (!empty($childrens)) { foreach ($childrens as $child) { $groups[] = (int) $child['id_grupo']; diff --git a/pandora_console/include/class/NetworkMap.class.php b/pandora_console/include/class/NetworkMap.class.php index a14e2b80c4..0eaea52134 100644 --- a/pandora_console/include/class/NetworkMap.class.php +++ b/pandora_console/include/class/NetworkMap.class.php @@ -810,7 +810,7 @@ class NetworkMap $filter['id_grupo'] = $this->idGroup; } else { // Show current group and children. - $childrens = groups_get_childrens($this->idGroup, null, true); + $childrens = groups_get_children($this->idGroup, null, true); if (!empty($childrens)) { $childrens = array_keys($childrens); diff --git a/pandora_console/include/functions_agents.php b/pandora_console/include/functions_agents.php index beff278dc1..8ba751042c 100644 --- a/pandora_console/include/functions_agents.php +++ b/pandora_console/include/functions_agents.php @@ -1108,11 +1108,14 @@ function agents_get_group_agents( foreach ($id_group as $parent) { $id_group = array_merge( $id_group, - groups_get_id_recursive($parent, false) + groups_get_children_ids($parent, $noACL) ); } } else { - $id_group = groups_get_id_recursive($id_group, false); + $id_group = array_merge( + [$id_group], + groups_get_children_ids($id_group, $noACL) + ); } // Check available groups for target user only if asking for 'All' group. diff --git a/pandora_console/include/functions_api.php b/pandora_console/include/functions_api.php index 6401910faa..863eaf0b3f 100644 --- a/pandora_console/include/functions_api.php +++ b/pandora_console/include/functions_api.php @@ -1973,7 +1973,7 @@ function api_get_all_agents($thrash1, $thrash2, $other, $returnType) $ag_groups = $other['data'][1]; // Recursion. if ($other['data'][6] === '1') { - $ag_groups = groups_get_id_recursive($ag_groups, true); + $ag_groups = groups_get_children_ids($ag_groups, true); } $ag_groups = implode(',', (array) $ag_groups); diff --git a/pandora_console/include/functions_custom_fields.php b/pandora_console/include/functions_custom_fields.php index d1f661e874..c15b0cae95 100644 --- a/pandora_console/include/functions_custom_fields.php +++ b/pandora_console/include/functions_custom_fields.php @@ -367,7 +367,7 @@ function agent_counters_custom_fields($filters) if ($filters['group']) { // Recursion check acl. if ($filters['recursion']) { - $recursion_groups = groups_get_id_recursive($filters['group'], true); + $recursion_groups = groups_get_children_ids($filters['group'], true); if (!users_can_manage_group_all('AR')) { if (isset($user_groups) && is_array($user_groups)) { $groups_intersect = array_intersect($user_groups, $recursion_groups); diff --git a/pandora_console/include/functions_events.php b/pandora_console/include/functions_events.php index 7a337fbdab..cf486b8acf 100644 --- a/pandora_console/include/functions_events.php +++ b/pandora_console/include/functions_events.php @@ -871,37 +871,23 @@ function events_get_all( } $groups = $filter['id_group_filter']; - if (isset($groups) && $groups > 0) { - $propagate = db_get_value( - 'propagate', - 'tgrupo', - 'id_grupo', - $groups - ); + if (isset($groups) === true && $groups > 0) { + $children = groups_get_children($groups); - if (!$propagate && isset($groups)) { - $sql_filters[] = sprintf( - ' AND (te.id_grupo = %d OR tasg.id_group = %d)', - $groups, - $groups - ); - } else { - $children = groups_get_children($groups); - $_groups = [ $groups ]; - if (!empty($children)) { - foreach ($children as $child) { - $_groups[] = (int) $child['id_grupo']; - } + $_groups = [ $groups ]; + if (empty($children) === false) { + foreach ($children as $child) { + $_groups[] = (int) $child['id_grupo']; } - - $groups = $_groups; - - $sql_filters[] = sprintf( - ' AND (te.id_grupo IN (%s) OR tasg.id_group IN (%s))', - join(',', $groups), - join(',', $groups) - ); } + + $groups = $_groups; + + $sql_filters[] = sprintf( + ' AND (te.id_grupo IN (%s) OR tasg.id_group IN (%s))', + join(',', $groups), + join(',', $groups) + ); } // Skip system messages if user is not PM. diff --git a/pandora_console/include/functions_groups.php b/pandora_console/include/functions_groups.php index b6c1a5e6c9..7b733b9c8d 100644 --- a/pandora_console/include/functions_groups.php +++ b/pandora_console/include/functions_groups.php @@ -265,49 +265,21 @@ function groups_check_used($idGroup) } -/** - * Return a array of id_group of childrens (to branches down) - * - * @param integer $parent The id_group parent to search the childrens. - * @param array $groups The groups, its for optimize the querys to DB. - */ -function groups_get_childrens_ids($parent, $groups=null) -{ - if (empty($groups)) { - $groups = db_get_all_rows_in_table('tgrupo'); - } - - $return = ''; - - foreach ($groups as $key => $group) { - if ($group['id_grupo'] == 0) { - continue; - } - - if ($group['parent'] == $parent) { - $return .= $group['id_grupo'].','; - $propagate = db_get_value('propagate', 'tgrupo', 'id_grupo', $group['id_grupo']); - if ($propagate) { - $return .= groups_get_childrens_ids($group['id_grupo']); - } - } - } - - return $return; -} - - /** * Return a array of id_group of children of given parent. * * @param integer $parent The id_grupo parent to search its children. * @param array $ignorePropagate Ignore propagate. + * @param string $privilege Default privilege. + * + * @return array Of Groups, children of $parent. */ -function groups_get_children($parent, $ignorePropagate=false) +function groups_get_children($parent, $ignorePropagate=false, $privilege='AR') { static $groups; + static $user_groups; - if (empty($groups)) { + if (empty($groups) === true) { $aux_groups = []; $groups = db_get_all_rows_in_table('tgrupo'); foreach ($groups as $key => $value) { @@ -317,13 +289,29 @@ function groups_get_children($parent, $ignorePropagate=false) $groups = $aux_groups; } + if (empty($user_groups) === true) { + $user_groups = users_get_groups(false, $privilege, true); + } + + // Admin see always all groups. + $ignorePropagate = users_is_admin() || $ignorePropagate; + $return = []; foreach ($groups as $key => $g) { if ($g['id_grupo'] == 0) { continue; } - if ($ignorePropagate || $parent == 0 || $groups[$parent]['propagate']) { + // IgnorePropagate will be true if user can access child. + $allowed = $ignorePropagate || array_key_exists( + $g['id_grupo'], + $user_groups + ); + + if ($allowed === true + || (int) $parent === 0 + || (bool) $groups[$parent]['propagate'] === true + ) { if ($g['parent'] == $parent) { $return += [$g['id_grupo'] => $g]; if ($g['propagate'] || $ignorePropagate) { @@ -340,38 +328,6 @@ function groups_get_children($parent, $ignorePropagate=false) } -/** - * @deprecated This is not working. Expects 'propagate' on CHILD not on PARENT!!! - * - * Return a array of id_group of childrens (to branches down) - * - * @param integer $parent The id_group parent to search the childrens. - * @param array $groups The groups, its for optimize the querys to DB. - */ -function groups_get_childrens($parent, $groups=null, $onlyPropagate=false) -{ - if (empty($groups)) { - $groups = db_get_all_rows_in_table('tgrupo'); - } - - $return = []; - - foreach ($groups as $key => $group) { - if ($group['id_grupo'] == 0) { - continue; - } - - if ($group['propagate'] || $onlyPropagate) { - if ($group['parent'] == $parent) { - $return = ($return + [$group['id_grupo'] => $group] + groups_get_childrens($group['id_grupo'], $groups, $onlyPropagate)); - } - } - } - - return $return; -} - - /** * Return a array of id_group of parents (to roots up). * @@ -541,35 +497,11 @@ function groups_get_all($groupWithAgents=false) * * @return array with all result groups */ -function groups_get_id_recursive($id_parent, $all=false) +function groups_get_children_ids($id_parent, $all=false) { - $return = []; + $return = groups_get_children($id_parent, $all); - $return = array_merge($return, [$id_parent]); - - // Check propagate - $propagate = db_get_value_filter('propagate', 'tgrupo', ['id_grupo' => $id_parent]); - - if (($propagate == 1) || $all) { - $children = db_get_all_rows_filter('tgrupo', ['parent' => $id_parent, 'disabled' => 0], ['id_grupo']); - - if ($children === false) { - $children = []; - } else { - $temp = []; - foreach ($children as $id_children) { - $temp = array_merge($temp, [$id_children['id_grupo']]); - } - - $children = $temp; - } - - foreach ($children as $id_children) { - $return = array_merge($return, groups_get_id_recursive($id_children, $all)); - } - } - - return $return; + return array_keys($return); } diff --git a/pandora_console/include/functions_networkmap.php b/pandora_console/include/functions_networkmap.php index 56172331c6..bd5152576d 100644 --- a/pandora_console/include/functions_networkmap.php +++ b/pandora_console/include/functions_networkmap.php @@ -319,7 +319,7 @@ function networkmap_generate_dot( if ($dont_show_subgroups) { $filter['id_grupo'] = $group; } else { - $childrens = groups_get_childrens($group, null, true); + $childrens = groups_get_children($group, null, true); if (!empty($childrens)) { $childrens = array_keys($childrens); diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php index 0bfe96e864..c480969f16 100755 --- a/pandora_console/include/functions_reporting.php +++ b/pandora_console/include/functions_reporting.php @@ -9072,7 +9072,7 @@ function reporting_get_group_stats($id_group=0, $access='AR') $covered_groups = []; $group_array = []; foreach ($id_group as $group) { - $children = groups_get_childrens($group); + $children = groups_get_children($group); // Show empty groups only if they have children with agents // $group_array = array(); diff --git a/pandora_console/include/functions_tags.php b/pandora_console/include/functions_tags.php index 5aace86006..fe36bc34fa 100644 --- a/pandora_console/include/functions_tags.php +++ b/pandora_console/include/functions_tags.php @@ -941,7 +941,7 @@ function tags_get_acl_tags_event_condition( } // Group condition (The module belongs to an agent of the group X) - // $group_condition = sprintf('id_grupo IN (%s)', implode(',', array_values(groups_get_id_recursive($group_id, true))));. + // $group_condition = sprintf('id_grupo IN (%s)', implode(',', array_values(groups_get_children_ids($group_id, true))));. $group_condition = '('.$id_grupo_table_pretag.'id_grupo = '.$group_id.' OR '.$alt_id_grupo_table_pretag.'id_group = '.$group_id.')'; // Tags condition (The module has at least one of the restricted tags). @@ -1350,7 +1350,7 @@ function tags_checks_event_acl($id_user, $id_group, $access, $tags=[], $children foreach ($user_tags as $user_tag) { $tags_user = $user_tag['tags']; $id_group_user = $user_tag['id_grupo']; - $childrens = groups_get_childrens($id_group_user, null, true); + $childrens = groups_get_children($id_group_user, null, true); if (empty($childrens)) { $group_ids = $id_group_user; @@ -1421,7 +1421,7 @@ function tags_get_agents_counter($id_tag, $groups_and_tags=[], $agent_filter=[], $tags_arr = explode(',', $tags); foreach ($tags_arr as $tag) { if ($tag == $id_tag) { - $hierarchy_groups = groups_get_id_recursive($group_id); + $hierarchy_groups = groups_get_children_ids($group_id); $groups_id = array_merge($groups_id, $hierarchy_groups); } } @@ -1923,7 +1923,7 @@ function tags_get_monitors_counter($id_tag, $groups_and_tags=[], $agent_filter=[ $tags_arr = explode(',', $tags); foreach ($tags_arr as $tag) { if ($tag == $id_tag) { - $hierarchy_groups = groups_get_id_recursive($group_id); + $hierarchy_groups = groups_get_children_ids($group_id); $groups_id = array_merge($groups_id, $hierarchy_groups); } } @@ -2219,7 +2219,7 @@ function tags_monitors_fired_alerts($id_tag, $groups_and_tags=[], $id_agente=fal $tags_arr = explode(',', $tags); foreach ($tags_arr as $tag) { if ($tag == $id_tag) { - $hierarchy_groups = groups_get_id_recursive($group_id); + $hierarchy_groups = groups_get_children_ids($group_id); $groups_id = array_merge($groups_id, $hierarchy_groups); } } @@ -2283,7 +2283,7 @@ function tags_get_monitors_alerts($id_tag, $groups_and_tags=[], $id_agente=false $tags_arr = explode(',', $tags); foreach ($tags_arr as $tag) { if ($tag == $id_tag) { - $hierarchy_groups = groups_get_id_recursive($group_id); + $hierarchy_groups = groups_get_children_ids($group_id); $groups_id = array_merge($groups_id, $hierarchy_groups); } } diff --git a/pandora_console/operation/agentes/estado_agente.php b/pandora_console/operation/agentes/estado_agente.php index c0e6d147c5..8144ea524a 100644 --- a/pandora_console/operation/agentes/estado_agente.php +++ b/pandora_console/operation/agentes/estado_agente.php @@ -546,7 +546,7 @@ if (!empty($search_custom)) { if ($group_id > 0) { $groups = [$group_id]; if ($recursion) { - $groups = groups_get_id_recursive($group_id, true); + $groups = groups_get_children_ids($group_id, true); } } else { $groups = []; @@ -576,7 +576,7 @@ if ($strict_user) { if ($group_id > 0) { $groups = [$group_id]; if ($recursion) { - $groups = groups_get_id_recursive($group_id, true); + $groups = groups_get_children_ids($group_id, true); } $filter['id_group'] = implode(',', $groups); diff --git a/pandora_console/operation/agentes/ver_agente.php b/pandora_console/operation/agentes/ver_agente.php index e86960938a..eb4a5186bf 100644 --- a/pandora_console/operation/agentes/ver_agente.php +++ b/pandora_console/operation/agentes/ver_agente.php @@ -146,14 +146,24 @@ if (is_ajax()) { // Perform search. $agents = agents_get_group_agents( + // Id_group. $id_group, + // Search. $filter, + // Case. 'lower', - false, + // NoACL. + true, + // ChildGroups. $recursion, + // Serialized. false, + // Separator. '|', - $cluster_mode + // Add_alert_bulk_op. + $cluster_mode, + // Force_serialized. + false ); if (empty($agents)) { diff --git a/pandora_console/operation/events/events.build_query.php b/pandora_console/operation/events/events.build_query.php index b1b4051e7f..0f68cbfb5f 100755 --- a/pandora_console/operation/events/events.build_query.php +++ b/pandora_console/operation/events/events.build_query.php @@ -28,7 +28,7 @@ if ($id_group > 0) { if ($propagate) { $childrens_ids = [$id_group]; - $childrens = groups_get_childrens($id_group, null, true); + $childrens = groups_get_children($id_group, null, true); if (!empty($childrens)) { foreach ($childrens as $child) {