2013-08-16 16:24:12 +02:00
|
|
|
<?php
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
|
|
|
namespace Icinga\Form\Config\Authentication;
|
|
|
|
|
2014-08-11 10:43:54 +02:00
|
|
|
use Exception;
|
2014-04-16 11:50:58 +02:00
|
|
|
use Icinga\Data\ResourceFactory;
|
2014-07-29 12:22:43 +02:00
|
|
|
use Icinga\Exception\ConfigurationError;
|
2014-08-11 10:43:54 +02:00
|
|
|
use Icinga\Authentication\Backend\DbUserBackend;
|
2013-08-16 16:24:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Form class for adding/modifying database authentication backends
|
|
|
|
*/
|
|
|
|
class DbBackendForm extends BaseBackendForm
|
|
|
|
{
|
|
|
|
/**
|
2014-08-11 10:43:54 +02:00
|
|
|
* The available database resources prepared to be used as select input data
|
|
|
|
*
|
2014-07-29 12:22:43 +02:00
|
|
|
* @var array
|
2013-08-16 16:24:12 +02:00
|
|
|
*/
|
2014-07-29 12:22:43 +02:00
|
|
|
protected $resources;
|
2014-04-16 11:50:58 +02:00
|
|
|
|
2014-08-11 10:39:13 +02:00
|
|
|
/**
|
|
|
|
* Initialize this form
|
|
|
|
*
|
|
|
|
* Populates $this->resources.
|
|
|
|
*
|
|
|
|
* @throws ConfigurationError In case no database resources can be found
|
|
|
|
*/
|
|
|
|
public function init()
|
2013-08-16 16:24:12 +02:00
|
|
|
{
|
2014-08-22 12:15:02 +02:00
|
|
|
$this->setName('form_config_authentication_db');
|
2014-08-22 15:20:54 +02:00
|
|
|
$this->setSubmitLabel(t('Save Changes'));
|
2014-08-22 12:15:02 +02:00
|
|
|
|
2014-07-29 12:22:43 +02:00
|
|
|
$dbResources = array_keys(
|
|
|
|
ResourceFactory::getResourceConfigs('db')->toArray()
|
2013-08-16 16:24:12 +02:00
|
|
|
);
|
2014-08-11 10:39:13 +02:00
|
|
|
|
2014-07-29 12:22:43 +02:00
|
|
|
if (empty($dbResources)) {
|
|
|
|
throw new ConfigurationError(
|
|
|
|
t('There are no database resources')
|
|
|
|
);
|
|
|
|
}
|
2013-08-16 16:24:12 +02:00
|
|
|
|
2014-08-11 10:39:13 +02:00
|
|
|
// array_combine() is necessary in order to use the array as select input data
|
|
|
|
$this->resources = array_combine($dbResources, $dbResources);
|
2014-07-29 12:22:43 +02:00
|
|
|
}
|
2013-08-16 16:24:12 +02:00
|
|
|
|
2014-08-11 10:43:54 +02:00
|
|
|
/**
|
|
|
|
* @see Form::createElements()
|
|
|
|
*/
|
2014-07-29 12:22:43 +02:00
|
|
|
public function createElements(array $formData)
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
$this->createElement(
|
|
|
|
'text',
|
|
|
|
'name',
|
|
|
|
array(
|
|
|
|
'required' => true,
|
|
|
|
'label' => t('Backend Name'),
|
|
|
|
'helptext' => t('The name of this authentication provider'),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
$this->createElement(
|
|
|
|
'select',
|
|
|
|
'resource',
|
|
|
|
array(
|
|
|
|
'required' => true,
|
|
|
|
'label' => t('Database Connection'),
|
|
|
|
'helptext' => t('The database connection to use for authenticating with this provider'),
|
|
|
|
'multiOptions' => $this->resources
|
|
|
|
)
|
|
|
|
),
|
|
|
|
$this->createElement(
|
|
|
|
'hidden',
|
|
|
|
'backend',
|
|
|
|
array(
|
|
|
|
'required' => true,
|
|
|
|
'value' => 'db'
|
|
|
|
)
|
2013-10-23 12:25:51 +02:00
|
|
|
)
|
|
|
|
);
|
2013-08-16 16:24:12 +02:00
|
|
|
}
|
|
|
|
|
2013-08-27 14:37:22 +02:00
|
|
|
/**
|
|
|
|
* Validate the current configuration by creating a backend and requesting the user count
|
|
|
|
*
|
2014-04-16 11:50:58 +02:00
|
|
|
* @return bool Whether validation succeeded or not
|
|
|
|
*
|
2014-08-11 10:43:54 +02:00
|
|
|
* @see BaseBackendForm::isValidAuthenticationBackend()
|
2013-08-27 14:37:22 +02:00
|
|
|
*/
|
|
|
|
public function isValidAuthenticationBackend()
|
2013-08-26 16:56:23 +02:00
|
|
|
{
|
2013-08-26 17:23:31 +02:00
|
|
|
try {
|
2014-04-28 16:45:37 +02:00
|
|
|
$testConnection = ResourceFactory::createResource(ResourceFactory::getResourceConfig(
|
2014-07-29 12:22:43 +02:00
|
|
|
$this->getValue('resource')
|
2014-04-28 16:45:37 +02:00
|
|
|
));
|
|
|
|
$dbUserBackend = new DbUserBackend($testConnection);
|
|
|
|
if ($dbUserBackend->count() < 1) {
|
2014-08-11 10:43:54 +02:00
|
|
|
$this->addErrorMessage(t('No users found under the specified database backend'));
|
2013-08-26 17:23:31 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-04-16 11:50:58 +02:00
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->addErrorMessage(sprintf(t('Using the specified backend failed: %s'), $e->getMessage()));
|
2013-08-26 17:23:31 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-11 10:43:54 +02:00
|
|
|
|
2013-08-26 16:56:23 +02:00
|
|
|
return true;
|
|
|
|
}
|
2013-08-16 16:24:12 +02:00
|
|
|
}
|