Skip to content

Commit

Permalink
security/acme-client: sync with master
Browse files Browse the repository at this point in the history
  • Loading branch information
fichtner committed Mar 19, 2019
1 parent 1d4a0ce commit 3796da5
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 628 deletions.
2 changes: 1 addition & 1 deletion security/acme-client/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PLUGIN_NAME= acme-client
PLUGIN_VERSION= 1.20
PLUGIN_VERSION= 1.23
PLUGIN_COMMENT= Let's Encrypt client
PLUGIN_MAINTAINER= [email protected]
PLUGIN_DEPENDS= acme.sh bind912
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright (C) 2017 Frank Wall
* Copyright (C) 2017-2019 Frank Wall
* Copyright (C) 2015 Deciso B.V.
*
* All rights reserved.
Expand Down Expand Up @@ -29,181 +29,48 @@
*/
namespace OPNsense\AcmeClient\Api;

use \OPNsense\Base\ApiControllerBase;
use \OPNsense\AcmeClient\AcmeClient;
use \OPNsense\Core\Config;
use \OPNsense\Base\ApiMutableModelControllerBase;
use \OPNsense\Base\UIModelGrid;
use \OPNsense\Core\Config;
use \OPNsense\AcmeClient\AcmeClient;

/**
* Class AccountsController
* @package OPNsense\AcmeClient
*/
class AccountsController extends ApiControllerBase
class AccountsController extends ApiMutableModelControllerBase
{
/**
* 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 it easier to reference specific uuids
* without having to use them in the frontend descriptions.
* @param $mdl model reference
* @param $node reference node, to use as relative offset
* @param $reference reference for validation output, used to rename the validation output keys
* @return array result / validation output
*/
private function save($mdl, $node = null, $reference = null)
{
$result = array("result"=>"failed","validations" => array());
// perform validation
$valMsgs = $mdl->performValidation();
foreach ($valMsgs as $field => $msg) {
// replace absolute path to attribute for relative one at uuid.
if ($node != null) {
$fieldnm = str_replace($node->__reference, $reference, $msg->getField());
$result["validations"][$fieldnm] = $msg->getMessage();
} else {
$result["validations"][$msg->getField()] = $msg->getMessage();
}
}

// serialize model to config and save when there are no validation errors
if (count($result['validations']) == 0) {
// save config if validated correctly
$mdl->serializeToConfig();

Config::getInstance()->save();
$result = array("result" => "saved");
}
protected static $internalModelName = 'acmeclient';
protected static $internalModelClass = '\OPNsense\AcmeClient\AcmeClient';

return $result;
}

/**
* retrieve account settings or return defaults
* @param $uuid item unique id
* @return array
*/
public function getAction($uuid = null)
{
$mdlAcme = new AcmeClient();
if ($uuid != null) {
$node = $mdlAcme->getNodeByReference('accounts.account.'.$uuid);
if ($node != null) {
// return node
return array("account" => $node->getNodes());
}
} else {
// generate new node, but don't save to disc
$node = $mdlAcme->accounts->account->add();
return array("account" => $node->getNodes());
}
return array();
$this->sessionClose();
return $this->getBase('account', 'accounts.account', $uuid);
}

/**
* update account with given properties
* @param $uuid item unique id
* @return array
*/
public function setAction($uuid)
public function addAction()
{
if ($this->request->isPost() && $this->request->hasPost("account")) {
$mdlAcme = new AcmeClient();
if ($uuid != null) {
$node = $mdlAcme->getNodeByReference('accounts.account.'.$uuid);
if ($node != null) {
$node->setNodes($this->request->getPost("account"));
return $this->save($mdlAcme, $node, "account");
}
}
}
return array("result"=>"failed");
return $this->addBase('account', 'accounts.account');
}

/**
* add new account and set with attributes from post
* @return array
*/
public function addAction()
public function updateAction($uuid)
{
$result = array("result"=>"failed");
if ($this->request->isPost() && $this->request->hasPost("account")) {
$mdlAcme = new AcmeClient();
$node = $mdlAcme->accounts->account->Add();
$node->setNodes($this->request->getPost("account"));
return $this->save($mdlAcme, $node, "account");
}
return $result;
return $this->setBase('account', 'accounts.account', $uuid);
}

/**
* delete account by uuid
* @param $uuid item unique id
* @return array status
*/
public function delAction($uuid)
{
$result = array("result"=>"failed");
if ($this->request->isPost()) {
$mdlAcme = new AcmeClient();
if ($uuid != null) {
if ($mdlAcme->accounts->account->del($uuid)) {
// if item is removed, serialize to config and save
$mdlAcme->serializeToConfig();
Config::getInstance()->save();
$result['result'] = 'deleted';
} else {
$result['result'] = 'not found';
}
}
}
return $result;
return $this->delBase('accounts.account', $uuid);
}

/**
* toggle account by uuid (enable/disable)
* @param $uuid item unique id
* @param $enabled desired state enabled(1)/disabled(0), leave empty for toggle
* @return array status
*/
public function toggleAction($uuid, $enabled = null)
{

$result = array("result" => "failed");
if ($this->request->isPost()) {
$mdlAcme = new AcmeClient();
if ($uuid != null) {
$node = $mdlAcme->getNodeByReference('accounts.account.' . $uuid);
if ($node != null) {
if ($enabled == "0" || $enabled == "1") {
$node->enabled = (string)$enabled;
} elseif ((string)$node->enabled == "1") {
$node->enabled = "0";
} else {
$node->enabled = "1";
}
$result['result'] = $node->enabled;
// if item has toggled, serialize to config and save
$mdlAcme->serializeToConfig();
Config::getInstance()->save();
}
}
}
return $result;
return $this->toggleBase('accounts.account', $uuid);
}

/**
* search accounts
* @return array
*/
public function searchAction()
{
$this->sessionClose();
$mdlAcme = new AcmeClient();
$grid = new UIModelGrid($mdlAcme->accounts->account);
return $grid->fetchBindRequest(
$this->request,
array("enabled", "name", "email"),
"name"
);
return $this->searchBase('accounts.account', array('enabled', 'name', 'email'), 'name');
}
}
Loading

0 comments on commit 3796da5

Please sign in to comment.