Added audit log in massive operations

This commit is contained in:
Jose Gonzalez 2022-02-02 12:45:43 +01:00
parent 615672629c
commit bc6175abad
2 changed files with 180 additions and 171 deletions

View File

@ -14,7 +14,7 @@
* |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______|
*
* ============================================================================
* Copyright (c) 2005-2021 Artica Soluciones Tecnologicas
* Copyright (c) 2005-2022 Artica Soluciones Tecnologicas
* Please see http://pandorafms.org for full contribution list
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -45,7 +45,7 @@ $agent_ids = get_parameter('agent_ids', []);
$module_ids = get_parameter('module_ids', []);
$module_names = get_parameter('module_names', []);
if (is_ajax()) {
if (is_ajax() === true) {
$get_plugin = (bool) get_parameter('get_plugin');
$get_agents = (bool) get_parameter('get_agents');
$get_modules = (bool) get_parameter('get_modules');
@ -54,21 +54,21 @@ if (is_ajax()) {
if ($get_plugin) {
$plugin = db_get_row('tplugin', 'id', $plugin_id);
if (empty($plugin)) {
if (empty($plugin) === true) {
$plugin = [];
}
if (isset($plugin['description'])) {
if (isset($plugin['description']) === true) {
$plugin['description'] = io_safe_output($plugin['description']);
$plugin['description'] = str_replace("\n", '<br>', $plugin['description']);
}
if (isset($plugin['macros'])) {
if (isset($plugin['macros']) === true) {
$macros = json_decode($plugin['macros'], true);
if (!empty($macros)) {
if (empty($macros) === false) {
$macros = array_values($macros);
if (!empty($macros)) {
if (empty($macros) === false) {
$plugin['macros'] = $macros;
}
}
@ -79,15 +79,15 @@ if (is_ajax()) {
}
if ($get_agents) {
$sql = "SELECT ta.id_agente, ta.alias AS agent_alias,
$sql = 'SELECT ta.id_agente, ta.alias AS agent_alias,
tam.nombre AS module_name
FROM tagente ta
INNER JOIN tagente_modulo tam
ON ta.id_agente = tam.id_agente
AND tam.id_plugin = $plugin_id
ORDER BY ta.alias, tam.nombre";
AND tam.id_plugin = '.$plugin_id.'
ORDER BY ta.alias, tam.nombre';
$result = db_get_all_rows_sql($sql);
if (empty($result)) {
if (empty($result) === true) {
$result = [];
}
@ -99,7 +99,7 @@ if (is_ajax()) {
$name = io_safe_output($value['agent_alias']);
$module_name = $value['module_name'];
if (!empty($current_element) && $current_element['id'] !== $id) {
if (empty($current_element) === false && $current_element['id'] !== $id) {
$agents[] = $current_element;
$current_element = [];
}
@ -107,14 +107,14 @@ if (is_ajax()) {
$current_element['id'] = $id;
$current_element['name'] = $name;
if (!isset($current_element['module_names'])) {
if (isset($current_element['module_names']) === false) {
$current_element['module_names'] = [];
}
$current_element['module_names'][] = $module_name;
}
if (!empty($current_element)) {
if (empty($current_element) === false) {
$agents[] = $current_element;
}
@ -131,7 +131,7 @@ if (is_ajax()) {
];
$module_plugin_macros = db_get_all_rows_filter('tagente_modulo', $filter, $fields);
$module_plugin_macros = io_safe_output($module_plugin_macros);
if (empty($module_plugin_macros)) {
if (empty($module_plugin_macros) === true) {
$module_plugin_macros = [];
}
@ -139,9 +139,9 @@ if (is_ajax()) {
$module_plugin_macros,
function ($carry, $item) {
$macros = json_decode($item['macros'], true);
if (!empty($macros)) {
if (empty($macros) === false) {
$macros = array_values($macros);
if (!empty($macros)) {
if (empty($macros) === false) {
$carry[] = $macros;
}
}
@ -160,28 +160,28 @@ if (is_ajax()) {
$update = (bool) get_parameter('update');
if ($update) {
if ($update === true) {
try {
$plugin = db_get_row('tplugin', 'id', $plugin_id);
// Macros retrieved from the plugin definition
// Macros retrieved from the plugin definition.
$plugin_macros = [];
if (isset($plugin['macros'])) {
if (isset($plugin['macros']) === true) {
$plugin_macros = json_decode($plugin['macros'], true);
if (!empty($plugin_macros)) {
if (empty($plugin_macros) === false) {
$plugin_macros = array_values($plugin_macros);
}
}
// Error
// Error.
if (empty($plugin_macros)) {
throw new Exception(__('Error retrieving the plugin macros'));
}
// Macros returned by the form
// Macros returned by the form.
$macros = get_parameter('macros', []);
// Error
if (empty($macros)) {
// Error.
if (empty($macros) === true) {
throw new Exception(__('Error retrieving the modified macros'));
}
@ -199,36 +199,36 @@ if ($update) {
$filter,
$fields
);
if (empty($module_plugin_macros)) {
if (empty($module_plugin_macros) === true) {
$module_plugin_macros = [];
}
// Error
if (empty($module_plugin_macros)) {
// Error.
if (empty($module_plugin_macros) === true) {
throw new Exception(__('Error retrieving the module plugin macros'));
}
// Begin transaction
// db_process_sql_begin();
// db_process_sql_begin();.
$errors = 0;
$count = 0;
foreach ($module_plugin_macros as $item) {
$module_id = $item['id_agente_modulo'];
$module_macros_str = $item['macros'];
// Macros retrieved from the agent module
// Macros retrieved from the agent module.
$module_macros = json_decode($module_macros_str, true);
// Error
if (empty($module_macros)) {
// Error.
if (empty($module_macros) === true) {
throw new Exception(
__('Error retrieving the module plugin macros data')
);
}
// Get the new module plugin macros
// Get the new module plugin macros.
$result_macros = array_map(
function ($item) use ($macros, $module_macros) {
$result = [
@ -238,7 +238,7 @@ if ($update) {
'hide' => $item['hide'],
];
// Get the default value os the module plugin macro
// Get the default value os the module plugin macro.
$default = array_reduce(
$module_macros,
function ($carry, $module_macro) use ($result) {
@ -259,41 +259,48 @@ if ($update) {
$plugin_macros
);
// Error
if (empty($result_macros)) {
// Error.
if (empty($result_macros) === true) {
throw new Exception(__('Error building the new macros'));
}
$module_macros = io_json_mb_encode($result_macros, JSON_FORCE_OBJECT);
if (empty($module_macros)) {
if (empty($module_macros) === true) {
$module_macros = $module_macros_str;
}
$values = ['macros' => $module_macros];
$where = ['id_agente_modulo' => $module_id];
// $result = db_process_sql_update('tagente_modulo', $values, $where, 'AND', false);
$result = db_process_sql_update('tagente_modulo', $values, $where);
if (!$result) {
if ((bool) $result === false) {
$errors++;
} else {
$count += $result;
}
}
// if (!$errors) {
// db_process_sql_commit();
// }
// else {
// db_process_sql_rollback();
// }
// Result message
// Result message.
$auditMessage = sprintf(
'Plugin #%s modules updated',
$plugin_id
);
ui_print_info_message(sprintf(__('%d modules updated'), $count));
} catch (Exception $e) {
$auditMessage = sprintf(
'Try to update plugin #%s modules: %s',
$plugin_id,
$e->getMessage()
);
ui_print_error_message($e->getMessage());
}
db_pandora_audit(
AUDIT_LOG_MASSIVE_MANAGEMENT,
$auditMessage
);
}
$table = new StdClass();
@ -302,7 +309,7 @@ $table->width = '100%';
$table->rowstyle = [];
$table->data = [];
// Plugins
// Plugins.
$filter = ['order' => 'name'];
$fields = [
'id',
@ -341,10 +348,10 @@ $row[] = $plugins_select;
$table->data['plugin-ids-row'] = $row;
// Agents & modules
// Agents & modules.
$row = [];
// Agents
// Agents.
$agents_select = html_print_select(
$agent_ids,
'agent_ids[]',
@ -360,9 +367,7 @@ $agents_select = html_print_select(
$row[] = '<b>'.__('Agents').'</b>';
$row[] = $agents_select;
// Modules
// $modules_select = html_print_select ($module_ids, 'module_ids',
// false, '', '', 0, true, true, false);
// Modules.
$modules_select = html_print_select(
$module_names,
'module_names[]',
@ -447,7 +452,7 @@ echo '</form>';
$modulesSelect.empty();
}
// Creates the plugin info and macros columns
// Creates the plugin info and macros columns.
var fillPlugin = function (plugin) {
clearPluginData();

View File

@ -82,17 +82,17 @@ function config_update_value($token, $value)
);
}
if ($token == 'default_assign_tags') {
if ($token === 'default_assign_tags') {
$value = ($value);
}
if (!isset($config[$token])) {
if (isset($config[$token]) === false) {
$config[$token] = $value;
return (bool) config_create_value($token, io_safe_input($value));
}
// If it has not changed.
if ($config[$token] == $value) {
if ($config[$token] === $value) {
return true;
}
@ -108,6 +108,15 @@ function config_update_value($token, $value)
if ($result === 0) {
return true;
} else {
// Something in setup changes.
db_pandora_audit(
AUDIT_LOG_SETUP,
'Setup has changed',
false,
false,
sprintf('Token << %s >> updated.', $token)
);
return (bool) $result;
}
}
@ -126,7 +135,7 @@ function config_update_config()
include_once $config['homedir'].'/include/functions_io.php';
// If user is not even log it, don't try this.
if (! isset($config['id_user'])) {
if (isset($config['id_user']) === false) {
$config['error_config_update_config'] = [];
$config['error_config_update_config']['correct'] = false;
$config['error_config_update_config']['message'] = __('Failed updated: User did not login.');
@ -144,12 +153,7 @@ function config_update_config()
$update_config = (bool) get_parameter('update_config');
if ($update_config) {
db_pandora_audit(
AUDIT_LOG_SETUP,
'Setup has changed'
);
} else {
if ($update_config === false) {
// Do nothing.
return false;
}