Commit bd5be5aa authored by Ad Schellevis's avatar Ad Schellevis

(Proxy) merge model changes, add constraints.

parent 730d291e
...@@ -28,8 +28,7 @@ ...@@ -28,8 +28,7 @@
*/ */
namespace OPNsense\Proxy\Api; namespace OPNsense\Proxy\Api;
use \OPNsense\Base\ApiControllerBase; use \OPNsense\Base\ApiMutableModelControllerBase;
use \OPNsense\Proxy\Proxy;
use \OPNsense\Cron\Cron; use \OPNsense\Cron\Cron;
use \OPNsense\Core\Config; use \OPNsense\Core\Config;
use \OPNsense\Base\UIModelGrid; use \OPNsense\Base\UIModelGrid;
...@@ -38,57 +37,10 @@ use \OPNsense\Base\UIModelGrid; ...@@ -38,57 +37,10 @@ use \OPNsense\Base\UIModelGrid;
* Class SettingsController * Class SettingsController
* @package OPNsense\Proxy * @package OPNsense\Proxy
*/ */
class SettingsController extends ApiControllerBase class SettingsController extends ApiMutableModelControllerBase
{ {
/** static protected $internalModelName = 'proxy';
* retrieve proxy settings static protected $internalModelClass = '\OPNsense\Proxy\Proxy';
* @return array
*/
public function getAction()
{
$result = array();
if ($this->request->isGet()) {
$mdlProxy = new Proxy();
$result['proxy'] = $mdlProxy->getNodes();
}
return $result;
}
/**
* update proxy configuration fields
* @return array
* @throws \Phalcon\Validation\Exception
*/
public function setAction()
{
$result = array("result"=>"failed");
if ($this->request->hasPost("proxy")) {
// load model and update with provided data
$mdlProxy = new Proxy();
$mdlProxy->setNodes($this->request->getPost("proxy"));
// perform validation
$valMsgs = $mdlProxy->performValidation();
foreach ($valMsgs as $field => $msg) {
if (!array_key_exists("validations", $result)) {
$result["validations"] = array();
}
$result["validations"]["proxy.".$msg->getField()] = $msg->getMessage();
}
// serialize model to config and save
if ($valMsgs->count() == 0) {
$mdlProxy->serializeToConfig();
$cnf = Config::getInstance();
$cnf->save();
$result["result"] = "saved";
}
}
return $result;
}
/** /**
* *
...@@ -98,7 +50,7 @@ class SettingsController extends ApiControllerBase ...@@ -98,7 +50,7 @@ class SettingsController extends ApiControllerBase
public function searchRemoteBlacklistsAction() public function searchRemoteBlacklistsAction()
{ {
$this->sessionClose(); $this->sessionClose();
$mdlProxy = new Proxy(); $mdlProxy = $this->getModel();
$grid = new UIModelGrid($mdlProxy->forward->acl->remoteACLs->blacklists->blacklist); $grid = new UIModelGrid($mdlProxy->forward->acl->remoteACLs->blacklists->blacklist);
return $grid->fetchBindRequest( return $grid->fetchBindRequest(
$this->request, $this->request,
...@@ -114,7 +66,7 @@ class SettingsController extends ApiControllerBase ...@@ -114,7 +66,7 @@ class SettingsController extends ApiControllerBase
*/ */
public function getRemoteBlacklistAction($uuid = null) public function getRemoteBlacklistAction($uuid = null)
{ {
$mdlProxy = new Proxy(); $mdlProxy = $this->getModel();
if ($uuid != null) { if ($uuid != null) {
$node = $mdlProxy->getNodeByReference('forward.acl.remoteACLs.blacklists.blacklist.' . $uuid); $node = $mdlProxy->getNodeByReference('forward.acl.remoteACLs.blacklists.blacklist.' . $uuid);
if ($node != null) { if ($node != null) {
...@@ -139,7 +91,7 @@ class SettingsController extends ApiControllerBase ...@@ -139,7 +91,7 @@ class SettingsController extends ApiControllerBase
public function setRemoteBlacklistAction($uuid) public function setRemoteBlacklistAction($uuid)
{ {
if ($this->request->isPost() && $this->request->hasPost("blacklist")) { if ($this->request->isPost() && $this->request->hasPost("blacklist")) {
$mdlProxy = new Proxy(); $mdlProxy = $this->getModel();
if ($uuid != null) { if ($uuid != null) {
$node = $mdlProxy->getNodeByReference('forward.acl.remoteACLs.blacklists.blacklist.' . $uuid); $node = $mdlProxy->getNodeByReference('forward.acl.remoteACLs.blacklists.blacklist.' . $uuid);
if ($node != null) { if ($node != null) {
...@@ -175,7 +127,7 @@ class SettingsController extends ApiControllerBase ...@@ -175,7 +127,7 @@ class SettingsController extends ApiControllerBase
$result = array("result" => "failed"); $result = array("result" => "failed");
if ($this->request->isPost() && $this->request->hasPost("blacklist")) { if ($this->request->isPost() && $this->request->hasPost("blacklist")) {
$result = array("result" => "failed", "validations" => array()); $result = array("result" => "failed", "validations" => array());
$mdlProxy = new Proxy(); $mdlProxy = $this->getModel();
$node = $mdlProxy->forward->acl->remoteACLs->blacklists->blacklist->Add(); $node = $mdlProxy->forward->acl->remoteACLs->blacklists->blacklist->Add();
$node->setNodes($this->request->getPost("blacklist")); $node->setNodes($this->request->getPost("blacklist"));
$valMsgs = $mdlProxy->performValidation(); $valMsgs = $mdlProxy->performValidation();
...@@ -207,7 +159,7 @@ class SettingsController extends ApiControllerBase ...@@ -207,7 +159,7 @@ class SettingsController extends ApiControllerBase
$result = array("result" => "failed"); $result = array("result" => "failed");
if ($this->request->isPost()) { if ($this->request->isPost()) {
$mdlProxy = new Proxy(); $mdlProxy = $this->getModel();
if ($uuid != null) { if ($uuid != null) {
if ($mdlProxy->forward->acl->remoteACLs->blacklists->blacklist->del($uuid)) { if ($mdlProxy->forward->acl->remoteACLs->blacklists->blacklist->del($uuid)) {
// if item is removed, serialize to config and save // if item is removed, serialize to config and save
...@@ -233,7 +185,7 @@ class SettingsController extends ApiControllerBase ...@@ -233,7 +185,7 @@ class SettingsController extends ApiControllerBase
$result = array("result" => "failed"); $result = array("result" => "failed");
if ($this->request->isPost()) { if ($this->request->isPost()) {
$mdlProxy = new Proxy(); $mdlProxy = $this->getModel();
if ($uuid != null) { if ($uuid != null) {
$node = $mdlProxy->getNodeByReference('forward.acl.remoteACLs.blacklists.blacklist.' . $uuid); $node = $mdlProxy->getNodeByReference('forward.acl.remoteACLs.blacklists.blacklist.' . $uuid);
if ($node != null) { if ($node != null) {
...@@ -262,7 +214,7 @@ class SettingsController extends ApiControllerBase ...@@ -262,7 +214,7 @@ class SettingsController extends ApiControllerBase
$result = array("result" => "failed"); $result = array("result" => "failed");
if ($this->request->isPost()) { if ($this->request->isPost()) {
$mdlProxy = new Proxy(); $mdlProxy = $this->getModel();
if ((string)$mdlProxy->forward->acl->remoteACLs->UpdateCron == "") { if ((string)$mdlProxy->forward->acl->remoteACLs->UpdateCron == "") {
$mdlCron = new Cron(); $mdlCron = new Cron();
// update cron relation (if this doesn't break consistency) // update cron relation (if this doesn't break consistency)
......
<?php
/**
* Copyright (C) 2016 Deciso B.V.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
namespace OPNsense\Proxy\Migrations;
use OPNsense\Base\BaseModelMigration;
class M1_0_0 extends BaseModelMigration
{
}
<model> <model>
<mount>//OPNsense/proxy</mount> <mount>//OPNsense/proxy</mount>
<version>1.0.0</version>
<description> <description>
(squid) proxy settings (squid) proxy settings
</description> </description>
...@@ -151,12 +152,26 @@ ...@@ -151,12 +152,26 @@
<MinimumValue>1</MinimumValue> <MinimumValue>1</MinimumValue>
<ValidationMessage>Specify the overall bandwidth for downloads in kilobits per second.</ValidationMessage> <ValidationMessage>Specify the overall bandwidth for downloads in kilobits per second.</ValidationMessage>
<Required>N</Required> <Required>N</Required>
<Constraints>
<check001>
<ValidationMessage>Both throttling parameters should either be filled or empty</ValidationMessage>
<type>AllOrNoneConstraint</type>
<addFields>
<field1>perHostTrotteling</field1>
</addFields>
</check001>
</Constraints>
</OverallBandwidthTrotteling> </OverallBandwidthTrotteling>
<perHostTrotteling type="IntegerField"> <perHostTrotteling type="IntegerField">
<default>256</default> <default>256</default>
<MinimumValue>1</MinimumValue> <MinimumValue>1</MinimumValue>
<ValidationMessage>Specify the per host bandwidth for downloads in kilobits per second.</ValidationMessage> <ValidationMessage>Specify the per host bandwidth for downloads in kilobits per second.</ValidationMessage>
<Required>N</Required> <Required>N</Required>
<Constraints>
<check001>
<reference>OverallBandwidthTrotteling.check001</reference>
</check001>
</Constraints>
</perHostTrotteling> </perHostTrotteling>
</traffic> </traffic>
</general> </general>
...@@ -289,6 +304,12 @@ ...@@ -289,6 +304,12 @@
<Required>Y</Required> <Required>Y</Required>
<Mask>/^[a-zA-Z0-9]{1,245}\.?[a-zA-z0-9]{1,10}$/</Mask> <Mask>/^[a-zA-Z0-9]{1,245}\.?[a-zA-z0-9]{1,10}$/</Mask>
<ValidationMessage>The filename may only contain letters, digits and one dot (not required).</ValidationMessage> <ValidationMessage>The filename may only contain letters, digits and one dot (not required).</ValidationMessage>
<Constraints>
<check001>
<ValidationMessage>Filename should be unique</ValidationMessage>
<type>UniqueConstraint</type>
</check001>
</Constraints>
</filename> </filename>
<url type="UrlField"> <url type="UrlField">
<Required>Y</Required> <Required>Y</Required>
......
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