diff --git a/front/js/pialert_common.js b/front/js/pialert_common.js index 380738d2..313f8f29 100755 --- a/front/js/pialert_common.js +++ b/front/js/pialert_common.js @@ -300,6 +300,13 @@ function showMessage (textMessage="") { // ----------------------------------------------------------------------------- // General utilities // ----------------------------------------------------------------------------- + +// check if JSON object +function isJsonObject(value) { + return typeof value === 'object' && value !== null && !Array.isArray(value); +} + + // remove unnecessary lines from the result function sanitize(data) { diff --git a/front/php/templates/language/en_us.json b/front/php/templates/language/en_us.json index b6587ca9..6ab01bc5 100755 --- a/front/php/templates/language/en_us.json +++ b/front/php/templates/language/en_us.json @@ -421,6 +421,8 @@ "settings_old" : "The settings in the DB (shown on this page) are outdated. This is probably caused by a running scan. The settings were saved in the pialert.conf file, but the background process didn not have time to import it yet to the DB. You can wait until the settings get refreshed so you do not overwrite your old values. Feel free to save your settings either way if you don not mind losing the settings between the last save and now. There are also backup files created if you need to compare your settings later.", "settings_imported" : "Last time settings were imported from the pialert.conf file:", "settings_expand_all" : "Expand all", + "Setting_Override" : "Override default", + "Setting_Override_Description" : "TBD", "General_display_name" : "General", "General_icon" : "", "ENABLE_ARPSCAN_name" : "Enable ARP scan", diff --git a/front/plugins/known_template/config.json b/front/plugins/known_template/config.json index 0afb80dd..f83ad0f6 100755 --- a/front/plugins/known_template/config.json +++ b/front/plugins/known_template/config.json @@ -171,7 +171,7 @@ "type": "text.template", "maxLength": 50, "default_value": "(unknown)", - "value": { + "override_value": { "value":"(unknown)", "override": true }, diff --git a/front/settings.php b/front/settings.php index 5eb7fb1d..1cca1579 100755 --- a/front/settings.php +++ b/front/settings.php @@ -173,66 +173,111 @@ while ($row = $result -> fetchArray (SQLITE3_ASSOC)) { // go thru all settings and collect settings per settings group settingsData.forEach((set) => { + let val = set['Value']; + const codeName = set['Code_Name']; + const setType = set['Type'].toLowerCase(); + const isMetadata = codeName.includes('__metadata'); + // is this isn't a metadata entry, get corresponding metadata object from the dummy setting + const setObj = isMetadata ? {} : JSON.parse(getSetting(`${codeName}__metadata`)); + + // constructing final HTML for the setting setHtml = "" if(set["Group"] == group) { - // hide metadata by default by assigning it a special class - const isMetadata = set['Code_Name'].includes('__metadata'); + // hide metadata by default by assigning it a special class isMetadata ? metadataClass = 'metadata' : metadataClass = ''; isMetadata ? infoIcon = '' : infoIcon = ` ` ; + // NAME & DESCRIPTION columns setHtml += ` -
+
- +
- ${set['Code_Name']}${infoIcon} + ${codeName}${infoIcon}
- ${getString(set['Code_Name'] + '_description', set['Description'])} + ${getString(codeName + '_description', set['Description'])}
`; - // Render different input types based on the settings type - let input = ""; + // OVERRIDE + // surface settings override functionality if the setting is a template that can be overriden with user defined values + // if the setting is a json of the correct structure, handle like a template setting - const setType = set['Type'].toLowerCase(); + let overrideHtml = ""; + //pre-check if this is a json object that needs value extraction + + let overridable = false; + let override = false; + let overrideValue = val; + + // TODO finish + if ('override_value' in setObj) { + overridable = true; + const overrideObj = setObj["override_value"] + const override = overrideObj["override"]; + overrideValue = overrideObj["value"]; + + console.log(isJsonObject(val)) + console.log(setObj) + console.log(group) + } + + if(overridable) + { + let checked = override; + overrideHtml = `
+ + ${getString("Setting_Override")} + + + + +
`; + } + + + // INPUT + // pre-processing done, render setting based on type + + let inputHtml = ""; if (setType.startsWith('text') || setType.startsWith('string') || setType.startsWith('date-time') ) { if(setType.includes(".select")) { - input = generateInputOptions(set, input, isMultiSelect = false) + inputHtml = generateInputOptions(set, inputHtml, isMultiSelect = false) } else if(setType.includes(".multiselect")) { - input = generateInputOptions(set, input, isMultiSelect = true) + inputHtml = generateInputOptions(set, inputHtml, isMultiSelect = true) } else{ - input = ``; + inputHtml = ``; } } else if (setType === 'integer') { - input = ``; + inputHtml = ``; } else if (setType === 'password') { - input = ``; + inputHtml = ``; } else if (setType === 'readonly') { - input = ``; + inputHtml = ``; } else if (setType === 'boolean' || setType === 'integer.checkbox') { - let checked = set['Value'] === 'True' || set['Value'] === '1' ? 'checked' : ''; - input = ``; + let checked = val === 'True' || val === '1' ? 'checked' : ''; + inputHtml = ``; } else if (setType === 'integer.select') { - input = generateInputOptions(set, input) + inputHtml = generateInputOptions(set, inputHtml) } else if (setType === 'subnets') { - input = ` + inputHtml = `
@@ -245,55 +290,55 @@ while ($row = $result -> fetchArray (SQLITE3_ASSOC)) {
- `; - options = createArray(set['Value']); + options = createArray(val); options.forEach(option => { - input += ``; + inputHtml += ``; }); - input += '
' + + inputHtml += '
' + '
'; - } else if (setType === 'list') { + } else if (setType === 'list' || setType === 'list.readonly') { - settingKeyOfLists.push(set['Code_Name']); + settingKeyOfLists.push(codeName); - input = ` + inputHtml = `
- +
- +
- `; - let options = createArray(set['Value']); + let options = createArray(val); options.forEach(option => { - input += ``; + inputHtml += ``; }); - input += '
' + - `
`; + inputHtml += '
' + + `
`; } else if (setType === 'json') { - input = ``; + inputHtml = ``; } + // EVENTS + // process events (e.g. run ascan, or test a notification) if associated with the setting let eventsHtml = ""; - const eventsList = createArray(set['Events']); - - console.log(eventsList) + const eventsList = createArray(set['Events']); if (eventsList.length > 0) { eventsList.forEach(event => { eventsHtml += ` @@ -302,8 +347,9 @@ while ($row = $result -> fetchArray (SQLITE3_ASSOC)) { `; }); } - - setHtml += input + eventsHtml + ` + + // construct final HTML for the setting + setHtml += inputHtml + eventsHtml + overrideHtml + `
` @@ -318,7 +364,11 @@ while ($row = $result -> fetchArray (SQLITE3_ASSOC)) { } - // + + + + // --------------------------------------------------------- + // generate a list of options for a input select function generateInputOptions(set, input, isMultiSelect = false) { @@ -338,7 +388,8 @@ while ($row = $result -> fetchArray (SQLITE3_ASSOC)) { return input; } - // todo fix + // --------------------------------------------------------- + // Generate an array object from a string representation of an array function createArray(input) { // Empty array if (input === '[]') { diff --git a/pialert/helper.py b/pialert/helper.py index 947c66b7..4cac9713 100755 --- a/pialert/helper.py +++ b/pialert/helper.py @@ -336,4 +336,8 @@ class noti_struc: def __init__(self, json, text, html): self.json = json self.text = text - self.html = html \ No newline at end of file + self.html = html + +#------------------------------------------------------------------------------- +def isJsonObject(value): + return isinstance(value, dict) \ No newline at end of file diff --git a/pialert/initialise.py b/pialert/initialise.py index 1125538d..f9224610 100755 --- a/pialert/initialise.py +++ b/pialert/initialise.py @@ -9,7 +9,7 @@ import json import conf from const import fullConfPath -from helper import collect_lang_strings, updateSubnets, initOrSetParam +from helper import collect_lang_strings, updateSubnets, initOrSetParam, isJsonObject from logger import mylog from api import update_api from scheduler import schedule_class @@ -18,13 +18,13 @@ from plugin import get_plugins_configs, print_plugin_info #=============================================================================== # Initialise user defined values #=============================================================================== -# We need access to the DB to save new values so need to define DB access methods first -#------------------------------------------------------------------------------- #------------------------------------------------------------------------------- # Import user values # Check config dictionary -def ccd(key, default, config_dir, name, inputtype, options, group, events=[], desc = "", regex = "", setJsonMetadata = {}): +def ccd(key, default, config_dir, name, inputtype, options, group, events=[], desc = "", regex = "", setJsonMetadata = {}, overrideTemplate = {}): + + # use default inintialization value result = default if events is None: @@ -222,7 +222,7 @@ def importConfigs (db): events = set.get("events"), desc = set["description"][0]["string"], regex = "", - setJsonMetadata = set) + setJsonMetadata = set) # Save the user defined value into the object set["value"] = v diff --git a/pialert/mac_vendor.py b/pialert/mac_vendor.py index aa576039..056b1223 100755 --- a/pialert/mac_vendor.py +++ b/pialert/mac_vendor.py @@ -21,7 +21,7 @@ def update_devices_MAC_vendors (db, pArg = ''): # Update vendors DB (iab oui) mylog('verbose', [' Updating vendors DB (iab & oui)']) - update_args = ['sh', pialertPath + '/update_vendors.sh', pArg] + update_args = ['sh', pialertPath + '/back/update_vendors.sh', pArg] # Execute command if conf.LOG_LEVEL == 'debug':