From 0c39b0a698b046eac550ad4985baaa2fc6cb5ca2 Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Tue, 27 Feb 2024 10:47:17 +0100 Subject: [PATCH] mvc - add setBaseHook() to ApiMutableModelControllerBase controller implementation, similar to setActionHook(). Since setBase/addBase need a field to message errors on, its likely better to throw an exception when not able to complete the request in full (and data may not be saved). Usage examples include writing data in other fields not being offered by the gui (for example encode data base64) or autogenerated content as default, volatile model fields can be used to make sure the hook is able to process the non-persistent data (which can still be validated separately). --- .../Base/ApiMutableModelControllerBase.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/opnsense/mvc/app/controllers/OPNsense/Base/ApiMutableModelControllerBase.php b/src/opnsense/mvc/app/controllers/OPNsense/Base/ApiMutableModelControllerBase.php index 7b4a608b9..9f0817471 100644 --- a/src/opnsense/mvc/app/controllers/OPNsense/Base/ApiMutableModelControllerBase.php +++ b/src/opnsense/mvc/app/controllers/OPNsense/Base/ApiMutableModelControllerBase.php @@ -296,6 +296,18 @@ abstract class ApiMutableModelControllerBase extends ApiControllerBase { } + /** + * Hook to be overridden if the controller is to take an action when + * setBase/addBase is called. This hook is called after a model has been + * constructed and validated but before it serialized to the configuration + * and written to disk + * @throws UserException when action is not possible (and save should be aborted) + */ + protected function setBaseHook($node) + { + } + + /** * Update model settings * @return array status / validation errors @@ -409,6 +421,7 @@ abstract class ApiMutableModelControllerBase extends ApiControllerBase $result = $this->validate($node, $post_field); if (empty($result['validations'])) { + $this->setBaseHook($node); // save config if validated correctly $this->save(false, true); $result = array( @@ -492,6 +505,7 @@ abstract class ApiMutableModelControllerBase extends ApiControllerBase } $result = $this->validate($node, $post_field, true); if (empty($result['validations'])) { + $this->setBaseHook($node); // save config if validated correctly $this->save(false, true); $result = ["result" => "saved"];