#9021 Added offspring check

This commit is contained in:
Daniel Maya 2022-05-30 11:29:17 +02:00
parent 1e430f2f73
commit 79e7753cc8
5 changed files with 78 additions and 61 deletions

View File

@ -523,7 +523,6 @@ if (enterprise_installed()) {
$table_adv_cascade .= $cps_html;
}
$table_adv_parent = '<div class="label_select"><label class="input_label">'.__('Parent').'</label>';
$params = [];
$params['return'] = true;
@ -536,6 +535,11 @@ $params['value'] = db_get_value('alias', 'tagente', 'id_agente', $id_parent);
$params['selectbox_id'] = 'cascade_protection_module';
$params['javascript_is_function_select'] = true;
$params['cascade_protection'] = true;
if ($id_agente !== 0) {
// Deletes the agent's offspring.
$params['delete_offspring_agents'] = $id_agente;
}
$table_adv_parent .= '<div class="label_simple_items">';
$table_adv_parent .= ui_print_agent_autocomplete_input($params);
if (enterprise_installed()) {

View File

@ -86,6 +86,8 @@ if ($search_agents && (!is_metaconsole() || $force_local)) {
$addedItems = json_decode($addedItems);
$all = (string) get_parameter('all', 'all');
$delete_offspring_agents = (int) get_parameter('delete_offspring_agents', 0);
if ($addedItems != null) {
foreach ($addedItems as $item) {
echo $item."|\n";
@ -111,21 +113,9 @@ if ($search_agents && (!is_metaconsole() || $force_local)) {
}
$data = [];
// Get agents for only the alias
// Get agents for only the alias.
$filter_alias = $filter;
switch ($config['dbtype']) {
case 'mysql':
$filter_alias[] = '(UPPER(alias) LIKE "%'.$string.'%")';
break;
case 'postgresql':
$filter_alias[] = '(UPPER(alias) LIKE \'%'.$string.'%\')';
break;
case 'oracle':
$filter_alias[] = '(UPPER(alias) LIKE UPPER(\'%'.$string.'%\'))';
break;
}
$filter_alias[] = '(UPPER(alias) LIKE "%'.$string.'%")';
$agents = agents_get_agents($filter_alias, ['id_agente', 'nombre', 'direccion', 'alias']);
if ($agents !== false) {
@ -142,19 +132,7 @@ if ($search_agents && (!is_metaconsole() || $force_local)) {
// Get agents for only the name.
$filter_agents = $filter;
switch ($config['dbtype']) {
case 'mysql':
$filter_agents[] = '(UPPER(alias) NOT LIKE "%'.$string.'%" AND UPPER(nombre) LIKE "%'.$string.'%")';
break;
case 'postgresql':
$filter_agents[] = '(UPPER(alias) NOT LIKE \'%'.$string.'%\' AND UPPER(nombre) LIKE \'%'.$string.'%\')';
break;
case 'oracle':
$filter_agents[] = '(UPPER(alias) NOT LIKE UPPER(\'%'.$string.'%\') AND UPPER(nombre) LIKE UPPER(\'%'.$string.'%\'))';
break;
}
$filter_agents[] = '(UPPER(alias) NOT LIKE "%'.$string.'%" AND UPPER(nombre) LIKE "%'.$string.'%")';
$agents = agents_get_agents($filter_agents, ['id_agente', 'nombre', 'direccion', 'alias']);
if ($agents !== false) {
@ -169,21 +147,9 @@ if ($search_agents && (!is_metaconsole() || $force_local)) {
}
}
// Get agents for only the address
// Get agents for only the address.
$filter_address = $filter;
switch ($config['dbtype']) {
case 'mysql':
$filter_address[] = '(UPPER(alias) NOT LIKE "%'.$string.'%" AND UPPER(nombre) NOT LIKE "%'.$string.'%" AND UPPER(direccion) LIKE "%'.$string.'%")';
break;
case 'postgresql':
$filter_address[] = '(UPPER(alias) NOT LIKE \'%'.$string.'%\' AND UPPER(nombre) NOT LIKE \'%'.$string.'%\' AND UPPER(direccion) LIKE \'%'.$string.'%\')';
break;
case 'oracle':
$filter_address[] = '(UPPER(alias) NOT LIKE UPPER(\'%'.$string.'%\') AND UPPER(nombre) NOT LIKE UPPER(\'%'.$string.'%\') AND UPPER(direccion) LIKE UPPER(\'%'.$string.'%\'))';
break;
}
$filter_address[] = '(UPPER(alias) NOT LIKE "%'.$string.'%" AND UPPER(nombre) NOT LIKE "%'.$string.'%" AND UPPER(direccion) LIKE "%'.$string.'%")';
$agents = agents_get_agents($filter_address, ['id_agente', 'nombre', 'direccion', 'alias']);
if ($agents !== false) {
@ -198,21 +164,9 @@ if ($search_agents && (!is_metaconsole() || $force_local)) {
}
}
// Get agents for only the description
// Get agents for only the description.
$filter_description = $filter;
switch ($config['dbtype']) {
case 'mysql':
$filter_description[] = '(UPPER(alias) NOT LIKE "%'.$string.'%" AND UPPER(nombre) NOT LIKE "%'.$string.'%" AND UPPER(direccion) NOT LIKE "%'.$string.'%" AND UPPER(comentarios) LIKE "%'.$string.'%")';
break;
case 'postgresql':
$filter_description[] = '(UPPER(alias) NOT LIKE \'%'.$string.'%\' AND UPPER(nombre) NOT LIKE \'%'.$string.'%\' AND UPPER(direccion) NOT LIKE \'%'.$string.'%\' AND UPPER(comentarios) LIKE \'%'.$string.'%\')';
break;
case 'oracle':
$filter_description[] = '(UPPER(alias) NOT LIKE UPPER(\'%'.$string.'%\') AND UPPER(nombre) NOT LIKE UPPER(\'%'.$string.'%\') AND UPPER(direccion) NOT LIKE UPPER(\'%'.$string.'%\') AND UPPER(comentarios) LIKE UPPER(\'%'.$string.'%\'))';
break;
}
$filter_description[] = '(UPPER(alias) NOT LIKE "%'.$string.'%" AND UPPER(nombre) NOT LIKE "%'.$string.'%" AND UPPER(direccion) NOT LIKE "%'.$string.'%" AND UPPER(comentarios) LIKE "%'.$string.'%")';
$agents = agents_get_agents($filter_description, ['id_agente', 'nombre', 'direccion', 'alias']);
if ($agents !== false) {
@ -227,6 +181,18 @@ if ($search_agents && (!is_metaconsole() || $force_local)) {
}
}
if (empty($data) === false && $delete_offspring_agents !== 0) {
// Gets offspring and deletes them, including himself.
$agents_offspring = agents_get_offspring($delete_offspring_agents);
if (empty($agents_offspring) === false) {
foreach ($data as $key => $value) {
if (isset($agents_offspring[$value['id']]) === true) {
unset($data[$key]);
}
}
}
}
echo json_encode($data);
return;
} else if ($search_agents && is_metaconsole()) {

View File

@ -4268,3 +4268,30 @@ function get_status_data_agent_modules($id_group, $agents=[], $modules=[])
return $res;
}
function agents_get_offspring(int $id_agent)
{
$return = [];
// Get parent.
$agents = db_get_all_rows_filter(
'tagente',
[
'id_parent' => $id_agent,
'disabled' => 0,
],
'id_agente'
);
if ($agents !== false) {
foreach ($agents as $agent) {
if ((int) $agent['id_agente'] !== 0) {
$return += agents_get_offspring((int) $agent['id_agente']);
}
}
}
$return += [$id_agent => 0];
return $return;
}

View File

@ -1469,22 +1469,22 @@ function api_set_update_agent($id_agent, $thrash2, $other, $thrash3)
// Check parameters.
if ($idGroup == 0) {
$agent_update_error = __('The agent could not be modified. For security reasons, use a group other than 0.');
returnError('generic error', $agent_update_error);
returnError($agent_update_error);
return;
}
$server_name = db_get_value_sql('SELECT name FROM tserver WHERE BINARY name LIKE "'.$nameServer.'"');
if ($alias == '' && $alias_as_name === 0) {
returnError('alias_not_specified', 'No agent alias specified');
returnError('No agent alias specified');
return;
} else if (db_get_value_sql('SELECT id_grupo FROM tgrupo WHERE id_grupo = '.$idGroup) === false) {
returnError('id_grupo_not_exist', 'The group doesn`t exist.');
returnError('The group doesn`t exist.');
return;
} else if (db_get_value_sql('SELECT id_os FROM tconfig_os WHERE id_os = '.$idOS) === false) {
returnError('id_os_not_exist', 'The OS doesn`t exist.');
returnError('The OS doesn`t exist.');
return;
} else if ($server_name === false) {
returnError('server_not_exist', 'The '.get_product_name().' Server doesn`t exist.');
returnError('The '.get_product_name().' Server doesn`t exist.');
return;
}
@ -1525,6 +1525,14 @@ function api_set_update_agent($id_agent, $thrash2, $other, $thrash3)
returnError('The user cannot access to parent agent.');
return;
}
$agents_offspring = agents_get_offspring($id_agent);
if (empty($agents_offspring) === false) {
if (isset($agents_offspring[$idParent]) === true) {
returnError('The parent cannot be a offspring');
return;
}
}
}
$values_old = db_get_row_filter(
@ -1751,6 +1759,14 @@ function api_set_update_agent_field($id_agent, $use_agent_alias, $params)
returnError('The user cannot access to parent agent.');
return;
}
$agents_offspring = agents_get_offspring($id_agent);
if (empty($agents_offspring) === false) {
if (isset($agents_offspring[$data]) === true) {
returnError('The parent cannot be a offspring');
return;
}
}
break;
default:

View File

@ -5448,6 +5448,10 @@ function ui_print_agent_autocomplete_input($parameters)
'q' => 'term',
];
if (isset($parameters['delete_offspring_agents']) === true) {
$javascript_change_ajax_params_original['delete_offspring_agents'] = $parameters['delete_offspring_agents'];
}
if (!$metaconsole_enabled) {
$javascript_change_ajax_params_original['force_local'] = 1;
}