Refactor alert templates to use Laravel Blade templating engine (#8803)

* Initial start on new templating support

* further updates

* more updates

* More working version

* Last fixes

* Small updates

* modified test to look for dbname.

* Schema update

* Added fix for not being able to disable Philips Hue transport

* Updated sql file

* Updated db_schema

* Set myclabs/deep-copy to be 1.7.x version in composer

* Fixes from murrant

* Forced nikic/php-parser to version 3.1.x in composer

* Updated composer to use custom fork of string-blade-compiler + fixed transport use

* Updated to always use correct template

* Merged legacy and blade templating engines

* Removed template type + fixed test-alert.php

* Added more template placeholders

* Added ability to reference data in templates as $alert->X

* Updated docs for templates

* Updated db_schema.yaml

* Added $alert->builder

* Clarify the use of $alert->builder

* Fixed the use of $alert->transport

* renamed schema file

* Added template validation

* Small update to fix travis issue

* Add Docs text to title bar

* Updated amqp to new of version

* Consistency in alert rule and template title bars
This commit is contained in:
Neil Lathwood
2018-07-14 22:15:43 +01:00
committed by GitHub
parent b56380c261
commit ad6ec7f72a
28 changed files with 1121 additions and 587 deletions

View File

@@ -44,8 +44,8 @@ $rule_id = $_POST['rule_id'];
$count = mres($_POST['count']);
$delay = mres($_POST['delay']);
$interval = mres($_POST['interval']);
$mute = mres($_POST['mute']);
$invert = mres($_POST['invert']);
$mute = mres(isset($_POST['mute']) ? $_POST['mute'] : null);
$invert = mres(isset($_POST['invert']) ? $_POST['invert'] : null);
$name = mres($_POST['name']);
$proc = mres($_POST['proc']);
$recovery = ($vars['recovery']);
@@ -138,7 +138,7 @@ if (is_numeric($rule_id) && $rule_id > 0) {
if (is_numeric($rule_id) && $rule_id > 0) {
$devices = [];
$groups = [];
foreach ((array)$_POST['maps'] as $item) {
foreach ((array)$vars['maps'] as $item) {
if (starts_with($item, 'g')) {
$groups[] = (int)substr($item, 1);
} else {

View File

@@ -24,48 +24,50 @@
use LibreNMS\Authentication\Auth;
header('Content-type: text/plain');
$status = 'error';
if (!Auth::user()->hasGlobalAdmin()) {
die('ERROR: You need to be admin');
header('Content-Type: application/json');
$response = array('status' => $status, 'message' => 'You need to be admin');
die(_json_encode($response));
}
$status = 'error';
$template_id = 0;
$name = mres($_POST['name']);
if (!empty($name)) {
if (is_numeric($_REQUEST['template_id']) && $_REQUEST['rule_id']) {
$name = mres($vars['name']);
if (isset($vars['template']) && empty(view(['template' => $vars['template']], [])->__toString())) {
$message = 'Template failed to be parsed, please check the syntax';
} elseif (!empty($name)) {
if ((isset($vars['template_id']) && is_numeric($vars['template_id'])) && (isset($vars['rule_id']) && $vars['rule_id'])) {
//Update the template/rule mapping
if (is_array($_REQUEST['rule_id'])) {
$_REQUEST['rule_id'] = implode(",", $_REQUEST['rule_id']);
if (is_array($vars['rule_id'])) {
$vars['rule_id'] = implode(",", $vars['rule_id']);
}
if (substr($_REQUEST['rule_id'], 0, 1) != ",") {
$_REQUEST['rule_id'] = ",".$_REQUEST['rule_id'];
if (substr($vars['rule_id'], 0, 1) != ",") {
$vars['rule_id'] = ",".$vars['rule_id'];
}
if (substr($_REQUEST['rule_id'], -1, 1) != ",") {
$_REQUEST['rule_id'] .= ",";
if (substr($vars['rule_id'], -1, 1) != ",") {
$vars['rule_id'] .= ",";
}
if (dbUpdate(array('rule_id' => mres($_REQUEST['rule_id']), 'name' => $name), "alert_templates", "id = ?", array($_REQUEST['template_id'])) >= 0) {
if (dbUpdate(array('rule_id' => mres($vars['rule_id']), 'name' => $name), "alert_templates", "id = ?", array($vars['template_id'])) >= 0) {
$message = "Updated template and rule id mapping";
} else {
$message ="Failed to update the template and rule id mapping";
}
} elseif ($_REQUEST['template'] && is_numeric($_REQUEST['template_id'])) {
} elseif ($vars['template'] && is_numeric($vars['template_id'])) {
//Update template-text
if (dbUpdate(array('template' => $_REQUEST['template'], 'name' => $name, 'title' => $_REQUEST['title'], 'title_rec' => $_REQUEST['title_rec']), "alert_templates", "id = ?", array($_REQUEST['template_id'])) >= 0) {
if (dbUpdate(array('template' => $vars['template'], 'name' => $name, 'title' => $vars['title'], 'title_rec' => $vars['title_rec']), "alert_templates", "id = ?", array($vars['template_id'])) >= 0) {
$status = 'ok';
$message = "Alert template updated";
} else {
$message = "Failed to update the template";
}
} elseif ($_REQUEST['template']) {
} elseif ($vars['template']) {
//Create new template
if ($name != 'Default Alert Template') {
$template_id = dbInsert(array('template' => $_REQUEST['template'], 'name' => $name, 'title' => $_REQUEST['title'], 'title_rec' => $_REQUEST['title_rec']), "alert_templates");
$template_id = dbInsert(array('template' => $vars['template'], 'name' => $name, 'title' => $vars['title'], 'title_rec' => $vars['title_rec']), "alert_templates");
if ($template_id != false) {
$status = 'ok';
$message = "Alert template has been created.";
@@ -84,5 +86,4 @@ if (!empty($name)) {
$response = array('status' => $status, 'message' => $message, 'newid' => $template_id);
header('Content-Type: application/json');
echo _json_encode($response);

View File

@@ -19,7 +19,7 @@ if (!Auth::user()->hasGlobalAdmin()) {
die('ERROR: You need to be admin');
}
$template_id = ($_POST['template_id']);
$template_id = ($vars['template_id']);
if (is_numeric($template_id) && $template_id > 0) {
$template = dbFetchRow('SELECT * FROM `alert_templates` WHERE `id` = ? LIMIT 1', array($template_id));
@@ -28,6 +28,7 @@ if (is_numeric($template_id) && $template_id > 0) {
'name' => $template['name'],
'title' => $template['title'],
'title_rec' => $template['title_rec'],
'type' => $template['type'],
);
header('Content-type: application/json');
echo _json_encode($output);