From ecc2fef8f6a4c70f2854159efb1390be7bed1839 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Thu, 13 Jun 2019 12:30:09 +0200 Subject: [PATCH] event validation/in process --- pandora_console/include/ajax/events.php | 33 +++++++ .../include/class/ConsoleSupervisor.php | 5 +- pandora_console/include/functions_events.php | 96 ++++++++++++++++++- .../include/javascript/pandora_events.js | 36 ++++++- pandora_console/operation/events/events.php | 38 +++++--- 5 files changed, 184 insertions(+), 24 deletions(-) diff --git a/pandora_console/include/ajax/events.php b/pandora_console/include/ajax/events.php index a7d06810b6..55834d86d6 100644 --- a/pandora_console/include/ajax/events.php +++ b/pandora_console/include/ajax/events.php @@ -79,6 +79,39 @@ $save_filter = get_parameter('save_filter', 0); $get_filter_values = get_parameter('get_filter_values', 0); $update_event_filter = get_parameter('update_event_filter', 0); $save_event_filter = get_parameter('save_event_filter', 0); +$in_process_event = get_parameter('in_process_event', 0); +$validate_event = get_parameter('validate_event', 0); +$delete_event = get_parameter('delete_event', 0); + +// Delete event (filtered or not). +if ($delete_event) { + $filter = get_parameter('filter', []); + $id_evento = get_parameter('id_evento', 0); + + // Check acl. + echo events_delete($id_evento, $filter); + return; +} + +// Validates an event (filtered or not). +if ($validate_event) { + $filter = get_parameter('filter', []); + $id_evento = get_parameter('id_evento', 0); + + // Check acl. + echo events_update_status($id_evento, EVENT_VALIDATE, $filter); + return; +} + +// Sets status to in progress. +if ($in_process_event) { + $filter = get_parameter('filter', []); + $id_evento = get_parameter('id_evento', 0); + + // Check acl. + echo events_update_status($id_evento, EVENT_PROCESS, $filter); + return; +} // Saves an event filter. if ($save_event_filter) { diff --git a/pandora_console/include/class/ConsoleSupervisor.php b/pandora_console/include/class/ConsoleSupervisor.php index 1b5a114287..af688fb5a0 100644 --- a/pandora_console/include/class/ConsoleSupervisor.php +++ b/pandora_console/include/class/ConsoleSupervisor.php @@ -32,6 +32,7 @@ require_once $config['homedir'].'/include/functions_db.php'; require_once $config['homedir'].'/include/functions_io.php'; require_once $config['homedir'].'/include/functions_notifications.php'; require_once $config['homedir'].'/include/functions_servers.php'; +require_once $config['homedir'].'/include/functions_update_manager.php'; // Enterprise includes. enterprise_include_once('include/functions_metaconsole.php'); @@ -1940,6 +1941,7 @@ class ConsoleSupervisor public function checkUpdateManagerRegistration() { global $config; + include_once $config['homedir'].'/include/functions_update_manager.php'; $login = get_parameter('login', false); if (update_manager_verify_registration() === false) { @@ -2244,6 +2246,7 @@ class ConsoleSupervisor public function getUMMessages() { global $config; + include_once $config['homedir'].'/include/functions_update_manager.php'; if (update_manager_verify_registration() === false) { // Console not subscribed. @@ -2261,8 +2264,6 @@ class ConsoleSupervisor $future = (time() + 2 * SECONDS_1HOUR); config_update_value('last_um_check', $future); - include_once $config['homedir'].'/include/functions_update_manager.php'; - $params = [ 'pandora_uid' => $config['pandora_uid'], 'timezone' => $config['timezone'], diff --git a/pandora_console/include/functions_events.php b/pandora_console/include/functions_events.php index 75dab2cadc..12cb62810c 100644 --- a/pandora_console/include/functions_events.php +++ b/pandora_console/include/functions_events.php @@ -184,6 +184,90 @@ function events_get_column_names($fields) } +/** + * Validates all events matching target filter. + * + * @param integer $id_evento Master event. + * @param integer $status Target status. + * @param array $filter Optional. Filter options. + * @param boolean $history Apply on historical table. + * + * @return integer Events validated or false if error. + */ +function events_update_status($id_evento, $status, $filter=null, $history=false) +{ + error_log($id_evento); + error_log($status); + + if (!$status) { + return false; + } + + if (!isset($id_evento) || $id_evento <= 0) { + return false; + } + + if (!isset($filter) || !is_array($filter)) { + $filter = ['group_rep' => 0]; + } + + $table = events_get_events_table(is_metaconsole(), $history); + + switch ($filter['group_rep']) { + case '0': + case '2': + default: + // No groups option direct update. + $update_sql = sprintf( + 'UPDATE %s + SET estado = %d + WHERE id_evento = %d', + $table, + $status, + $id_evento + ); + break; + + case '1': + // Group by events. + $sql = events_get_all( + ['te.*'], + $filter, + // Offset. + null, + // Limit. + null, + // Order. + null, + // Sort_field. + null, + // Historical table. + $history, + // Return_sql. + true + ); + + $update_sql = sprintf( + 'UPDATE %s tu INNER JOIN ( %s ) tf + ON tu.estado = tf.estado + AND tu.evento = tf.evento + AND tu.id_agente = tf.id_agente + AND tu.id_agentmodule = tf.id_agentmodule + AND tf.max_id_evento = %d + SET tu.estado = %d', + $table, + $sql, + $id_evento, + $status + ); + break; + } + + error_log($update_sql); + return db_process_sql($update_sql); +} + + /** * Retrieve all events filtered. * @@ -193,6 +277,8 @@ function events_get_column_names($fields) * @param integer $limit Limit (pagination). * @param string $order Sort order. * @param string $sort_field Sort field. + * @param boolean $history Apply on historical table. + * @param boolean $return_sql Return SQL (true) or execute it (false). * * @return array Events. * @throws Exception On error. @@ -204,6 +290,7 @@ function events_get_all( $limit=null, $order=null, $sort_field=null, + $history=false, $return_sql=false ) { global $config; @@ -326,7 +413,7 @@ function events_get_all( } } - $table = events_get_events_table($meta, $history); + $table = events_get_events_table(is_metaconsole(), $history); $tevento = sprintf( '(SELECT * FROM %s @@ -385,7 +472,7 @@ function events_get_all( case '1': // Group by events. - $group_by .= 'estado, evento, id_agente, id_agentmodule'; + $group_by .= 'te.estado, te.evento, te.id_agente, te.id_agentmodule'; $group_by .= $extra; break; @@ -429,7 +516,10 @@ function events_get_all( MAX(id_evento) as max_id_evento'; if ($count === false) { - unset($fields[array_search('te.user_comment', $fields)]); + $idx = array_search('te.user_comment', $fields); + if ($idx !== false) { + unset($fields[$idx]); + } } } diff --git a/pandora_console/include/javascript/pandora_events.js b/pandora_console/include/javascript/pandora_events.js index 9b0b6617da..c52f36607f 100644 --- a/pandora_console/include/javascript/pandora_events.js +++ b/pandora_console/include/javascript/pandora_events.js @@ -1,4 +1,4 @@ -/*global jQuery,$,forced_title_callback,Base64,*/ +/*global jQuery,$,forced_title_callback,Base64*/ // Show the modal window of an event function show_event_dialog(event_id, group_rep, dialog_page, result) { @@ -669,11 +669,39 @@ function show_event_response_command_dialog(id, response, total_checked) { }); } -function validate_event(e, row) { - console.log(row); +function update_event(table, id_evento, type) { + var inputs = $("#events_form :input"); + var values = {}; + inputs.each(function() { + values[this.name] = $(this).val(); + }); + + // Update events matching current filters and id_evento selected. + $.ajax({ + type: "POST", + url: $("#hidden-ajax_file").val(), + data: { + page: "include/ajax/events", + validate_event: type.validate_event, + in_process_event: type.in_process_event, + id_evento: id_evento, + filter: values + }, + success: function() { + table.draw().page(0); + } + }); } -function delete_event(e, row) { +function validate_event(table, id_evento) { + return update_event(table, id_evento, { validate_event: 1 }); +} + +function in_process_event(table, id_evento) { + return update_event(table, id_evento, { in_process_event: 1 }); +} + +function delete_event(table, id_evento, row) { $(row) .closest("tr") .remove(); diff --git a/pandora_console/operation/events/events.php b/pandora_console/operation/events/events.php index d5244cd3d2..be4499c300 100644 --- a/pandora_console/operation/events/events.php +++ b/pandora_console/operation/events/events.php @@ -53,6 +53,10 @@ if (! check_acl($config['id_user'], 0, 'ER') 'ACL Violation', 'Trying to access event viewer' ); + if (is_ajax()) { + return ['error' => 'noaccess']; + } + include 'general/noaccess.php'; return; } @@ -94,10 +98,6 @@ $user_comment = get_parameter('filter[user_comment]'); // Ajax responses. if (is_ajax()) { - $get_filter_values = get_parameter('get_filter_values', 0); - $save_event_filter = get_parameter('save_event_filter', 0); - $update_event_filter = get_parameter('update_event_filter', 0); - $get_event_filters = get_parameter('get_event_filters', 0); $get_events = get_parameter('get_events', 0); $filter = get_parameter('filter', []); // Datatables offset, limit. @@ -1083,16 +1083,18 @@ try { // Close. $active_filters_div .= ''; + $table_id = 'events'; // Print datatable. ui_print_datatable( [ - 'id' => 'events', + 'id' => $table_id, 'class' => 'info_table events', 'style' => 'width: 100%;', 'ajax_url' => 'operation/events/events', 'ajax_data' => ['get_events' => 1], 'form' => [ + 'id' => 'events_form', 'class' => 'flex-row', 'html' => $filter, 'inputs' => [], @@ -1215,12 +1217,12 @@ function process_datatables_callback(table, settings) { $(rows).eq( i ).before( '' +'' - +group+' ' + +group+' ' +''+'' +'' +'' ); - events_per_group.push(i); + events_per_group.push(i - last_count); last_count = i; last = group; j += 1; @@ -1426,25 +1428,31 @@ function process_datatables_item(item) { item.options += ')" > __('Show more')]); ?>'; // Validate. - item.options += ''; + item.options += ''; item.options += ' __('Validate events')]); ?>'; } else { + item.options += item.id_evento+')" >'; item.options += ' __('Validate event')]); ?>'; } - // In progress. - item.options += ''; - item.options += ' __('Chnge to in progress status')]); ?>'; + // In process. + item.options += ''; + } else { + item.options += item.id_evento+')" >'; + } + item.options += ' __('Change to in progress status')]); ?>'; // Delete. - item.options += ''; + item.options += ''; item.options += ' __('Delete events')]); ?>'; } else { + item.options += item.id_evento+', this)" >'; item.options += ' __('Delete event')]); ?>'; }