From 7d66c4bf9b228123dcd0a3757e532dcf67c3f0d9 Mon Sep 17 00:00:00 2001 From: kulikov-a <36099472+kulikov-a@users.noreply.github.com> Date: Tue, 6 Apr 2021 10:42:05 +0300 Subject: [PATCH 01/13] fw_log.volt: add filter templates --- .../views/OPNsense/Diagnostics/fw_log.volt | 250 +++++++++++++++++- 1 file changed, 237 insertions(+), 13 deletions(-) diff --git a/src/opnsense/mvc/app/views/OPNsense/Diagnostics/fw_log.volt b/src/opnsense/mvc/app/views/OPNsense/Diagnostics/fw_log.volt index 416e57d5f..7c5fa9d36 100644 --- a/src/opnsense/mvc/app/views/OPNsense/Diagnostics/fw_log.volt +++ b/src/opnsense/mvc/app/views/OPNsense/Diagnostics/fw_log.volt @@ -1,6 +1,6 @@ {# # - # Copyright (c) 2014-2016 Deciso B.V. + # Copyright (c) 2014-2021 Deciso B.V. # All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, @@ -37,7 +37,7 @@ let hostnameMap = {}; /** - * reverse lookup address fields (replace adres part for hostname if found) + * reverse lookup address fields (replace address part for hostname if found) */ function reverse_lookup() { let to_fetch = []; @@ -354,6 +354,95 @@ fetch_log(); }); + // templates actions + $("#templates").change(function () { + if ($('#templ_save_start').is(':visible')) { + //apply chosen template + let t_data = $(this).find('option:selected').data('template') ? $(this).find('option:selected').data('template') : {'filters': "0", 'or': "0"}; + set_selection(t_data.filters.split(','), t_data.or); + } else { + //choose template to modify or create new one. Show Name input if New option clicked + if ($('#templates').val() === "00001") { + $('#templates').selectpicker('hide'); + $('#templ_name').show().focus(); + } + } + }); + + $('#templ_save_start').click(function () { + if ($(".badge").text() == '') { + BootstrapDialog.show({ + type: BootstrapDialog.TYPE_DANGER, + title: "{{ lang._('Save filters template') }}", + message: "{{ lang._('Filters not set') }}", + buttons: [{ + label: "{{ lang._('Close') }}", + action: function (dialogRef) { + dialogRef.close(); + } + }] + }); + } else { + $('.templates').hide(); + $('.templ_save').show(); + $('#templ_name').focus(); + if ($("#templates option").length == 3){ + //no stored templates. skip to new template name + $('#templates').val("00001").selectpicker('refresh').change(); + } + } + }); + + $("#templ_save_cancel").click(function () { + $('#templ_name').val("").hide(); + $('.templ_save').hide(); + $('.templates').show(); + $('#templates').val('').selectpicker('refresh').selectpicker('show'); + }); + + $("#templ_name").on('keyup', function (e) { + if (e.key === 'Enter' || e.keyCode === 13) { + $('#templ_save_apply').click(); + } else if (e.keyCode === 27) { + $('#templ_name').val("").hide(); + $('#templates').val('').selectpicker('refresh').selectpicker('show'); + } + }); + + $("#templ_save_apply").click(function () { + let fltrs = ""; + $('.badge').each(function () { + fltrs += $(this).text() + ","; + }); + fltrs = fltrs.slice(0, -1); + let or = $('#filter_or_type').prop("checked") ? "1" : "0"; + let t_data = { + 'template': { + 'filters': fltrs, + 'or': or + } + }; + $('#templates').selectpicker('refresh').selectpicker('show'); + if ($("#templ_name").val().length >= 1 && $("#templ_name").is(':visible')) { + //new template + t_data.template.name = $("#templ_name").val(); + $('#templ_name').val("").hide(); + addTemplate(t_data); + } else if ($("#templ_name").val().length == 0 && $("#templ_name").is(':hidden') && $("#templates").val().length == 36) { + //edit template + let t_id = $("#templates").val(); + t_data.template.name = $("#templates option:selected").text(); + editTemplate(t_id, t_data); + } + }); + + $("#template_delete").click(function () { + let t_id = $('#templates').val(); + if (t_id.length == 36) { + delTemplate(t_id); + } + }); + // fetch interface mappings on load ajaxGet('/api/diagnostics/interface/getInterfaceNames', {}, function(data, status) { interface_descriptions = data; @@ -389,18 +478,27 @@ filter_value.show(); } }).change(); + fetchTemplates("00000", true); } }); + }); + // startup poller + poller(); + + /** * set new selection * @param items list of lexical expressions + * @param operator enable or disable global OR operator */ - function set_selection(items) + function set_selection(items, operator) { // remove old selection $("#filters > span.badge").click(); + // operator default value + operator = operator || "0"; // collect valid condition types let conditions = []; $("#filter_condition > option").each(function(){ @@ -413,15 +511,121 @@ $("#filter_condition").val(parts[1]); $("#filter_value").val(parts[2]); $("#add_filter_condition").click(); + } else if (value.toLowerCase() == "or=1") { + operator = "1"; + } + }); + $("#filter_or_type").prop('checked', operator === "1" ? true : false); + $(".selectpicker").selectpicker('refresh'); + $("#filter_tag").change(); + } + + /** + * add new filters template + * @param t_data template's parameters + */ + function addTemplate(t_data) { + ajaxCall('/api/diagnostics/lvtemplates/addItem/', t_data, function(data, status) { + if ((status == "success") && (data.result == "saved")) { + fetchTemplates(data.uuid); + } else { + BootstrapDialog.show({ + type: BootstrapDialog.TYPE_DANGER, + title: "{{ lang._('Add filters template') }}", + message: "{{ lang._('Template save failed. Message: ') }}" + data.result, + buttons: [{ + label: "{{ lang._('Close') }}", + action: function (dialogRef) { + dialogRef.close(); + } + }] + }); + fetchTemplates("00000"); + } + }) + } + + /** + * set template new values + * @param t_id template uuid + * @param t_data template's parameters + */ + function editTemplate(t_id, t_data) { + ajaxCall('/api/diagnostics/lvtemplates/setItem/' + t_id, t_data, function(data, status) { + if ((status == "success") && (data.result == "saved")) { + fetchTemplates(t_id); + } else { + BootstrapDialog.show({ + type: BootstrapDialog.TYPE_DANGER, + title: "{{ lang._('Filters template edit') }}", + message: "{{ lang._('Template edit failed. Message: ') }}" + data.result, + buttons: [{ + label: "{{ lang._('Close') }}", + action: function (dialogRef) { + dialogRef.close(); + } + }] + }); + fetchTemplates(t_id); + } + }) + } + + /** + * delete filters template + * @param t_id template uuid + */ + function delTemplate(t_id) { + ajaxCall('/api/diagnostics/lvtemplates/delItem/' + t_id, {}, function(data, status) { + if ((status == "success") && (data.result == "deleted")) { + //don't reset current filters so template can be restored right after delete + $("#templates option[value=" + t_id + "]").remove(); + $("#templates").val("").selectpicker('refresh'); + } else { + BootstrapDialog.show({ + type: BootstrapDialog.TYPE_DANGER, + title: "{{ lang._('Filters template delete') }}", + message: "{{ lang._('Template delete failed. Result: ') }}" + data.result, + buttons: [{ + label: "{{ lang._('Close') }}", + action: function (dialogRef) { + dialogRef.close(); + } + }] + }); + } + }) + } + + /** + * fetch templates from config + * @param opt select value to make :selected and apply + * @param parse_url flag to parse and apply url params on page load + */ + function fetchTemplates(opt, parse_url) { + opt = opt || "00000"; + //apply = apply || true; + $('#templ_name').val(""); + $('#templates').empty(); + $('#templates').append($('