Assign invalid device_id if a rule is created with a mapping.

Update rule to invalidate `device_id` if a map has been assigned afterwards.
Update rule to restore `device_id` if no more maps are assigned to it.
Remove all maps (if any) if a rule is deleted.
Fixed SQL-schema
This commit is contained in:
f0o
2015-04-04 11:37:07 +00:00
parent 00954ff4d6
commit 441c27a7d4
5 changed files with 45 additions and 18 deletions

View File

@@ -15,17 +15,27 @@
if(is_admin() === false) {
die('ERROR: You need to be admin');
}
if(!is_numeric($_POST['map_id'])) {
echo('ERROR: No map selected');
exit;
$ret = array();
$brk = false;
if( !is_numeric($_POST['map_id']) ) {
array_unshift($ret,'ERROR: No map selected');
} else {
if(dbDelete('alert_map', "`id` = ?", array($_POST['map_id']))) {
echo('Map has been deleted.');
exit;
} else {
echo('ERROR: Map has not been deleted.');
exit;
}
if( dbFetchCell('SELECT COUNT(B.id) FROM alert_map,alert_map AS B WHERE alert_map.rule=B.rule && alert_map.id = ?',array($_POST['map_id'])) <= 1 ) {
$rule = dbFetchRow('SELECT alert_rules.id,alert_rules.device_id FROM alert_map,alert_rules WHERE alert_map.rule=alert_rules.id && alert_map.id = ?',array($_POST['map_id']));
$rule['device_id'] = str_replace(":",'',$rule['device_id']);
if( dbUpdate(array('device_id'=>$rule['device_id']),'alert_rules','id = ?',array($rule['id'])) >= 0 ) {
$ret[] = "Restored Rule: <i>".$rule['id'].": device_id = '".$rule['device_id']."'</i>";
} else {
array_unshift($ret, 'ERROR: Rule '.$rule['id'].' has not been restored.');
$brk = true;
}
}
if( $brk === false && dbDelete('alert_map', "`id` = ?", array($_POST['map_id'])) ) {
$ret[] = 'Map has been deleted.';
} else {
array_unshift($ret, 'ERROR: Map has not been deleted.');
}
}
foreach( $ret as $msg ) {
echo $msg."<br/>";
}