#11705 new custom_field tevento

This commit is contained in:
Jonathan 2023-09-22 12:24:15 +02:00
parent 1ffebcf49c
commit 79fe89c3c5
9 changed files with 205 additions and 0 deletions

View File

@ -0,0 +1,6 @@
START TRANSACTION;
ALTER TABLE `tevento`
ADD COLUMN `custom_field` TEXT NULL AFTER `module_status`;
COMMIT;

View File

@ -115,6 +115,7 @@ $fields_available['module_status'] = __('Module Status');
$fields_available['mini_severity'] = __('Severity mini');
$fields_available['module_custom_id'] = __('Module custom ID');
$fields_available['custom_data'] = __('Custom data');
$fields_available['custom_field'] = __('Custom field');
// Remove fields already selected.

View File

@ -92,6 +92,7 @@ $get_id_source_event = get_parameter('get_id_source_event');
$node_id = (int) get_parameter('node_id', 0);
$settings_modal = get_parameter('settings', 0);
$parameters_modal = get_parameter('parameters', 0);
$update_custom_field = get_parameter('update_custom_field', 0);
// User private filter.
$current_filter = get_parameter('current_filter', 0);
$private_filter_event = get_parameter('private_filter_event', 0);
@ -2759,3 +2760,50 @@ if ($draw_row_response_info === true) {
echo $output;
return;
}
if ($update_custom_field) {
$custom_field = get_parameter('custom_field_value');
$event_id = get_parameter('event_id');
$server_id = 0;
if (is_metaconsole() === true) {
$server_id = (int) get_parameter('server_id');
}
// Safe custom fields for hacks.
if (preg_match('/script/i', io_safe_output($custom_field))) {
$return = false;
} else {
try {
if (is_metaconsole() === true
&& $server_id > 0
) {
$node = new Node($server_id);
$node->connect();
}
$return = events_custom_field(
$event_id,
$custom_field
);
} catch (\Exception $e) {
// Unexistent agent.
if (is_metaconsole() === true
&& $server_id > 0
) {
$node->disconnect();
}
$return = false;
} finally {
if (is_metaconsole() === true
&& $server_id > 0
) {
$node->disconnect();
}
}
}
echo ($return === true) ? 'update_ok' : 'update_error';
return;
}

View File

@ -17783,3 +17783,45 @@ function api_token_check(string $token)
return db_get_value('id_user', 'tusuario', 'api_token', $token);
}
}
/**
* Set custom field value in tevento
*
* @param mixed $id_event Event id.
* @param mixed $custom_field Custom field to set.
* @return void
*/
function api_set_event_custom_field($id, $value)
{
// Get the event
$event = events_get_event($id, false, is_metaconsole());
// If event not exists, end the execution.
if ($event === false) {
returnError(
'event_not_exists',
'Event not exists'
);
$result = false;
}
// Safe custom fields for hacks.
if (preg_match('/script/i', io_safe_output($value))) {
$result = false;
}
$result = events_custom_field(
$id,
$value
);
// If update results failed
if (empty($result) === true || $result === false) {
returnError(
'The event could not be updated'
);
return false;
} else {
returnData('string', ['data' => 'Event updated.']);
}
}

View File

@ -219,6 +219,7 @@ function events_get_all_fields()
$columns['module_status'] = __('Module status');
$columns['module_custom_id'] = __('Module custom id');
$columns['custom_data'] = __('Custom data');
$columns['custom_field'] = __('Custom field');
return $columns;
}
@ -322,6 +323,9 @@ function events_get_column_name($field, $table_alias=false)
case 'custom_data':
return __('Custom data');
case 'custom_field':
return __('Custom field');
default:
return __($field);
}
@ -4632,6 +4636,22 @@ function events_page_details($event, $server_id=0)
$table_details->data[] = $data;
$data = [];
$data[0] = __('Custom Field');
$data[1] = '<div class="flex-row-center">'.html_print_input_text('custom_field', $event['custom_field'], '', false, 255, true, false, false, '', 'w60p');
$data[1] .= html_print_button(
__('Update'),
'update_custom_field',
false,
'update_custom_field('.$event['id_evento'].', '.$event['server_id'].');',
[
'icon' => 'next',
'mode' => 'link',
],
true
).'</div>';
$table_details->data[] = $data;
$details = '<div id="extended_event_details_page" class="extended_event_pages">'.html_print_table($table_details, true).'</div>';
if (is_metaconsole() === true && empty($server_id) === false) {
@ -6203,3 +6223,57 @@ function event_get_counter_extraId(array $event, ?array $filters)
return $counters;
}
/**
* Update event detail custom field
*
* @param mixed $id_event Event ID or array of events.
* @param string $custom_field Custom_field to be update.
*
* @return boolean Whether or not it was successful
*/
function events_custom_field(
$id_event,
$custom_field,
) {
global $config;
// Cleans up the selection for all unwanted values also casts any single
// values as an array.
$id_event = (array) safe_int($id_event, 1);
// Check ACL.
foreach ($id_event as $k => $id) {
$event_group = events_get_group($id);
if (check_acl($config['id_user'], $event_group, 'EW') == 0) {
db_pandora_audit(
AUDIT_LOG_ACL_VIOLATION,
'Attempted updating event #'.$id
);
unset($id_event[$k]);
}
}
if (empty($id_event) === true) {
return false;
}
// Get the current event comments.
$first_event = $id_event;
if (is_array($id_event) === true) {
$first_event = reset($id_event);
}
// Update comment.
$ret = db_process_sql_update(
'tevento',
['custom_field' => $custom_field],
['id_evento' => $first_event]
);
if (($ret === false) || ($ret === 0)) {
return false;
}
return true;
}

View File

@ -492,6 +492,37 @@ function event_comment(current_event) {
return false;
}
// Save custom_field into an event.
function update_custom_field(event_id, server_id) {
var custom_field_value = $("#text-custom_field").val();
var params = {
page: "include/ajax/events",
update_custom_field: 1,
custom_field_value: custom_field_value,
event_id: event_id,
server_id: server_id
};
$("#button-update_custom_field").attr("disabled", "disabled");
$("#response_loading").show();
jQuery.ajax({
data: params,
type: "POST",
url: getUrlAjax(),
dataType: "html",
success: function(data) {
if (data === "update_error") {
alert("Custom field not valid");
}
$("#button-update_custom_field").removeAttr("disabled");
$("#response_loading").hide();
$("#button-events_form_search_bt").trigger("click");
}
});
}
var processed = 0;
function update_event(table, id_evento, type, event_rep, row, server_id) {
var inputs = $("#events_form :input");

View File

@ -837,6 +837,7 @@ class EventsListWidget extends Widget
'mini_severity' => __('Severity mini'),
'module_custom_id' => __('Module custom ID'),
'custom_data' => __('Custom data'),
'custom_field' => __('Custom field'),
];
}

View File

@ -377,6 +377,7 @@ if (is_ajax() === true) {
'te.owner_user',
'if(te.ack_utimestamp > 0, te.ack_utimestamp,"") as ack_utimestamp',
'te.custom_data',
'te.custom_field',
'te.data',
'te.module_status',
'ta.alias as agent_name',

View File

@ -723,6 +723,7 @@ CREATE TABLE IF NOT EXISTS `tevento` (
`custom_data` TEXT,
`data` TINYTEXT,
`module_status` INT NOT NULL DEFAULT 0,
`custom_field` TEXT,
PRIMARY KEY (`id_evento`),
KEY `idx_agente` (`id_agente`),
KEY `idx_agentmodule` (`id_agentmodule`),