2014-09-02 15:39:21 +02:00
|
|
|
<?php
|
2015-02-03 15:49:34 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | http://www.gnu.org/licenses/gpl-2.0.txt */
|
2014-09-02 15:39:21 +02:00
|
|
|
|
2014-11-14 10:57:14 +01:00
|
|
|
namespace Icinga\Forms\Config\Resource;
|
2014-09-02 15:39:21 +02:00
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use Icinga\Web\Form;
|
2014-11-18 13:11:52 +01:00
|
|
|
use Icinga\Data\ConfigObject;
|
2014-09-02 15:39:21 +02:00
|
|
|
use Icinga\Data\ResourceFactory;
|
2014-09-29 14:46:27 +02:00
|
|
|
use Icinga\Application\Platform;
|
2014-09-02 15:39:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Form class for adding/modifying database resources
|
|
|
|
*/
|
|
|
|
class DbResourceForm extends Form
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Initialize this form
|
|
|
|
*/
|
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
$this->setName('form_config_resource_db');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Form::createElements()
|
|
|
|
*/
|
|
|
|
public function createElements(array $formData)
|
|
|
|
{
|
2014-09-29 14:46:27 +02:00
|
|
|
$dbChoices = array();
|
2014-12-01 15:38:10 +01:00
|
|
|
if (Platform::hasMysqlSupport()) {
|
2014-09-29 14:46:27 +02:00
|
|
|
$dbChoices['mysql'] = 'MySQL';
|
|
|
|
}
|
2014-12-01 15:38:10 +01:00
|
|
|
if (Platform::hasPostgresqlSupport()) {
|
2014-09-29 14:46:27 +02:00
|
|
|
$dbChoices['pgsql'] = 'PostgreSQL';
|
|
|
|
}
|
|
|
|
|
2014-09-29 11:20:39 +02:00
|
|
|
$this->addElement(
|
|
|
|
'text',
|
|
|
|
'name',
|
|
|
|
array(
|
|
|
|
'required' => true,
|
2015-01-19 11:26:23 +01:00
|
|
|
'label' => $this->translate('Resource Name'),
|
|
|
|
'description' => $this->translate('The unique name of this resource')
|
2014-09-29 11:20:39 +02:00
|
|
|
)
|
|
|
|
);
|
2014-09-03 12:21:31 +02:00
|
|
|
$this->addElement(
|
|
|
|
'select',
|
|
|
|
'db',
|
|
|
|
array(
|
|
|
|
'required' => true,
|
2015-01-19 11:26:23 +01:00
|
|
|
'label' => $this->translate('Database Type'),
|
|
|
|
'description' => $this->translate('The type of SQL database'),
|
2014-09-29 14:46:27 +02:00
|
|
|
'multiOptions' => $dbChoices
|
2014-09-03 12:21:31 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->addElement(
|
|
|
|
'text',
|
|
|
|
'host',
|
|
|
|
array (
|
|
|
|
'required' => true,
|
2015-01-19 11:26:23 +01:00
|
|
|
'label' => $this->translate('Host'),
|
|
|
|
'description' => $this->translate('The hostname of the database'),
|
2014-09-03 12:21:31 +02:00
|
|
|
'value' => 'localhost'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->addElement(
|
2014-11-14 10:15:11 +01:00
|
|
|
'number',
|
|
|
|
'port',
|
|
|
|
array(
|
|
|
|
'required' => true,
|
2015-01-19 11:26:23 +01:00
|
|
|
'label' => $this->translate('Port'),
|
|
|
|
'description' => $this->translate('The port to use'),
|
2014-11-14 10:15:11 +01:00
|
|
|
'value' => 3306
|
2014-09-02 15:39:21 +02:00
|
|
|
)
|
|
|
|
);
|
2014-09-03 12:21:31 +02:00
|
|
|
$this->addElement(
|
|
|
|
'text',
|
|
|
|
'dbname',
|
|
|
|
array(
|
|
|
|
'required' => true,
|
2015-01-19 11:26:23 +01:00
|
|
|
'label' => $this->translate('Database Name'),
|
|
|
|
'description' => $this->translate('The name of the database to use')
|
2014-09-03 12:21:31 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->addElement(
|
|
|
|
'text',
|
|
|
|
'username',
|
|
|
|
array (
|
|
|
|
'required' => true,
|
2015-01-19 11:26:23 +01:00
|
|
|
'label' => $this->translate('Username'),
|
|
|
|
'description' => $this->translate('The user name to use for authentication')
|
2014-09-03 12:21:31 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->addElement(
|
|
|
|
'password',
|
|
|
|
'password',
|
|
|
|
array(
|
|
|
|
'required' => true,
|
|
|
|
'renderPassword' => true,
|
2015-01-19 11:26:23 +01:00
|
|
|
'label' => $this->translate('Password'),
|
|
|
|
'description' => $this->translate('The password to use for authentication')
|
2014-09-03 12:21:31 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
2014-09-02 15:39:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate that the current configuration points to a valid resource
|
|
|
|
*
|
|
|
|
* @see Form::onSuccess()
|
|
|
|
*/
|
2014-11-14 14:59:12 +01:00
|
|
|
public function onSuccess()
|
2014-09-02 15:39:21 +02:00
|
|
|
{
|
2014-09-29 11:02:45 +02:00
|
|
|
if (false === static::isValidResource($this)) {
|
2014-09-02 15:39:21 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate the resource configuration by trying to connect with it
|
|
|
|
*
|
|
|
|
* @param Form $form The form to fetch the configuration values from
|
|
|
|
*
|
|
|
|
* @return bool Whether validation succeeded or not
|
|
|
|
*/
|
2014-09-29 11:02:45 +02:00
|
|
|
public static function isValidResource(Form $form)
|
2014-09-02 15:39:21 +02:00
|
|
|
{
|
|
|
|
try {
|
2014-11-18 13:11:52 +01:00
|
|
|
$resource = ResourceFactory::createResource(new ConfigObject($form->getValues()));
|
2014-09-02 15:39:21 +02:00
|
|
|
$resource->getConnection()->getConnection();
|
|
|
|
} catch (Exception $e) {
|
2015-01-19 11:26:23 +01:00
|
|
|
$form->addError(
|
2015-01-19 13:47:53 +01:00
|
|
|
$form->translate('Connectivity validation failed, connection to the given resource not possible.')
|
2015-01-19 11:26:23 +01:00
|
|
|
);
|
2014-09-02 15:39:21 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|