icingaweb2/application/forms/Config/Authentication/DbBackendForm.php
Johannes Meyer fb5685bac3 Merge branch 'master' into bugfix/rebuild-form-builder-5525
Conflicts:
	application/forms/Config/Authentication/BaseBackendForm.php
	application/forms/Config/Authentication/DbBackendForm.php
	application/forms/Config/Authentication/LdapBackendForm.php
	application/forms/Config/Authentication/ReorderForm.php
	application/forms/Config/LoggingForm.php
	application/forms/Config/ResourceForm.php
	application/forms/Preference/GeneralForm.php
	library/Icinga/Application/Config.php
	library/Icinga/Web/Form.php
	modules/monitoring/application/controllers/ConfigController.php
	modules/monitoring/application/forms/Config/Backend/CreateBackendForm.php
	modules/monitoring/application/forms/Config/Instance/CreateInstanceForm.php
	modules/monitoring/application/forms/Config/Instance/EditInstanceForm.php
	modules/monitoring/application/forms/Config/SecurityForm.php
2014-08-29 16:05:56 +02:00

120 lines
3.3 KiB
PHP

<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Form\Config\Authentication;
use Exception;
use Icinga\Web\Form;
use Icinga\Web\Request;
use Icinga\Data\ResourceFactory;
use Icinga\Authentication\Backend\DbUserBackend;
/**
* Form class for adding/modifying database authentication backends
*/
class DbBackendForm extends Form
{
/**
* The database resource names the user can choose from
*
* @var array
*/
protected $resources;
/**
* Initialize this form
*/
public function init()
{
$this->setName('form_config_authbackend_db');
}
/**
* Set the resource names the user can choose from
*
* @param array $resources The resources to choose from
*
* @return self
*/
public function setResources(array $resources)
{
$this->resources = $resources;
return $this;
}
/**
* @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' => false === empty($this->resources)
? array_combine($this->resources, $this->resources)
: array()
)
),
$this->createElement(
'hidden',
'backend',
array(
'required' => true,
'value' => 'db'
)
)
);
}
/**
* Validate that the selected resource is a valid database authentication backend
*
* @see Form::onSuccess()
*/
public function onSuccess(Request $request)
{
if (false === $this->isValidAuthenticationBackend($this)) {
return false;
}
}
/**
* Validate the configuration by creating a backend and requesting the user count
*
* @param Form $form The form to fetch the configuration values from
*
* @return bool Whether validation succeeded or not
*/
public function isValidAuthenticationBackend(Form $form)
{
$element = $form->getElement('resource');
try {
$dbUserBackend = new DbUserBackend(ResourceFactory::create($element->getValue()));
if ($dbUserBackend->count() < 1) {
$element->addError(t('No users found under the specified database backend'));
return false;
}
} catch (Exception $e) {
$element->addError(sprintf(t('Using the specified backend failed: %s'), $e->getMessage()));
return false;
}
return true;
}
}