Commit 618036bc authored by Ad Schellevis's avatar Ad Schellevis

(mvc) refactor ApiMutableModelControllerBase for https://github.com/opnsense/core/pull/1126

parent 5c68c8f2
...@@ -111,36 +111,51 @@ abstract class ApiMutableModelControllerBase extends ApiControllerBase ...@@ -111,36 +111,51 @@ abstract class ApiMutableModelControllerBase extends ApiControllerBase
* validate and save model after update or insertion. * validate and save model after update or insertion.
* Use the reference node and tag to rename validation output for a specific node to a new offset, which makes * Use the reference node and tag to rename validation output for a specific node to a new offset, which makes
* it easier to reference specific uuids without having to use them in the frontend descriptions. * it easier to reference specific uuids without having to use them in the frontend descriptions.
* @param $mdl The model to save
* @param $node reference node, to use as relative offset * @param $node reference node, to use as relative offset
* @param $actionHook A closure containing an action to do after validation but before saving. Must return null if successful, or an error message if not.
* @return array result / validation output * @return array result / validation output
*/ */
protected function save($mdl, $node = null, $actionHook = null) protected function validateAndSave($node = null)
{ {
$result = array("result"=>"failed","validations" => array()); $result = $this->validate();
if (empty($result['result'])) {
return $this->save();
}
return $result;
}
/**
* validate this model
* @param $node reference node, to use as relative offset
* @return array result / validation output
*/
protected function validate($node = null)
{
$result = array("result"=>"");
// perform validation // perform validation
$valMsgs = $mdl->performValidation(); $valMsgs = $this->getModel()->performValidation();
foreach ($valMsgs as $field => $msg) { foreach ($valMsgs as $field => $msg) {
if (!array_key_exists("validations", $result)) {
$result["validations"] = array();
$result["result"] = "failed";
}
// replace absolute path to attribute for relative one at uuid. // replace absolute path to attribute for relative one at uuid.
if ($node != null) { if ($node != null) {
$fieldnm = str_replace($node->__reference, static::$internalModelName, $msg->getField()); $fieldnm = str_replace($node->__reference, static::$internalModelName, $msg->getField());
$result["validations"][$fieldnm] = $msg->getMessage(); $result["validations"][$fieldnm] = $msg->getMessage();
} else { } else {
$result["validations"][$msg->getField()] = $msg->getMessage(); $result["validations"][static::$internalModelName.".".$msg->getField()] = $msg->getMessage();
} }
} }
if (count($result['validations']) != 0) {
return $result; return $result;
} }
// run the action hook (default one just always returns null)
$errorMessage = $actionHook ? $actionHook($mdl) : null; /**
if ($errorMessage != null) { * save model after update or insertion, validate() first to avoid raising exceptions
return array("result"=>failed, "error"=>$errorMessage); * @return array result / validation output
} */
// serialize model to config and save when there are no validation errors protected function save()
// and the action hook completed successfully {
$mdl->serializeToConfig(); $this->getModel()->serializeToConfig();
Config::getInstance()->save(); Config::getInstance()->save();
return array("result"=>"saved"); return array("result"=>"saved");
} }
...@@ -166,7 +181,15 @@ abstract class ApiMutableModelControllerBase extends ApiControllerBase ...@@ -166,7 +181,15 @@ abstract class ApiMutableModelControllerBase extends ApiControllerBase
// load model and update with provided data // load model and update with provided data
$mdl = $this->getModel(); $mdl = $this->getModel();
$mdl->setNodes($this->request->getPost(static::$internalModelName)); $mdl->setNodes($this->request->getPost(static::$internalModelName));
$result = $this->save($mdl, null, $this->setActionHook); $result = $this->validate();
if (empty($result['result'])) {
$errorMessage = $this->setActionHook();
if (!empty($errorMessage)) {
$result['error'] = $errorMessage;
} else {
return $this->save();
}
}
} }
return $result; return $result;
} }
......
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