diff --git a/mail/postfix/src/opnsense/mvc/app/controllers/OPNsense/Postfix/Api/AntispamController.php b/mail/postfix/src/opnsense/mvc/app/controllers/OPNsense/Postfix/Api/AntispamController.php index c9896d712..31702b9ee 100644 --- a/mail/postfix/src/opnsense/mvc/app/controllers/OPNsense/Postfix/Api/AntispamController.php +++ b/mail/postfix/src/opnsense/mvc/app/controllers/OPNsense/Postfix/Api/AntispamController.php @@ -2,6 +2,7 @@ /** * Copyright (C) 2015 - 2017 Deciso B.V. * Copyright (C) 2017 Michael Muenz + * Copyright (C) 2018 Fabian Franz * * All rights reserved. * @@ -30,47 +31,10 @@ namespace OPNsense\Postfix\Api; -use \OPNsense\Base\ApiControllerBase; -use \OPNsense\Postfix\Antispam; -use \OPNsense\Core\Config; +use \OPNsense\Base\ApiMutableModelControllerBase; -class AntispamController extends ApiControllerBase +class AntispamController extends ApiMutableModelControllerBase { - public function getAction() - { - // define list of configurable settings - $result = array(); - if ($this->request->isGet()) { - $mdlAntispam = new Antispam(); - $result['antispam'] = $mdlAntispam->getNodes(); - } - return $result; - } - - public function setAction() - { - $result = array("result"=>"failed"); - if ($this->request->isPost()) { - // load model and update with provided data - $mdlAntispam = new Antispam(); - $mdlAntispam->setNodes($this->request->getPost("antispam")); - - // perform validation - $valMsgs = $mdlAntispam->performValidation(); - foreach ($valMsgs as $field => $msg) { - if (!array_key_exists("validations", $result)) { - $result["validations"] = array(); - } - $result["validations"]["antispam.".$msg->getField()] = $msg->getMessage(); - } - - // serialize model to config and save - if ($valMsgs->count() == 0) { - $mdlAntispam->serializeToConfig(); - Config::getInstance()->save(); - $result["result"] = "saved"; - } - } - return $result; - } + static protected $internalModelName = 'antispam'; + static protected $internalModelClass = '\OPNsense\Postfix\Antispam'; } diff --git a/mail/postfix/src/opnsense/mvc/app/controllers/OPNsense/Postfix/Api/DomainController.php b/mail/postfix/src/opnsense/mvc/app/controllers/OPNsense/Postfix/Api/DomainController.php index f449b9245..3d9809973 100644 --- a/mail/postfix/src/opnsense/mvc/app/controllers/OPNsense/Postfix/Api/DomainController.php +++ b/mail/postfix/src/opnsense/mvc/app/controllers/OPNsense/Postfix/Api/DomainController.php @@ -2,6 +2,7 @@ /** * Copyright (C) 2015 - 2017 Deciso B.V. * Copyright (C) 2017 Michael Muenz + * Copyright (C) 2018 Fabian Franz * * All rights reserved. * @@ -30,176 +31,40 @@ namespace OPNsense\Postfix\Api; -use \OPNsense\Postfix\Domain; -use \OPNsense\Core\Config; use \OPNsense\Base\ApiMutableModelControllerBase; -use \OPNsense\Base\UIModelGrid; class DomainController extends ApiMutableModelControllerBase { - static protected $internalModelName = 'Domain'; + static protected $internalModelName = 'domain'; static protected $internalModelClass = '\OPNsense\Postfix\Domain'; - public function getAction() - { - // define list of configurable settings - $result = array(); - if ($this->request->isGet()) { - $mdlDomain = new Domain(); - $result['domain'] = $mdlDomain->getNodes(); - } - return $result; - } - - public function setAction() - { - $result = array("result"=>"failed"); - if ($this->request->isPost()) { - // load model and update with provided data - $mdlDomain = new Domain(); - $mdlDomain->setNodes($this->request->getPost("domain")); - // perform validation - $valMsgs = $mdlDomain->performValidation(); - foreach ($valMsgs as $field => $msg) { - if (!array_key_exists("validations", $result)) { - $result["validations"] = array(); - } - $result["validations"]["domain.".$msg->getField()] = $msg->getMessage(); - } - // serialize model to config and save - if ($valMsgs->count() == 0) { - $mdlDomain->serializeToConfig(); - Config::getInstance()->save(); - $result["result"] = "saved"; - } - } - return $result; - } - public function searchDomainAction() { - $this->sessionClose(); - $mdlDomain = $this->getModel(); - $grid = new UIModelGrid($mdlDomain->domains->domain); - return $grid->fetchBindRequest( - $this->request, - array("enabled", "domainname", "destination" ) - ); + return $this->searchBase('domains.domain', array("enabled", "domainname", "destination")); } public function getDomainAction($uuid = null) { - $mdlDomain = $this->getModel(); - if ($uuid != null) { - $node = $mdlDomain->getNodeByReference('domains.domain.' . $uuid); - if ($node != null) { - // return node - return array("domain" => $node->getNodes()); - } - } else { - $node = $mdlDomain->domains->domain->add(); - return array("domain" => $node->getNodes()); - } - return array(); + return $this->getBase('domain', 'domains.domain', $uuid); } public function addDomainAction() { - $result = array("result" => "failed"); - if ($this->request->isPost() && $this->request->hasPost("domain")) { - $result = array("result" => "failed", "validations" => array()); - $mdlDomain = $this->getModel(); - $node = $mdlDomain->domains->domain->Add(); - $node->setNodes($this->request->getPost("domain")); - $valMsgs = $mdlDomain->performValidation(); - foreach ($valMsgs as $field => $msg) { - $fieldnm = str_replace($node->__reference, "domain", $msg->getField()); - $result["validations"][$fieldnm] = $msg->getMessage(); - } - if (count($result['validations']) == 0) { - unset($result['validations']); - // save config if validated correctly - $mdlDomain->serializeToConfig(); - Config::getInstance()->save(); - unset($result['validations']); - $result["result"] = "saved"; - } - } - return $result; + return $this->addBase('domain', 'domains.domain'); } public function delDomainAction($uuid) { - $result = array("result" => "failed"); - if ($this->request->isPost()) { - $mdlDomain = $this->getModel(); - if ($uuid != null) { - if ($mdlDomain->domains->domain->del($uuid)) { - $mdlDomain->serializeToConfig(); - Config::getInstance()->save(); - $result['result'] = 'deleted'; - } else { - $result['result'] = 'not found'; - } - } - } - return $result; + return $this->delBase('domains.domain', $uuid); } public function setDomainAction($uuid) { - if ($this->request->isPost() && $this->request->hasPost("domain")) { - $mdlSetting = $this->getModel(); - if ($uuid != null) { - $node = $mdlSetting->getNodeByReference('domains.domain.' . $uuid); - if ($node != null) { - $result = array("result" => "failed", "validations" => array()); - $domainInfo = $this->request->getPost("domain"); - $node->setNodes($domainInfo); - $valMsgs = $mdlSetting->performValidation(); - foreach ($valMsgs as $field => $msg) { - $fieldnm = str_replace($node->__reference, "domain", $msg->getField()); - $result["validations"][$fieldnm] = $msg->getMessage(); - } - if (count($result['validations']) == 0) { - // save config if validated correctly - $mdlSetting->serializeToConfig(); - Config::getInstance()->save(); - $result = array("result" => "saved"); - } - return $result; - } - } - } - return array("result" => "failed"); - } - - public function toggle_handler($uuid, $elements, $element) - { - $result = array("result" => "failed"); - if ($this->request->isPost()) { - $mdlSetting = $this->getModel(); - if ($uuid != null) { - $node = $mdlSetting->getNodeByReference($elements . '.'. $element .'.' . $uuid); - if ($node != null) { - if ($node->enabled->__toString() == "1") { - $result['result'] = "Disabled"; - $node->enabled = "0"; - } else { - $result['result'] = "Enabled"; - $node->enabled = "1"; - } - // if item has toggled, serialize to config and save - $mdlSetting->serializeToConfig(); - Config::getInstance()->save(); - } - } - } - return $result; + return $this->setBase('domain', 'domains.domain', $uuid); } public function toggleDomainAction($uuid) { - return $this->toggle_handler($uuid, 'domains', 'domain'); + return $this->toggleBase('domains.domain', $uuid); } } diff --git a/mail/postfix/src/opnsense/mvc/app/controllers/OPNsense/Postfix/Api/RecipientController.php b/mail/postfix/src/opnsense/mvc/app/controllers/OPNsense/Postfix/Api/RecipientController.php index a37ae7ba1..3035fcc91 100644 --- a/mail/postfix/src/opnsense/mvc/app/controllers/OPNsense/Postfix/Api/RecipientController.php +++ b/mail/postfix/src/opnsense/mvc/app/controllers/OPNsense/Postfix/Api/RecipientController.php @@ -2,6 +2,7 @@ /** * Copyright (C) 2015 - 2017 Deciso B.V. * Copyright (C) 2017 Michael Muenz + * Copyright (C) 2018 Fabian Franz * * All rights reserved. * @@ -30,176 +31,40 @@ namespace OPNsense\Postfix\Api; -use \OPNsense\Postfix\Recipient; -use \OPNsense\Core\Config; use \OPNsense\Base\ApiMutableModelControllerBase; -use \OPNsense\Base\UIModelGrid; class RecipientController extends ApiMutableModelControllerBase { - static protected $internalModelName = 'Recipient'; + static protected $internalModelName = 'recipient'; static protected $internalModelClass = '\OPNsense\Postfix\Recipient'; - public function getAction() - { - // define list of configurable settings - $result = array(); - if ($this->request->isGet()) { - $mdlRecipient = new Recipient(); - $result['recipient'] = $mdlRecipient->getNodes(); - } - return $result; - } - - public function setAction() - { - $result = array("result"=>"failed"); - if ($this->request->isPost()) { - // load model and update with provided data - $mdlRecipient = new Recipient(); - $mdlRecipient->setNodes($this->request->getPost("recipient")); - // perform validation - $valMsgs = $mdlRecipient->performValidation(); - foreach ($valMsgs as $field => $msg) { - if (!array_key_exists("validations", $result)) { - $result["validations"] = array(); - } - $result["validations"]["recipient.".$msg->getField()] = $msg->getMessage(); - } - // serialize model to config and save - if ($valMsgs->count() == 0) { - $mdlRecipient->serializeToConfig(); - Config::getInstance()->save(); - $result["result"] = "saved"; - } - } - return $result; - } - public function searchRecipientAction() { - $this->sessionClose(); - $mdlRecipient = $this->getModel(); - $grid = new UIModelGrid($mdlRecipient->recipients->recipient); - return $grid->fetchBindRequest( - $this->request, - array("enabled", "address", "action" ) - ); + return $this->searchBase('recipients.recipient', array("enabled", "address", "action")); } public function getRecipientAction($uuid = null) { - $mdlRecipient = $this->getModel(); - if ($uuid != null) { - $node = $mdlRecipient->getNodeByReference('recipients.recipient.' . $uuid); - if ($node != null) { - // return node - return array("recipient" => $node->getNodes()); - } - } else { - $node = $mdlRecipient->recipients->recipient->add(); - return array("recipient" => $node->getNodes()); - } - return array(); + return $this->getBase('recipient', 'recipients.recipient', $uuid); } public function addRecipientAction() { - $result = array("result" => "failed"); - if ($this->request->isPost() && $this->request->hasPost("recipient")) { - $result = array("result" => "failed", "validations" => array()); - $mdlRecipient = $this->getModel(); - $node = $mdlRecipient->recipients->recipient->Add(); - $node->setNodes($this->request->getPost("recipient")); - $valMsgs = $mdlRecipient->performValidation(); - foreach ($valMsgs as $field => $msg) { - $fieldnm = str_replace($node->__reference, "recipient", $msg->getField()); - $result["validations"][$fieldnm] = $msg->getMessage(); - } - if (count($result['validations']) == 0) { - unset($result['validations']); - // save config if validated correctly - $mdlRecipient->serializeToConfig(); - Config::getInstance()->save(); - unset($result['validations']); - $result["result"] = "saved"; - } - } - return $result; + return $this->addBase('recipient', 'recipients.recipient'); } public function delRecipientAction($uuid) { - $result = array("result" => "failed"); - if ($this->request->isPost()) { - $mdlRecipient = $this->getModel(); - if ($uuid != null) { - if ($mdlRecipient->recipients->recipient->del($uuid)) { - $mdlRecipient->serializeToConfig(); - Config::getInstance()->save(); - $result['result'] = 'deleted'; - } else { - $result['result'] = 'not found'; - } - } - } - return $result; + return $this->delBase('recipients.recipient', $uuid); } public function setRecipientAction($uuid) { - if ($this->request->isPost() && $this->request->hasPost("recipient")) { - $mdlSetting = $this->getModel(); - if ($uuid != null) { - $node = $mdlSetting->getNodeByReference('recipients.recipient.' . $uuid); - if ($node != null) { - $result = array("result" => "failed", "validations" => array()); - $recipientInfo = $this->request->getPost("recipient"); - $node->setNodes($recipientInfo); - $valMsgs = $mdlSetting->performValidation(); - foreach ($valMsgs as $field => $msg) { - $fieldnm = str_replace($node->__reference, "recipient", $msg->getField()); - $result["validations"][$fieldnm] = $msg->getMessage(); - } - if (count($result['validations']) == 0) { - // save config if validated correctly - $mdlSetting->serializeToConfig(); - Config::getInstance()->save(); - $result = array("result" => "saved"); - } - return $result; - } - } - } - return array("result" => "failed"); - } - - public function toggle_handler($uuid, $elements, $element) - { - $result = array("result" => "failed"); - if ($this->request->isPost()) { - $mdlSetting = $this->getModel(); - if ($uuid != null) { - $node = $mdlSetting->getNodeByReference($elements . '.'. $element .'.' . $uuid); - if ($node != null) { - if ($node->enabled->__toString() == "1") { - $result['result'] = "Disabled"; - $node->enabled = "0"; - } else { - $result['result'] = "Enabled"; - $node->enabled = "1"; - } - // if item has toggled, serialize to config and save - $mdlSetting->serializeToConfig(); - Config::getInstance()->save(); - } - } - } - return $result; + return $this->setBase('recipient', 'recipients.recipient', $uuid); } public function toggleRecipientAction($uuid) { - return $this->toggle_handler($uuid, 'recipients', 'recipient'); + return $this->toggleBase('recipients.recipient', $uuid); } } diff --git a/mail/postfix/src/opnsense/mvc/app/controllers/OPNsense/Postfix/Api/SenderController.php b/mail/postfix/src/opnsense/mvc/app/controllers/OPNsense/Postfix/Api/SenderController.php index 0dded2fed..ce151c548 100644 --- a/mail/postfix/src/opnsense/mvc/app/controllers/OPNsense/Postfix/Api/SenderController.php +++ b/mail/postfix/src/opnsense/mvc/app/controllers/OPNsense/Postfix/Api/SenderController.php @@ -2,6 +2,7 @@ /** * Copyright (C) 2015 - 2017 Deciso B.V. * Copyright (C) 2017 Michael Muenz + * Copyright (C) 2018 Fabian Franz * * All rights reserved. * @@ -30,176 +31,40 @@ namespace OPNsense\Postfix\Api; -use \OPNsense\Postfix\Sender; -use \OPNsense\Core\Config; use \OPNsense\Base\ApiMutableModelControllerBase; -use \OPNsense\Base\UIModelGrid; class SenderController extends ApiMutableModelControllerBase { - static protected $internalModelName = 'Sender'; + static protected $internalModelName = 'sender'; static protected $internalModelClass = '\OPNsense\Postfix\Sender'; - public function getAction() - { - // define list of configurable settings - $result = array(); - if ($this->request->isGet()) { - $mdlSender = new Sender(); - $result['sender'] = $mdlSender->getNodes(); - } - return $result; - } - - public function setAction() - { - $result = array("result"=>"failed"); - if ($this->request->isPost()) { - // load model and update with provided data - $mdlSender = new Sender(); - $mdlSender->setNodes($this->request->getPost("sender")); - // perform validation - $valMsgs = $mdlSender->performValidation(); - foreach ($valMsgs as $field => $msg) { - if (!array_key_exists("validations", $result)) { - $result["validations"] = array(); - } - $result["validations"]["sender.".$msg->getField()] = $msg->getMessage(); - } - // serialize model to config and save - if ($valMsgs->count() == 0) { - $mdlSender->serializeToConfig(); - Config::getInstance()->save(); - $result["result"] = "saved"; - } - } - return $result; - } - public function searchSenderAction() { - $this->sessionClose(); - $mdlSender = $this->getModel(); - $grid = new UIModelGrid($mdlSender->senders->sender); - return $grid->fetchBindRequest( - $this->request, - array("enabled", "address", "action" ) - ); + return $this->searchBase('senders.sender', array("enabled", "address", "action")); } public function getSenderAction($uuid = null) { - $mdlSender = $this->getModel(); - if ($uuid != null) { - $node = $mdlSender->getNodeByReference('senders.sender.' . $uuid); - if ($node != null) { - // return node - return array("sender" => $node->getNodes()); - } - } else { - $node = $mdlSender->senders->sender->add(); - return array("sender" => $node->getNodes()); - } - return array(); + return $this->getBase('sender', 'senders.sender', $uuid); } public function addSenderAction() { - $result = array("result" => "failed"); - if ($this->request->isPost() && $this->request->hasPost("sender")) { - $result = array("result" => "failed", "validations" => array()); - $mdlSender = $this->getModel(); - $node = $mdlSender->senders->sender->Add(); - $node->setNodes($this->request->getPost("sender")); - $valMsgs = $mdlSender->performValidation(); - foreach ($valMsgs as $field => $msg) { - $fieldnm = str_replace($node->__reference, "sender", $msg->getField()); - $result["validations"][$fieldnm] = $msg->getMessage(); - } - if (count($result['validations']) == 0) { - unset($result['validations']); - // save config if validated correctly - $mdlSender->serializeToConfig(); - Config::getInstance()->save(); - unset($result['validations']); - $result["result"] = "saved"; - } - } - return $result; + return $this->addBase('sender', 'senders.sender'); } public function delSenderAction($uuid) { - $result = array("result" => "failed"); - if ($this->request->isPost()) { - $mdlSender = $this->getModel(); - if ($uuid != null) { - if ($mdlSender->senders->sender->del($uuid)) { - $mdlSender->serializeToConfig(); - Config::getInstance()->save(); - $result['result'] = 'deleted'; - } else { - $result['result'] = 'not found'; - } - } - } - return $result; + return $this->delBase('senders.sender', $uuid); } public function setSenderAction($uuid) { - if ($this->request->isPost() && $this->request->hasPost("sender")) { - $mdlSetting = $this->getModel(); - if ($uuid != null) { - $node = $mdlSetting->getNodeByReference('senders.sender.' . $uuid); - if ($node != null) { - $result = array("result" => "failed", "validations" => array()); - $senderInfo = $this->request->getPost("sender"); - $node->setNodes($senderInfo); - $valMsgs = $mdlSetting->performValidation(); - foreach ($valMsgs as $field => $msg) { - $fieldnm = str_replace($node->__reference, "sender", $msg->getField()); - $result["validations"][$fieldnm] = $msg->getMessage(); - } - if (count($result['validations']) == 0) { - // save config if validated correctly - $mdlSetting->serializeToConfig(); - Config::getInstance()->save(); - $result = array("result" => "saved"); - } - return $result; - } - } - } - return array("result" => "failed"); - } - - public function toggle_handler($uuid, $elements, $element) - { - $result = array("result" => "failed"); - if ($this->request->isPost()) { - $mdlSetting = $this->getModel(); - if ($uuid != null) { - $node = $mdlSetting->getNodeByReference($elements . '.'. $element .'.' . $uuid); - if ($node != null) { - if ($node->enabled->__toString() == "1") { - $result['result'] = "Disabled"; - $node->enabled = "0"; - } else { - $result['result'] = "Enabled"; - $node->enabled = "1"; - } - // if item has toggled, serialize to config and save - $mdlSetting->serializeToConfig(); - Config::getInstance()->save(); - } - } - } - return $result; + return $this->setBase('sender', 'senders.sender', $uuid); } public function toggleSenderAction($uuid) { - return $this->toggle_handler($uuid, 'senders', 'sender'); + return $this->toggleBase('senders.sender', $uuid); } }