Skip to content

Commit

Permalink
MVC, obey 'user-config-readonly' for subclasses of ApiMutableModelCon…
Browse files Browse the repository at this point in the history
…trollerBase.

Eventually (19.7?), user-config-readonly should go to the features of past times, it has has quite some downsides and will keep haunting everybody as long as it's there.

This patch makes sure ApiMutableModelControllerBase->save() adds an additional ACL check before doing the actual save, raising a user exception when not allowed.
Not all our classes use this, but can probably do so, same counts for plugins. We are not, under any circumstances, are going to move an acl check to the Config or model classes, since these have no relationship with the user.
  • Loading branch information
AdSchellevis committed Mar 14, 2019
1 parent 64c4d89 commit 3af0219
Showing 1 changed file with 16 additions and 11 deletions.
Expand Up @@ -31,6 +31,8 @@
namespace OPNsense\Base;

use \OPNsense\Core\Config;
use OPNsense\Core\ACL;
use \OPNsense\Base\UserException;

/**
* Class ApiMutableModelControllerBase, inherit this class to implement
Expand Down Expand Up @@ -178,9 +180,16 @@ protected function validate($node = null, $prefix = null)
*/
protected function save()
{
$this->getModel()->serializeToConfig();
Config::getInstance()->save();
return array("result"=>"saved");
if (!(new ACL())->hasPrivilege($this->getUserName(), 'user-config-readonly')) {
$this->getModel()->serializeToConfig();
Config::getInstance()->save();
return array("result"=>"saved");
} else {
// XXX remove user-config-readonly in some future release
throw new UserException(
sprintf("User %s denied for write access (user-config-readonly set)", $this->getUserName())
);
}
}

/**
Expand Down Expand Up @@ -293,8 +302,7 @@ public function addBase($post_field, $path)

if (empty($result['validations'])) {
// save config if validated correctly
$mdl->serializeToConfig();
Config::getInstance()->save();
$this->save();
$result = array(
"result" => "saved",
"uuid" => $node->getAttribute('uuid')
Expand Down Expand Up @@ -327,8 +335,7 @@ public function delBase($path, $uuid)
$tmp = $tmp->{$step};
}
if ($tmp->del($uuid)) {
$mdl->serializeToConfig();
Config::getInstance()->save();
$this->save();
$result['result'] = 'deleted';
} else {
$result['result'] = 'not found';
Expand Down Expand Up @@ -358,8 +365,7 @@ public function setBase($post_field, $path, $uuid)
$result = $this->validate($node, $post_field);
if (empty($result['validations'])) {
// save config if validated correctly
$mdl->serializeToConfig();
Config::getInstance()->save();
$this->save();
$result = array("result" => "saved");
} else {
$result["result"] = "failed";
Expand Down Expand Up @@ -405,8 +411,7 @@ public function toggleBase($path, $uuid, $enabled = null)
}
// if item has toggled, serialize to config and save
if ($result['changed']) {
$mdl->serializeToConfig();
Config::getInstance()->save();
$this->save();
}
}
}
Expand Down

0 comments on commit 3af0219

Please sign in to comment.