2014-09-02 15:39:21 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2014-09-02 15:39:21 +02:00
|
|
|
|
2014-11-14 10:57:14 +01:00
|
|
|
namespace Icinga\Forms\Config;
|
2014-09-02 15:39:21 +02:00
|
|
|
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use Icinga\Web\Notification;
|
2014-11-14 10:57:14 +01:00
|
|
|
use Icinga\Forms\ConfigForm;
|
|
|
|
use Icinga\Forms\Config\Resource\DbResourceForm;
|
|
|
|
use Icinga\Forms\Config\Resource\FileResourceForm;
|
|
|
|
use Icinga\Forms\Config\Resource\LdapResourceForm;
|
|
|
|
use Icinga\Forms\Config\Resource\LivestatusResourceForm;
|
2015-05-28 10:50:13 +02:00
|
|
|
use Icinga\Forms\Config\Resource\SshResourceForm;
|
2014-09-02 15:39:21 +02:00
|
|
|
use Icinga\Application\Platform;
|
|
|
|
use Icinga\Exception\ConfigurationError;
|
|
|
|
|
|
|
|
class ResourceConfigForm extends ConfigForm
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Initialize this form
|
|
|
|
*/
|
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
$this->setName('form_config_resource');
|
2015-01-19 11:26:23 +01:00
|
|
|
$this->setSubmitLabel($this->translate('Save Changes'));
|
2014-09-02 15:39:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a form object for the given resource type
|
|
|
|
*
|
|
|
|
* @param string $type The resource type for which to return a form
|
|
|
|
*
|
|
|
|
* @return Form
|
|
|
|
*/
|
|
|
|
public function getResourceForm($type)
|
|
|
|
{
|
|
|
|
if ($type === 'db') {
|
|
|
|
return new DbResourceForm();
|
|
|
|
} elseif ($type === 'ldap') {
|
|
|
|
return new LdapResourceForm();
|
|
|
|
} elseif ($type === 'livestatus') {
|
|
|
|
return new LivestatusResourceForm();
|
|
|
|
} elseif ($type === 'file') {
|
|
|
|
return new FileResourceForm();
|
2015-05-28 10:50:13 +02:00
|
|
|
} elseif ($type === 'ssh') {
|
|
|
|
return new SshResourceForm();
|
2014-09-02 15:39:21 +02:00
|
|
|
} else {
|
2015-01-19 11:26:23 +01:00
|
|
|
throw new InvalidArgumentException(sprintf($this->translate('Invalid resource type "%s" provided'), $type));
|
2014-09-02 15:39:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a particular resource
|
|
|
|
*
|
|
|
|
* The backend to add is identified by the array-key `name'.
|
|
|
|
*
|
|
|
|
* @param array $values The values to extend the configuration with
|
|
|
|
*
|
2015-04-07 14:23:26 +02:00
|
|
|
* @return $this
|
2014-09-02 15:39:21 +02:00
|
|
|
*
|
2015-05-28 10:50:13 +02:00
|
|
|
* @throws InvalidArgumentException In case the resource does already exist
|
2014-09-02 15:39:21 +02:00
|
|
|
*/
|
|
|
|
public function add(array $values)
|
|
|
|
{
|
|
|
|
$name = isset($values['name']) ? $values['name'] : '';
|
|
|
|
if (! $name) {
|
2015-01-19 11:26:23 +01:00
|
|
|
throw new InvalidArgumentException($this->translate('Resource name missing'));
|
2014-11-18 13:11:52 +01:00
|
|
|
} elseif ($this->config->hasSection($name)) {
|
2015-01-19 11:26:23 +01:00
|
|
|
throw new InvalidArgumentException($this->translate('Resource already exists'));
|
2014-09-02 15:39:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unset($values['name']);
|
2014-11-18 13:11:52 +01:00
|
|
|
$this->config->setSection($name, $values);
|
2014-09-02 15:39:21 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Edit a particular resource
|
|
|
|
*
|
|
|
|
* @param string $name The name of the resource to edit
|
|
|
|
* @param array $values The values to edit the configuration with
|
|
|
|
*
|
|
|
|
* @return array The edited configuration
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException In case the resource does not exist
|
|
|
|
*/
|
|
|
|
public function edit($name, array $values)
|
|
|
|
{
|
|
|
|
if (! $name) {
|
2015-01-19 11:26:23 +01:00
|
|
|
throw new InvalidArgumentException($this->translate('Old resource name missing'));
|
2014-09-02 15:39:21 +02:00
|
|
|
} elseif (! ($newName = isset($values['name']) ? $values['name'] : '')) {
|
2015-01-19 11:26:23 +01:00
|
|
|
throw new InvalidArgumentException($this->translate('New resource name missing'));
|
2014-11-18 13:11:52 +01:00
|
|
|
} elseif (! $this->config->hasSection($name)) {
|
2015-01-19 11:26:23 +01:00
|
|
|
throw new InvalidArgumentException($this->translate('Unknown resource provided'));
|
2014-09-02 15:39:21 +02:00
|
|
|
}
|
|
|
|
|
2014-11-18 13:11:52 +01:00
|
|
|
$resourceConfig = $this->config->getSection($name);
|
|
|
|
$this->config->removeSection($name);
|
2014-09-02 15:39:21 +02:00
|
|
|
unset($values['name']);
|
2014-11-18 13:11:52 +01:00
|
|
|
$this->config->setSection($newName, $resourceConfig->merge($values));
|
|
|
|
return $resourceConfig;
|
2014-09-02 15:39:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a particular resource
|
|
|
|
*
|
|
|
|
* @param string $name The name of the resource to remove
|
|
|
|
*
|
|
|
|
* @return array The removed resource configuration
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException In case the resource does not exist
|
|
|
|
*/
|
|
|
|
public function remove($name)
|
|
|
|
{
|
|
|
|
if (! $name) {
|
2015-01-19 11:26:23 +01:00
|
|
|
throw new InvalidArgumentException($this->translate('Resource name missing'));
|
2014-11-18 13:11:52 +01:00
|
|
|
} elseif (! $this->config->hasSection($name)) {
|
2015-01-19 11:26:23 +01:00
|
|
|
throw new InvalidArgumentException($this->translate('Unknown resource provided'));
|
2014-09-02 15:39:21 +02:00
|
|
|
}
|
|
|
|
|
2014-11-18 13:11:52 +01:00
|
|
|
$resourceConfig = $this->config->getSection($name);
|
2015-05-28 10:50:13 +02:00
|
|
|
$resourceForm = $this->getResourceForm($resourceConfig->type);
|
|
|
|
if (method_exists($resourceForm, 'beforeRemove')) {
|
|
|
|
$resourceForm::beforeRemove($resourceConfig);
|
|
|
|
}
|
|
|
|
|
2014-11-18 13:11:52 +01:00
|
|
|
$this->config->removeSection($name);
|
2014-09-02 15:39:21 +02:00
|
|
|
return $resourceConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add or edit a resource and save the configuration
|
|
|
|
*
|
|
|
|
* Performs a connectivity validation using the submitted values. A checkbox is
|
|
|
|
* added to the form to skip the check if it fails and redirection is aborted.
|
|
|
|
*
|
|
|
|
* @see Form::onSuccess()
|
|
|
|
*/
|
2014-11-14 14:59:12 +01:00
|
|
|
public function onSuccess()
|
2014-09-02 15:39:21 +02:00
|
|
|
{
|
2015-05-28 10:50:13 +02:00
|
|
|
$resourceForm = $this->getResourceForm($this->getElement('type')->getValue());
|
|
|
|
|
2014-09-02 15:39:21 +02:00
|
|
|
if (($el = $this->getElement('force_creation')) === null || false === $el->isChecked()) {
|
2014-09-29 11:02:45 +02:00
|
|
|
if (method_exists($resourceForm, 'isValidResource') && false === $resourceForm::isValidResource($this)) {
|
2014-09-02 15:39:21 +02:00
|
|
|
$this->addElement($this->getForceCreationCheckbox());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-14 14:59:12 +01:00
|
|
|
$resource = $this->request->getQuery('resource');
|
2014-09-02 15:39:21 +02:00
|
|
|
try {
|
|
|
|
if ($resource === null) { // create new resource
|
2015-05-28 10:50:13 +02:00
|
|
|
if (method_exists($resourceForm, 'beforeAdd')) {
|
|
|
|
if (! $resourceForm::beforeAdd($this)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-09-02 15:39:21 +02:00
|
|
|
$this->add($this->getValues());
|
2015-01-19 11:26:23 +01:00
|
|
|
$message = $this->translate('Resource "%s" has been successfully created');
|
2014-09-02 15:39:21 +02:00
|
|
|
} else { // edit existing resource
|
|
|
|
$this->edit($resource, $this->getValues());
|
2015-01-19 11:26:23 +01:00
|
|
|
$message = $this->translate('Resource "%s" has been successfully changed');
|
2014-09-02 15:39:21 +02:00
|
|
|
}
|
|
|
|
} catch (InvalidArgumentException $e) {
|
|
|
|
Notification::error($e->getMessage());
|
2015-05-27 10:59:35 +02:00
|
|
|
return false;
|
2014-09-02 15:39:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->save()) {
|
|
|
|
Notification::success(sprintf($message, $this->getElement('name')->getValue()));
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populate the form in case a resource is being edited
|
|
|
|
*
|
|
|
|
* @see Form::onRequest()
|
|
|
|
*
|
|
|
|
* @throws ConfigurationError In case the backend name is missing in the request or is invalid
|
|
|
|
*/
|
2014-11-14 14:59:12 +01:00
|
|
|
public function onRequest()
|
2014-09-02 15:39:21 +02:00
|
|
|
{
|
2014-11-14 14:59:12 +01:00
|
|
|
$resource = $this->request->getQuery('resource');
|
2014-09-02 15:39:21 +02:00
|
|
|
if ($resource !== null) {
|
|
|
|
if ($resource === '') {
|
2015-01-19 11:26:23 +01:00
|
|
|
throw new ConfigurationError($this->translate('Resource name missing'));
|
2014-11-18 13:11:52 +01:00
|
|
|
} elseif (! $this->config->hasSection($resource)) {
|
2015-01-19 11:26:23 +01:00
|
|
|
throw new ConfigurationError($this->translate('Unknown resource provided'));
|
2014-09-02 15:39:21 +02:00
|
|
|
}
|
|
|
|
|
2014-11-18 13:11:52 +01:00
|
|
|
$configValues = $this->config->getSection($resource)->toArray();
|
2014-09-02 15:39:21 +02:00
|
|
|
$configValues['name'] = $resource;
|
|
|
|
$this->populate($configValues);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a checkbox to be displayed at the beginning of the form
|
|
|
|
* which allows the user to skip the connection validation
|
|
|
|
*
|
|
|
|
* @return Zend_Form_Element
|
|
|
|
*/
|
|
|
|
protected function getForceCreationCheckbox()
|
|
|
|
{
|
|
|
|
return $this->createElement(
|
|
|
|
'checkbox',
|
|
|
|
'force_creation',
|
|
|
|
array(
|
2014-09-02 17:03:32 +02:00
|
|
|
'order' => 0,
|
|
|
|
'ignore' => true,
|
2015-01-19 11:26:23 +01:00
|
|
|
'label' => $this->translate('Force Changes'),
|
|
|
|
'description' => $this->translate('Check this box to enforce changes without connectivity validation')
|
2014-09-02 15:39:21 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Form::createElemeents()
|
|
|
|
*/
|
|
|
|
public function createElements(array $formData)
|
|
|
|
{
|
|
|
|
$resourceType = isset($formData['type']) ? $formData['type'] : 'db';
|
|
|
|
|
|
|
|
$resourceTypes = array(
|
2015-01-19 11:26:23 +01:00
|
|
|
'file' => $this->translate('File'),
|
2014-09-02 15:39:21 +02:00
|
|
|
'livestatus' => 'Livestatus',
|
2015-05-28 10:50:13 +02:00
|
|
|
'ssh' => $this->translate('SSH Identity'),
|
2014-09-02 15:39:21 +02:00
|
|
|
);
|
|
|
|
if ($resourceType === 'ldap' || Platform::extensionLoaded('ldap')) {
|
|
|
|
$resourceTypes['ldap'] = 'LDAP';
|
|
|
|
}
|
2014-12-01 15:38:10 +01:00
|
|
|
if ($resourceType === 'db' || Platform::hasMysqlSupport() || Platform::hasPostgresqlSupport()) {
|
2015-01-19 11:26:23 +01:00
|
|
|
$resourceTypes['db'] = $this->translate('SQL Database');
|
2014-09-02 15:39:21 +02:00
|
|
|
}
|
|
|
|
|
2014-09-03 12:21:31 +02:00
|
|
|
$this->addElement(
|
|
|
|
'select',
|
|
|
|
'type',
|
|
|
|
array(
|
|
|
|
'required' => true,
|
|
|
|
'autosubmit' => true,
|
2015-01-19 11:26:23 +01:00
|
|
|
'label' => $this->translate('Resource Type'),
|
|
|
|
'description' => $this->translate('The type of resource'),
|
2014-09-03 12:21:31 +02:00
|
|
|
'multiOptions' => $resourceTypes,
|
|
|
|
'value' => $resourceType
|
2014-09-02 15:39:21 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isset($formData['force_creation']) && $formData['force_creation']) {
|
|
|
|
// In case another error occured and the checkbox was displayed before
|
2014-09-03 12:21:31 +02:00
|
|
|
$this->addElement($this->getForceCreationCheckbox());
|
2014-09-02 15:39:21 +02:00
|
|
|
}
|
|
|
|
|
2014-09-03 12:21:31 +02:00
|
|
|
$this->addElements($this->getResourceForm($resourceType)->createElements($formData)->getElements());
|
2014-09-02 15:39:21 +02:00
|
|
|
}
|
|
|
|
}
|