Commit 63db313c authored by Ad Schellevis's avatar Ad Schellevis

(ids) add multi toggle for multiple items at once

parent 558b3033
...@@ -249,33 +249,42 @@ class SettingsController extends ApiControllerBase ...@@ -249,33 +249,42 @@ class SettingsController extends ApiControllerBase
/** /**
* toggle usage of rule file or set enabled / disabled depending on parameters * toggle usage of rule file or set enabled / disabled depending on parameters
* @param $filename (target) rule file name * @param $filenames (target) rule file name, or list of filenames separated by a comma
* @param $enabled desired state enabled(1)/disabled(1), leave empty for toggle * @param $enabled desired state enabled(1)/disabled(1), leave empty for toggle
* @return array status 0/1 or error * @return array status 0/1 or error
* @throws \Exception * @throws \Exception
* @throws \Phalcon\Validation\Exception * @throws \Phalcon\Validation\Exception
*/ */
public function toggleInstalledRulesetAction($filename, $enabled = null) public function toggleInstalledRulesetAction($filenames, $enabled = null)
{ {
$update_count = 0;
$result = array("status" => "none"); $result = array("status" => "none");
if ($this->request->isPost()) { if ($this->request->isPost()) {
$backend = new Backend(); $backend = new Backend();
$response = $backend->configdRun("ids list installablerulesets"); $response = $backend->configdRun("ids list installablerulesets");
$data = json_decode($response, true); $data = json_decode($response, true);
if ($data != null && array_key_exists("items", $data) && array_key_exists($filename, $data['items'])) { foreach (explode(",", $filenames) as $filename) {
$node = $this->getModel()->getFileNode($filename); if ($data != null && array_key_exists("items", $data) && array_key_exists($filename, $data['items'])) {
if ($enabled == "0" || $enabled == "1") { $node = $this->getModel()->getFileNode($filename);
$node->enabled = (string)$enabled; if ($enabled == "0" || $enabled == "1") {
} elseif ((string)$node->enabled == "1") { $node->enabled = (string)$enabled;
$node->enabled = "0"; } elseif ((string)$node->enabled == "1") {
$node->enabled = "0";
} else {
$node->enabled = "1";
}
// only update result state if all items until now are ok
if ($result['status'] != 'error') {
$result['status'] = $node->enabled;
}
$update_count++;
} else { } else {
$node->enabled = "1"; $result['status'] = "error";
} }
$result['status'] = $node->enabled; }
if ($update_count > 0) {
$this->getModel()->serializeToConfig(); $this->getModel()->serializeToConfig();
Config::getInstance()->save(); Config::getInstance()->save();
} else {
$result['status'] = "error";
} }
} }
return $result; return $result;
...@@ -283,23 +292,45 @@ class SettingsController extends ApiControllerBase ...@@ -283,23 +292,45 @@ class SettingsController extends ApiControllerBase
/** /**
* toggle rule enable status * toggle rule enable status
* @param $sid * @param $sids
* @return array * @param $enabled desired state enabled(1)/disabled(1), leave empty for toggle
* @return array empty
*/ */
public function toggleRuleAction($sid) public function toggleRuleAction($sids, $enabled = null)
{ {
$ruleinfo = $this->getRuleInfoAction($sid); if ($this->request->isPost()) {
if (count($ruleinfo) > 0) { $update_count = 0;
if ($ruleinfo['enabled_default'] != $ruleinfo['enabled']) { foreach (explode(",", $sids) as $sid) {
// if we're switching back to default, remove alter rule $ruleinfo = $this->getRuleInfoAction($sid);
$this->getModel()->removeRule($sid) ; if (count($ruleinfo) > 0) {
} elseif ($ruleinfo['enabled'] == 1) { if ($enabled == null) {
$this->getModel()->disableRule($sid) ; // toggle state
} else { if ($ruleinfo['enabled'] == 1) {
$this->getModel()->enableRule($sid) ; $new_state = 0;
} else {
$new_state = 1;
}
} elseif ($enabled == 1) {
$new_state = 1;
} else {
$new_state = 0;
}
if ($ruleinfo['enabled_default'] == $new_state) {
// if we're switching back to default, remove alter rule
$this->getModel()->removeRule($sid);
} elseif ($new_state == 1) {
$this->getModel()->enableRule($sid);
} else {
$this->getModel()->disableRule($sid);
}
$update_count++;
}
}
if ($update_count > 0) {
$this->getModel()->serializeToConfig();
Config::getInstance()->save();
} }
$this->getModel()->serializeToConfig();
Config::getInstance()->save();
} }
return array(); return array();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment