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;
|
2015-07-24 14:24:11 +02:00
|
|
|
use Icinga\Application\Platform;
|
|
|
|
use Icinga\Exception\ConfigurationError;
|
|
|
|
use Icinga\Data\ConfigObject;
|
|
|
|
use Icinga\Data\Inspectable;
|
|
|
|
use Icinga\Data\Inspection;
|
|
|
|
use Icinga\Data\ResourceFactory;
|
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;
|
2015-07-24 14:24:11 +02:00
|
|
|
use Icinga\Web\Form;
|
|
|
|
use Icinga\Web\Notification;
|
2014-09-02 15:39:21 +02:00
|
|
|
|
|
|
|
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'));
|
2015-07-24 14:31:02 +02:00
|
|
|
$this->setValidatePartial(true);
|
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()) {
|
2015-07-24 14:24:11 +02:00
|
|
|
$inspection = static::inspectResource($this);
|
|
|
|
if ($inspection !== null && $inspection->hasError()) {
|
|
|
|
$this->error($inspection->getError());
|
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;
|
|
|
|
}
|
|
|
|
}
|
2015-11-13 16:14:11 +01:00
|
|
|
$this->add(array_filter($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
|
2015-11-13 16:14:11 +01:00
|
|
|
$this->edit($resource, array_filter($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
|
|
|
);
|
2015-09-07 16:44:06 +02:00
|
|
|
if ($resourceType === 'ldap' || Platform::hasLdapSupport()) {
|
2014-09-02 15:39:21 +02:00
|
|
|
$resourceTypes['ldap'] = 'LDAP';
|
|
|
|
}
|
2015-09-07 16:44:06 +02:00
|
|
|
if ($resourceType === 'db' || Platform::hasDatabaseSupport()) {
|
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
|
|
|
}
|
2015-07-24 14:24:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a resource by using the given form's values and return its inspection results
|
|
|
|
*
|
|
|
|
* @param Form $form
|
|
|
|
*
|
|
|
|
* @return Inspection
|
|
|
|
*/
|
|
|
|
public static function inspectResource(Form $form)
|
|
|
|
{
|
|
|
|
if ($form->getValue('type') !== 'ssh') {
|
|
|
|
$resource = ResourceFactory::createResource(new ConfigObject($form->getValues()));
|
|
|
|
if ($resource instanceof Inspectable) {
|
|
|
|
return $resource->inspect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-24 14:31:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Run the configured resource's inspection checks and show the result, if necessary
|
|
|
|
*
|
|
|
|
* This will only run any validation if the user pushed the 'resource_validation' button.
|
|
|
|
*
|
|
|
|
* @param array $formData
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isValidPartial(array $formData)
|
|
|
|
{
|
|
|
|
if ($this->getElement('resource_validation')->isChecked() && parent::isValid($formData)) {
|
|
|
|
$inspection = static::inspectResource($this);
|
|
|
|
if ($inspection !== null) {
|
|
|
|
$join = function ($e) use (& $join) {
|
|
|
|
return is_string($e) ? $e : join("\n", array_map($join, $e));
|
|
|
|
};
|
|
|
|
$this->addElement(
|
|
|
|
'note',
|
|
|
|
'inspection_output',
|
|
|
|
array(
|
|
|
|
'order' => 0,
|
|
|
|
'value' => '<strong>' . $this->translate('Validation Log') . "</strong>\n\n"
|
|
|
|
. join("\n", array_map($join, $inspection->toArray())),
|
|
|
|
'decorators' => array(
|
|
|
|
'ViewHelper',
|
|
|
|
array('HtmlTag', array('tag' => 'pre', 'class' => 'log-output')),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($inspection->hasError()) {
|
|
|
|
$this->warning(sprintf(
|
|
|
|
$this->translate('Failed to successfully validate the configuration: %s'),
|
|
|
|
$inspection->getError()
|
|
|
|
));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->info($this->translate('The configuration has been successfully validated.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a submit button to this form and one to manually validate the configuration
|
|
|
|
*
|
|
|
|
* Calls parent::addSubmitButton() to add the submit button.
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function addSubmitButton()
|
|
|
|
{
|
|
|
|
parent::addSubmitButton()
|
|
|
|
->getElement('btn_submit')
|
|
|
|
->setDecorators(array('ViewHelper'));
|
|
|
|
|
|
|
|
$this->addElement(
|
|
|
|
'submit',
|
|
|
|
'resource_validation',
|
|
|
|
array(
|
2015-08-20 15:48:41 +02:00
|
|
|
'ignore' => true,
|
|
|
|
'label' => $this->translate('Validate Configuration'),
|
|
|
|
'data-progress-label' => $this->translate('Validation In Progress'),
|
|
|
|
'decorators' => array('ViewHelper')
|
2015-07-24 14:31:02 +02:00
|
|
|
)
|
|
|
|
);
|
2015-08-28 11:00:03 +02:00
|
|
|
|
|
|
|
$this->setAttrib('data-progress-element', 'resource-progress');
|
|
|
|
$this->addElement(
|
|
|
|
'note',
|
|
|
|
'resource-progress',
|
|
|
|
array(
|
|
|
|
'decorators' => array(
|
|
|
|
'ViewHelper',
|
|
|
|
array('Spinner', array('id' => 'resource-progress'))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-07-24 14:31:02 +02:00
|
|
|
$this->addDisplayGroup(
|
2015-08-28 11:00:03 +02:00
|
|
|
array('btn_submit', 'resource_validation', 'resource-progress'),
|
2015-07-24 14:31:02 +02:00
|
|
|
'submit_validation',
|
|
|
|
array(
|
|
|
|
'decorators' => array(
|
|
|
|
'FormElements',
|
2015-09-30 12:20:34 +02:00
|
|
|
array('HtmlTag', array('tag' => 'div', 'class' => 'control-group form-controls'))
|
2015-07-24 14:31:02 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2014-09-02 15:39:21 +02:00
|
|
|
}
|