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[]',
@ -393,7 +398,7 @@ echo '</form>';
?>
<script type="text/javascript">
var $table = $('table#massive_plugin_edition'),
$form = $('form#form-massive_plugin_edition'),
$submitButton = $('input#submit-upd-btn'),
@ -401,7 +406,7 @@ echo '</form>';
$pluginsSelect = $('select#plugin_id'),
$agentsSelect = $('select#agent_ids'),
$modulesSelect = $('select#module_names');
var agents = [],
ajaxPage = "<?php echo $config['homeurl'].'/'; ?>ajax.php",
canSubmit = false,
@ -409,15 +414,15 @@ echo '</form>';
agentsXHR,
modulesXHR,
modulePluginMacrosXHR;
var allowSubmit = function (val) {
if (typeof val === 'undefined')
val = true;
canSubmit = val;
$submitButton.prop('disabled', !val);
}
var clearModulePluginMacrosValues = function () {
$('input.plugin-macro')
.val('')
@ -428,47 +433,47 @@ echo '</form>';
.siblings('button')
.remove();
}
var hidePluginData = function () {
$('table#massive_plugin_edition tr.plugin-data-row').hide();
}
var clearPluginData = function () {
hidePluginData();
clearModulePluginMacrosValues();
$('table#massive_plugin_edition tr.plugin-data-row').remove();
}
var clearAgentsData = function () {
$agentsSelect.empty();
}
var clearModulesData = function () {
$modulesSelect.empty();
}
// Creates the plugin info and macros columns
// Creates the plugin info and macros columns.
var fillPlugin = function (plugin) {
clearPluginData();
if (typeof plugin === 'undefined'
|| typeof plugin.execute === 'undefined'
|| typeof plugin.parameters === 'undefined'
|| typeof plugin.description === 'undefined'
|| typeof plugin.macros === 'undefined')
throw new Error('<?php echo __('Invalid plugin data'); ?>');
if (_.isString(plugin.macros)) {
plugin.macros = JSON.parse(plugin.macros);
}
var $commandRow = $('<tr></tr>'),
$commandCellTitle = $('<td></td>'),
$commandCellData = $('<td></td>'),
$descriptionRow = $('<tr></tr>'),
$descriptionCellTitle = $('<td></td>'),
$descriptionCellData = $('<td></td>');
$commandCellTitle
.addClass('plugin-data-cell')
.css('font-weight', 'bold')
@ -482,7 +487,7 @@ echo '</form>';
.addClass('plugin-data-row')
.css('vertical-align', 'top')
.append($commandCellTitle, $commandCellData);
$descriptionCellTitle
.addClass('plugin-data-cell')
.css('font-weight', 'bold')
@ -495,16 +500,16 @@ echo '</form>';
.addClass('plugin-data-row')
.css('vertical-align', 'top')
.append($descriptionCellTitle, $descriptionCellData);
$table.append($commandRow, $descriptionRow);
_.each(plugin.macros, function (macro, index) {
var $macroRow = $('<tr></tr>'),
$macroCellTitle = $('<td></td>'),
$macroCellData = $('<td></td>'),
$macroInput = $('<input>'),
$macroIdentifier = $('<span></span>');
$macroInput
.prop('id', macro.macro)
.prop('name', 'macros[' + macro.macro + ']')
@ -526,12 +531,12 @@ echo '</form>';
.bind('focus', function() {
$(this).autocomplete("search");
});
$macroIdentifier
.css('font-weight', 'normal')
.css('padding-left', '5px')
.append('(' + macro.macro + ')');
$macroCellTitle
.addClass('plugin-data-cell')
.css('font-weight', 'bold')
@ -544,22 +549,22 @@ echo '</form>';
$macroRow
.addClass('plugin-data-row')
.append($macroCellTitle, $macroCellData);
$table.append($macroRow);
});
}
var removeMultipleElementsButton = function (element) {
element
.css('width', '99%')
.siblings('button')
.remove();
}
// This button removes the special properties of the multiple values macro input
var addMultipleElementsButton = function (element) {
$button = $('<button>');
$button
.css('display', 'inline')
.css('margin-left', '3px')
@ -567,49 +572,49 @@ echo '</form>';
.click(function (e) {
e.stopImmediatePropagation();
e.preventDefault();
if (!confirm("<?php echo __('Are you sure?'); ?>"))
return false;
removeMultipleElementsButton(element);
element
.val('')
.data('multiple_values', false)
.prop('placeholder', '');
});
element
.css('width', '90%')
.css('display', 'inline')
.parent()
.append($button);
}
// Fills the module plugin macros values
var fillPluginMacros = function (moduleMacros) {
clearModulePluginMacrosValues();
if (!(moduleMacros instanceof Array))
throw new Error('<?php echo __('Invalid macros array'); ?>');
$("input.plugin-macro").each(function(index, el) {
var id = $(el).prop('id');
var values = _.chain(moduleMacros)
.flatten()
.where({ macro: id })
.pluck('value')
.uniq()
.value();
$(el).prop('disabled', false);
// Remove the [""] element
if (values.length == 1 && _.first(values) === '') {
values = [];
}
if (values.length == 1) {
$(el).val(_.first(values));
}
@ -622,9 +627,9 @@ echo '</form>';
else {
$(el).val('');
}
if ($(el).prop('type') !== 'password' && values.length > 0) {
$(el).autocomplete("option", {
disabled: false,
source: values
@ -642,14 +647,14 @@ echo '</form>';
.css('padding-right', '20px')
.css('text-align', 'left');
}
// Fills the agents select
var fillAgents = function (agents, selected) {
clearAgentsData();
if (!(agents instanceof Array))
throw new Error('<?php echo __('Invalid agents array'); ?>');
_.each(agents, function (agent, index) {
if (typeof agent.id !== 'undefined' && typeof agent.name !== 'undefined') {
$('<option>')
@ -658,7 +663,7 @@ echo '</form>';
.prop('selected', function () {
if (typeof selected !== 'undefined')
return false;
return _.contains(selected, agent.id.toString());
})
.appendTo($agentsSelect);
@ -669,14 +674,14 @@ echo '</form>';
}
});
}
// Fills the modules select
var fillModules = function (modules, selected) {
clearModulesData();
if (!(modules instanceof Array))
throw new Error('<?php echo __('Invalid modules array'); ?>');
_.each(modules, function (module, index) {
if (_.isString(module)) {
$('<option>')
@ -685,7 +690,7 @@ echo '</form>';
.prop('selected', function () {
if (typeof selected === 'undefined')
return false;
return _.contains(selected, module);
})
.appendTo($modulesSelect);
@ -697,7 +702,7 @@ echo '</form>';
.prop('selected', function () {
if (typeof selected === 'undefined')
return false;
return _.contains(selected, module.name);
})
.appendTo($modulesSelect);
@ -707,9 +712,9 @@ echo '</form>';
return false;
}
});
}
var processGet = function (params, callback) {
return jQuery.post(ajaxPage, params, 'json')
.done(function (data, textStatus, jqXHR) {
@ -726,34 +731,34 @@ echo '</form>';
callback(errorThrown);
})
.always(function (jqXHR, textStatus) {
});
}
var getPlugin = function (pluginID, callback) {
var params = {
page: 'godmode/massive/massive_edit_plugins',
get_plugin: 1,
plugin_id: pluginID
};
pluginXHR = processGet(params, function (error, data) {
callback(error, data);
});
}
var getAgents = function (pluginID, callback) {
var params = {
page: 'godmode/massive/massive_edit_plugins',
get_agents: 1,
plugin_id: pluginID
};
agentsXHR = processGet(params, function (error, data) {
callback(error, data);
});
}
var getModules = function (pluginID, agentIDs, callback) {
var params = {
page: 'godmode/massive/massive_edit_plugins',
@ -761,12 +766,12 @@ echo '</form>';
plugin_id: pluginID,
agent_ids: agentIDs
};
modulesXHR = processGet(params, function (error, data) {
callback(error, data);
});
}
var getModulePluginMacros = function (pluginID, agentIDs, moduleNames, callback) {
var params = {
page: 'godmode/massive/massive_edit_plugins',
@ -775,17 +780,17 @@ echo '</form>';
agent_ids: agentIDs,
module_names: moduleNames
};
modulePluginMacrosXHR = processGet(params, function (error, data) {
callback(error, data);
});
}
// Extract the a module names array from the agents
var moduleNamesFromAgents = function (agents) {
if (!(agents instanceof Array))
throw new Error('<?php echo __('Invalid agents array'); ?>');
var moduleNames = _.map(agents, function (agent) {
return agent['module_names'];
});
@ -794,28 +799,28 @@ echo '</form>';
.flatten()
.uniq()
.value();
return moduleNames;
}
var agentsFilteredWithAgents = function (agents, agentIDs) {
if (!(agents instanceof Array))
throw new Error('<?php echo __('Invalid agents array'); ?>');
var agentsFiltered = _.filter(agents, function (agent) {
return _.contains(agentIDs, agent.id.toString());
});
// Hack. Is possible that find returns an object instead of an array
// when the only array item is an object. Probably an Underscore.js bug
if (typeof agentsFiltered !== 'undefined'
&& !(agentsFiltered instanceof Array)
&& (agentsFiltered instanceof Object))
agentsFiltered = [agentsFiltered];
return agentsFiltered;
}
var resetController = function () {
if (typeof pluginXHR !== 'undefined') {
pluginXHR.abort();
@ -833,54 +838,54 @@ echo '</form>';
modulePluginMacrosXHR.abort();
modulePluginMacrosXHR = undefined;
}
allowSubmit(false);
agents = [];
hideSpinner();
clearPluginData();
$agentModulesRow.hide();
clearAgentsData();
clearModulesData();
}
var errorHandler = function (error) {
hideSpinner();
console.log("<?php echo __('Error'); ?>: " + error.message);
// alert("<?php echo __('Error'); ?>: " + err.message);
// Init the plugin id select
$pluginsSelect.val(0).change();
}
$pluginsSelect.change(function (e) {
allowSubmit(false);
// Plugin id
var currentVal = $(this).val();
resetController();
if (currentVal == 0)
return;
try {
showSpinner();
// This asyc functions are executed at the same time
getPlugin(currentVal, function (error, data) {
if (error) {
errorHandler(error);
return;
}
plugin = data;
try {
fillPlugin(plugin);
// Hide spinner only if the another call has finished
if (typeof agentsXHR === 'undefined'
|| agentsXHR.state() === 'resolved'
@ -893,21 +898,21 @@ echo '</form>';
return;
}
});
// This asyc functions are executed at the same time
getAgents(currentVal, function (error, data) {
if (error) {
errorHandler(error);
return;
}
// This agent variable is global to this script scope
agents = data;
try {
if (agents.length > 0) {
fillAgents(agents);
$agentModulesRow.show();
}
else {
@ -915,14 +920,14 @@ echo '</form>';
contents.html = '<?php echo __('There are no modules using this plugin'); ?>';
contents.title = '<?php echo __('Massive operations'); ?>';
showMassiveModal(contents);
// Abort the another call
if (typeof pluginXHR !== 'undefined') {
pluginXHR.abort();
pluginXHR = undefined;
}
}
// Hide spinner only if the another call has finished
if (typeof pluginXHR === 'undefined'
|| pluginXHR.state() === 'resolved'
@ -940,23 +945,23 @@ echo '</form>';
errorHandler(err);
return;
}
}).change(); // Trigger the change
$agentsSelect.change(function (e) {
allowSubmit(false);
var ids = $(this).val();
var modulesSelected = $modulesSelect.val();
try {
var agentsFiltered = agentsFilteredWithAgents(agents, ids);
var modules = moduleNamesFromAgents(agentsFiltered);
for (var i = 0; i < modules.length; i++) {
modules[i] = htmlDecode(modules[i]);
}
fillModules(modules, modulesSelected);
}
catch (err) {
@ -964,42 +969,42 @@ echo '</form>';
return;
}
});
$modulesSelect.change(function (e) {
allowSubmit(false);
var pluginID = $pluginsSelect.val();
var moduleNames = $(this).val();
var agentIDs = $agentsSelect.val();
if (_.isNull(moduleNames) || _.isUndefined(moduleNames)) {
e.preventDefault();
return false;
}
try {
showSpinner();
clearModulePluginMacrosValues();
getModulePluginMacros(pluginID, agentIDs, moduleNames, function (error, data) {
if (error) {
errorHandler(error);
return;
}
try {
var modulePluginMacros = data;
if (_.isArray(modulePluginMacros) && modulePluginMacros.length > 0) {
fillPluginMacros(modulePluginMacros);
allowSubmit(true);
}
else {
throw new Error('<?php echo __('There was a problem loading the module plugin macros data'); ?>');
}
hideSpinner();
}
catch (err) {
@ -1013,7 +1018,7 @@ echo '</form>';
return;
}
});
$form.submit(function(e) {
if (!canSubmit) {
e.stopImmediatePropagation();
@ -1023,20 +1028,20 @@ echo '</form>';
$form.find('input.plugin-macro')
.filter(function() {
var val = $(this).val();
if ($(this).data("multiple_values") == true
&& (typeof val === 'undefined'
|| val.length == 0))
return true;
else
return false;
}).prop('disabled', true);
}
});
$(document).ready (function () {
});
</script>

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;
}