groups_get_children and propagate update

This commit is contained in:
fbsanchez 2020-10-21 11:38:57 +02:00
parent 7bd7d190c5
commit 42cca865ac
19 changed files with 78 additions and 147 deletions

View File

@ -668,7 +668,7 @@ function mainAgentsModules()
if ($recursion) { if ($recursion) {
$filter_groups['id_grupo'] = array_merge( $filter_groups['id_grupo'] = array_merge(
$group_id, $group_id,
groups_get_id_recursive($group_id, true) groups_get_children_ids($group_id, true)
); );
} else { } else {
$filter_groups['id_grupo'] = $group_id; $filter_groups['id_grupo'] = $group_id;

View File

@ -408,7 +408,7 @@ if ($ag_group > 0) {
$ag_groups = []; $ag_groups = [];
$ag_groups = (array) $ag_group; $ag_groups = (array) $ag_group;
if ($recursion) { 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); $user_groups_to_sql = implode(',', $ag_groups);

View File

@ -877,7 +877,7 @@ if ($id_downtime > 0) {
$filter_cond = ''; $filter_cond = '';
if ($filter_group > 0) { if ($filter_group > 0) {
if ($recursion) { if ($recursion) {
$rg = groups_get_id_recursive($filter_group, true); $rg = groups_get_children_ids($filter_group, true);
$filter_cond .= ' AND id_grupo IN ('; $filter_cond .= ' AND id_grupo IN (';
$i = 0; $i = 0;

View File

@ -40,7 +40,7 @@ if (is_ajax()) {
$keys_prefix = (string) get_parameter('keys_prefix', ''); $keys_prefix = (string) get_parameter('keys_prefix', '');
if ($recursion) { if ($recursion) {
$groups = groups_get_id_recursive($id_group, true); $groups = groups_get_children_ids($id_group, true);
} else { } else {
$groups = [$id_group]; $groups = [$id_group];
} }

View File

@ -278,7 +278,7 @@ if ($ag_group > 0) {
$ag_groups = []; $ag_groups = [];
$ag_groups = (array) $ag_group; $ag_groups = (array) $ag_group;
if ($recursion) { if ($recursion) {
$ag_groups = groups_get_id_recursive($ag_group, true); $ag_groups = groups_get_children_ids($ag_group, true);
} }
} }

View File

@ -143,7 +143,7 @@ if (!$own_info['is_admin'] && !check_acl($config['id_user'], 0, 'AW')) {
$ag_groups = []; $ag_groups = [];
$ag_groups = (array) $ag_group; $ag_groups = (array) $ag_group;
if ($recursion) { 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']) { } else if ($own_info['is_admin']) {
$returnAllGroups = 1; $returnAllGroups = 1;

View File

@ -248,7 +248,7 @@ class CredentialStore extends Wizard
); );
} else { } else {
$groups = [ $filter['filter_id_group'] ]; $groups = [ $filter['filter_id_group'] ];
$childrens = groups_get_childrens($id_group, null, true); $childrens = groups_get_children($id_group, null, true);
if (!empty($childrens)) { if (!empty($childrens)) {
foreach ($childrens as $child) { foreach ($childrens as $child) {
$groups[] = (int) $child['id_grupo']; $groups[] = (int) $child['id_grupo'];

View File

@ -810,7 +810,7 @@ class NetworkMap
$filter['id_grupo'] = $this->idGroup; $filter['id_grupo'] = $this->idGroup;
} else { } else {
// Show current group and children. // Show current group and children.
$childrens = groups_get_childrens($this->idGroup, null, true); $childrens = groups_get_children($this->idGroup, null, true);
if (!empty($childrens)) { if (!empty($childrens)) {
$childrens = array_keys($childrens); $childrens = array_keys($childrens);

View File

@ -1108,11 +1108,14 @@ function agents_get_group_agents(
foreach ($id_group as $parent) { foreach ($id_group as $parent) {
$id_group = array_merge( $id_group = array_merge(
$id_group, $id_group,
groups_get_id_recursive($parent, false) groups_get_children_ids($parent, $noACL)
); );
} }
} else { } 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. // Check available groups for target user only if asking for 'All' group.

View File

@ -1973,7 +1973,7 @@ function api_get_all_agents($thrash1, $thrash2, $other, $returnType)
$ag_groups = $other['data'][1]; $ag_groups = $other['data'][1];
// Recursion. // Recursion.
if ($other['data'][6] === '1') { 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); $ag_groups = implode(',', (array) $ag_groups);

View File

@ -367,7 +367,7 @@ function agent_counters_custom_fields($filters)
if ($filters['group']) { if ($filters['group']) {
// Recursion check acl. // Recursion check acl.
if ($filters['recursion']) { 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 (!users_can_manage_group_all('AR')) {
if (isset($user_groups) && is_array($user_groups)) { if (isset($user_groups) && is_array($user_groups)) {
$groups_intersect = array_intersect($user_groups, $recursion_groups); $groups_intersect = array_intersect($user_groups, $recursion_groups);

View File

@ -871,37 +871,23 @@ function events_get_all(
} }
$groups = $filter['id_group_filter']; $groups = $filter['id_group_filter'];
if (isset($groups) && $groups > 0) { if (isset($groups) === true && $groups > 0) {
$propagate = db_get_value( $children = groups_get_children($groups);
'propagate',
'tgrupo',
'id_grupo',
$groups
);
if (!$propagate && isset($groups)) { $_groups = [ $groups ];
$sql_filters[] = sprintf( if (empty($children) === false) {
' AND (te.id_grupo = %d OR tasg.id_group = %d)', foreach ($children as $child) {
$groups, $_groups[] = (int) $child['id_grupo'];
$groups
);
} else {
$children = groups_get_children($groups);
$_groups = [ $groups ];
if (!empty($children)) {
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. // Skip system messages if user is not PM.

View File

@ -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. * Return a array of id_group of children of given parent.
* *
* @param integer $parent The id_grupo parent to search its children. * @param integer $parent The id_grupo parent to search its children.
* @param array $ignorePropagate Ignore propagate. * @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 $groups;
static $user_groups;
if (empty($groups)) { if (empty($groups) === true) {
$aux_groups = []; $aux_groups = [];
$groups = db_get_all_rows_in_table('tgrupo'); $groups = db_get_all_rows_in_table('tgrupo');
foreach ($groups as $key => $value) { foreach ($groups as $key => $value) {
@ -317,13 +289,29 @@ function groups_get_children($parent, $ignorePropagate=false)
$groups = $aux_groups; $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 = []; $return = [];
foreach ($groups as $key => $g) { foreach ($groups as $key => $g) {
if ($g['id_grupo'] == 0) { if ($g['id_grupo'] == 0) {
continue; 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) { if ($g['parent'] == $parent) {
$return += [$g['id_grupo'] => $g]; $return += [$g['id_grupo'] => $g];
if ($g['propagate'] || $ignorePropagate) { 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). * 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 * @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]); return array_keys($return);
// 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;
} }

View File

@ -319,7 +319,7 @@ function networkmap_generate_dot(
if ($dont_show_subgroups) { if ($dont_show_subgroups) {
$filter['id_grupo'] = $group; $filter['id_grupo'] = $group;
} else { } else {
$childrens = groups_get_childrens($group, null, true); $childrens = groups_get_children($group, null, true);
if (!empty($childrens)) { if (!empty($childrens)) {
$childrens = array_keys($childrens); $childrens = array_keys($childrens);

View File

@ -9072,7 +9072,7 @@ function reporting_get_group_stats($id_group=0, $access='AR')
$covered_groups = []; $covered_groups = [];
$group_array = []; $group_array = [];
foreach ($id_group as $group) { 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 // Show empty groups only if they have children with agents
// $group_array = array(); // $group_array = array();

View File

@ -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 (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.')'; $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). // 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) { foreach ($user_tags as $user_tag) {
$tags_user = $user_tag['tags']; $tags_user = $user_tag['tags'];
$id_group_user = $user_tag['id_grupo']; $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)) { if (empty($childrens)) {
$group_ids = $id_group_user; $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); $tags_arr = explode(',', $tags);
foreach ($tags_arr as $tag) { foreach ($tags_arr as $tag) {
if ($tag == $id_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); $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); $tags_arr = explode(',', $tags);
foreach ($tags_arr as $tag) { foreach ($tags_arr as $tag) {
if ($tag == $id_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); $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); $tags_arr = explode(',', $tags);
foreach ($tags_arr as $tag) { foreach ($tags_arr as $tag) {
if ($tag == $id_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); $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); $tags_arr = explode(',', $tags);
foreach ($tags_arr as $tag) { foreach ($tags_arr as $tag) {
if ($tag == $id_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); $groups_id = array_merge($groups_id, $hierarchy_groups);
} }
} }

View File

@ -546,7 +546,7 @@ if (!empty($search_custom)) {
if ($group_id > 0) { if ($group_id > 0) {
$groups = [$group_id]; $groups = [$group_id];
if ($recursion) { if ($recursion) {
$groups = groups_get_id_recursive($group_id, true); $groups = groups_get_children_ids($group_id, true);
} }
} else { } else {
$groups = []; $groups = [];
@ -576,7 +576,7 @@ if ($strict_user) {
if ($group_id > 0) { if ($group_id > 0) {
$groups = [$group_id]; $groups = [$group_id];
if ($recursion) { if ($recursion) {
$groups = groups_get_id_recursive($group_id, true); $groups = groups_get_children_ids($group_id, true);
} }
$filter['id_group'] = implode(',', $groups); $filter['id_group'] = implode(',', $groups);

View File

@ -146,14 +146,24 @@ if (is_ajax()) {
// Perform search. // Perform search.
$agents = agents_get_group_agents( $agents = agents_get_group_agents(
// Id_group.
$id_group, $id_group,
// Search.
$filter, $filter,
// Case.
'lower', 'lower',
false, // NoACL.
true,
// ChildGroups.
$recursion, $recursion,
// Serialized.
false, false,
// Separator.
'|', '|',
$cluster_mode // Add_alert_bulk_op.
$cluster_mode,
// Force_serialized.
false
); );
if (empty($agents)) { if (empty($agents)) {

View File

@ -28,7 +28,7 @@ if ($id_group > 0) {
if ($propagate) { if ($propagate) {
$childrens_ids = [$id_group]; $childrens_ids = [$id_group];
$childrens = groups_get_childrens($id_group, null, true); $childrens = groups_get_children($id_group, null, true);
if (!empty($childrens)) { if (!empty($childrens)) {
foreach ($childrens as $child) { foreach ($childrens as $child) {