BpConfig/UploadForm: unify common code, enforce...

...prefix(es) according configured restrictions

refs  #106
This commit is contained in:
Thomas Gelf 2017-02-08 16:08:29 +01:00
parent 52ee6d759f
commit 7452a39f82
3 changed files with 74 additions and 66 deletions

View file

@ -2,28 +2,12 @@
namespace Icinga\Module\Businessprocess\Forms;
use Icinga\Application\Config;
use Icinga\Authentication\Auth;
use Icinga\Module\Businessprocess\BpConfig;
use Icinga\Module\Businessprocess\Storage\Storage;
use Icinga\Module\Businessprocess\Web\Form\QuickForm;
use Icinga\Module\Businessprocess\Web\Form\BpConfigBaseForm;
class BpConfigForm extends QuickForm
class BpConfigForm extends BpConfigBaseForm
{
/** @var Storage */
protected $storage;
protected $backend;
/** @var BpConfig */
protected $config;
protected $node;
protected $objectList = array();
protected $processList = array();
protected $deleteButtonName;
public function setup()
@ -132,24 +116,6 @@ class BpConfigForm extends QuickForm
}
}
protected function listAvailableBackends()
{
$keys = array_keys(Config::module('monitoring', 'backends')->toArray());
return array_combine($keys, $keys);
}
public function setStorage($storage)
{
$this->storage = $storage;
return $this;
}
public function setProcessConfig($config)
{
$this->config = $config;
return $this;
}
protected function onRequest()
{
$name = $this->getValue('name');
@ -170,7 +136,11 @@ class BpConfigForm extends QuickForm
// New config
$config = new BpConfig();
$config->setName($name);
$config->getMetadata()->set('Owner', Auth::getInstance()->getUser()->getUsername());
if (! $this->prepareMetadata($config)) {
return;
}
$this->setSuccessUrl(
$this->getSuccessUrl()->setParams(
array('config' => $name, 'unlocked' => true)

View file

@ -3,23 +3,15 @@
namespace Icinga\Module\Businessprocess\Forms;
use Exception;
use Icinga\Application\Config;
use Icinga\Module\Businessprocess\BpConfig;
use Icinga\Module\Businessprocess\Storage\LegacyConfigParser;
use Icinga\Module\Businessprocess\Storage\LegacyStorage;
use Icinga\Module\Businessprocess\Web\Form\QuickForm;
use Icinga\Module\Businessprocess\Web\Form\BpConfigBaseForm;
use Icinga\Web\Notification;
class BpUploadForm extends QuickForm
class BpUploadForm extends BpConfigBaseForm
{
/** @var LegacyStorage */
protected $storage;
protected $backend;
/** @var BpConfig */
protected $config;
protected $node;
protected $objectList = array();
@ -151,24 +143,6 @@ class BpUploadForm extends QuickForm
);
}
protected function listAvailableBackends()
{
$keys = array_keys(Config::module('monitoring', 'backends')->toArray());
return array_combine($keys, $keys);
}
public function setStorage(LegacyStorage $storage)
{
$this->storage = $storage;
return $this;
}
public function setProcessConfig(BpConfig $config)
{
$this->config = $config;
return $this;
}
protected function getTempDir()
{
return sys_get_temp_dir();
@ -211,7 +185,11 @@ class BpUploadForm extends QuickForm
$name
));
return false;
return;
}
if (! $this->prepareMetadata($config)) {
return;
}
$this->storage->storeProcess($config);

View file

@ -0,0 +1,60 @@
<?php
namespace Icinga\Module\Businessprocess\Web\Form;
use Icinga\Application\Config;
use Icinga\Authentication\Auth;
use Icinga\Module\Businessprocess\Storage\LegacyStorage;
use Icinga\Module\Businessprocess\BpConfig;
abstract class BpConfigBaseForm extends QuickForm
{
/** @var LegacyStorage */
protected $storage;
/** @var BpConfig */
protected $config;
protected function listAvailableBackends()
{
$keys = array_keys(Config::module('monitoring', 'backends')->toArray());
return array_combine($keys, $keys);
}
public function setStorage(LegacyStorage $storage)
{
$this->storage = $storage;
return $this;
}
public function setProcessConfig(BpConfig $config)
{
$this->config = $config;
return $this;
}
protected function prepareMetadata(BpConfig $config)
{
$meta = $config->getMetadata();
$auth = Auth::getInstance();
$meta->set('Owner', $auth->getUser()->getUsername());
$prefixes = $auth->getRestrictions('businessprocess/prefix');
if (! empty($prefixes) && ! $meta->nameIsPrefixedWithOneOf($prefixes)) {
if (count($prefixes) === 1) {
$this->getElement('name')->addError(sprintf(
$this->translate('Please prefix the name with "%s"'),
current($prefixes)
));
} else {
$this->getElement('name')->addError(sprintf(
$this->translate('Please prefix the name with one of "%s"'),
implode('", "', $prefixes)
));
}
return false;
}
return true;
}
}