icingaweb2/application/forms/Config/Authentication/DbBackendForm.php

112 lines
3.2 KiB
PHP
Raw Normal View History

<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Form\Config\Authentication;
2014-08-11 10:43:54 +02:00
use Exception;
use Icinga\Data\ResourceFactory;
use Icinga\Exception\ConfigurationError;
2014-08-11 10:43:54 +02:00
use Icinga\Authentication\Backend\DbUserBackend;
/**
* 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
*
* @var array
*/
protected $resources;
/**
* Initialize this form
*
* Populates $this->resources.
*
* @throws ConfigurationError In case no database resources can be found
*/
public function init()
{
$this->setName('form_config_authentication_db');
$this->setSubmitLabel(t('Save Changes'));
$dbResources = array_keys(
ResourceFactory::getResourceConfigs('db')->toArray()
);
if (empty($dbResources)) {
throw new ConfigurationError(
t('There are no database resources')
);
}
// array_combine() is necessary in order to use the array as select input data
$this->resources = array_combine($dbResources, $dbResources);
}
2014-08-11 10:43:54 +02:00
/**
* @see Form::createElements()
*/
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
)
);
}
/**
* Validate the current configuration by creating a backend and requesting the user count
*
* @return bool Whether validation succeeded or not
*
2014-08-11 10:43:54 +02:00
* @see BaseBackendForm::isValidAuthenticationBackend()
*/
public function isValidAuthenticationBackend()
{
try {
$testConnection = ResourceFactory::createResource(ResourceFactory::getResourceConfig(
$this->getValue('resource')
));
$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'));
return false;
}
} catch (Exception $e) {
$this->addErrorMessage(sprintf(t('Using the specified backend failed: %s'), $e->getMessage()));
return false;
}
2014-08-11 10:43:54 +02:00
return true;
}
}