2013-08-16 10:24:12 -04:00
|
|
|
<?php
|
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
|
2014-11-14 04:57:14 -05:00
|
|
|
namespace Icinga\Forms\Config\Authentication;
|
2013-08-16 10:24:12 -04:00
|
|
|
|
2014-08-11 04:43:54 -04:00
|
|
|
use Exception;
|
2014-08-29 09:16:13 -04:00
|
|
|
use Icinga\Web\Form;
|
2014-11-18 07:11:52 -05:00
|
|
|
use Icinga\Data\ConfigObject;
|
2014-04-16 05:50:58 -04:00
|
|
|
use Icinga\Data\ResourceFactory;
|
2014-08-11 04:43:54 -04:00
|
|
|
use Icinga\Authentication\Backend\DbUserBackend;
|
2013-08-16 10:24:12 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Form class for adding/modifying database authentication backends
|
|
|
|
|
*/
|
2014-08-29 09:16:13 -04:00
|
|
|
class DbBackendForm extends Form
|
2013-08-16 10:24:12 -04:00
|
|
|
{
|
|
|
|
|
/**
|
2014-08-29 09:16:13 -04:00
|
|
|
* The database resource names the user can choose from
|
2014-08-11 04:43:54 -04:00
|
|
|
*
|
2014-07-29 06:22:43 -04:00
|
|
|
* @var array
|
2013-08-16 10:24:12 -04:00
|
|
|
*/
|
2014-07-29 06:22:43 -04:00
|
|
|
protected $resources;
|
2014-04-16 05:50:58 -04:00
|
|
|
|
2014-08-11 04:39:13 -04:00
|
|
|
/**
|
|
|
|
|
* Initialize this form
|
|
|
|
|
*/
|
|
|
|
|
public function init()
|
2013-08-16 10:24:12 -04:00
|
|
|
{
|
2014-08-29 09:16:13 -04:00
|
|
|
$this->setName('form_config_authbackend_db');
|
|
|
|
|
}
|
2013-08-16 10:24:12 -04:00
|
|
|
|
2014-08-29 09:16:13 -04:00
|
|
|
/**
|
|
|
|
|
* 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;
|
2014-07-29 06:22:43 -04:00
|
|
|
}
|
2013-08-16 10:24:12 -04:00
|
|
|
|
2014-08-11 04:43:54 -04:00
|
|
|
/**
|
|
|
|
|
* @see Form::createElements()
|
|
|
|
|
*/
|
2014-07-29 06:22:43 -04:00
|
|
|
public function createElements(array $formData)
|
|
|
|
|
{
|
2014-09-03 06:21:31 -04:00
|
|
|
$this->addElement(
|
|
|
|
|
'text',
|
|
|
|
|
'name',
|
|
|
|
|
array(
|
|
|
|
|
'required' => true,
|
2015-01-19 05:26:23 -05:00
|
|
|
'label' => $this->translate('Backend Name'),
|
|
|
|
|
'description' => $this->translate(
|
2014-10-21 10:15:04 -04:00
|
|
|
'The name of this authentication provider that is used to differentiate it from others'
|
|
|
|
|
),
|
2013-10-23 06:25:51 -04:00
|
|
|
)
|
|
|
|
|
);
|
2014-09-03 06:21:31 -04:00
|
|
|
$this->addElement(
|
|
|
|
|
'select',
|
|
|
|
|
'resource',
|
|
|
|
|
array(
|
|
|
|
|
'required' => true,
|
2015-01-19 05:26:23 -05:00
|
|
|
'label' => $this->translate('Database Connection'),
|
|
|
|
|
'description' => $this->translate(
|
|
|
|
|
'The database connection to use for authenticating with this provider'
|
|
|
|
|
),
|
2014-09-03 06:21:31 -04:00
|
|
|
'multiOptions' => false === empty($this->resources)
|
|
|
|
|
? array_combine($this->resources, $this->resources)
|
|
|
|
|
: array()
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
$this->addElement(
|
|
|
|
|
'hidden',
|
|
|
|
|
'backend',
|
|
|
|
|
array(
|
2014-11-18 09:06:36 -05:00
|
|
|
'disabled' => true,
|
2014-09-03 06:21:31 -04:00
|
|
|
'value' => 'db'
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return $this;
|
2013-08-16 10:24:12 -04:00
|
|
|
}
|
|
|
|
|
|
2013-08-27 08:37:22 -04:00
|
|
|
/**
|
2014-08-29 09:16:13 -04:00
|
|
|
* Validate that the selected resource is a valid database authentication backend
|
2013-08-27 08:37:22 -04:00
|
|
|
*
|
2014-08-29 09:16:13 -04:00
|
|
|
* @see Form::onSuccess()
|
|
|
|
|
*/
|
2014-11-14 08:59:12 -05:00
|
|
|
public function onSuccess()
|
2014-08-29 09:16:13 -04:00
|
|
|
{
|
2014-09-29 05:02:45 -04:00
|
|
|
if (false === static::isValidAuthenticationBackend($this)) {
|
2014-08-29 09:16:13 -04:00
|
|
|
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
|
2014-04-16 05:50:58 -04:00
|
|
|
*
|
2014-08-29 09:16:13 -04:00
|
|
|
* @return bool Whether validation succeeded or not
|
2013-08-27 08:37:22 -04:00
|
|
|
*/
|
2014-09-29 05:02:45 -04:00
|
|
|
public static function isValidAuthenticationBackend(Form $form)
|
2013-08-26 10:56:23 -04:00
|
|
|
{
|
2013-08-26 11:23:31 -04:00
|
|
|
try {
|
2014-09-29 05:06:16 -04:00
|
|
|
$dbUserBackend = new DbUserBackend(ResourceFactory::createResource($form->getResourceConfig()));
|
2014-04-28 10:45:37 -04:00
|
|
|
if ($dbUserBackend->count() < 1) {
|
2015-01-19 07:47:53 -05:00
|
|
|
$form->addError($form->translate('No users found under the specified database backend'));
|
2013-08-26 11:23:31 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
2014-04-16 05:50:58 -04:00
|
|
|
} catch (Exception $e) {
|
2015-01-19 07:47:53 -05:00
|
|
|
$form->addError(sprintf($form->translate('Using the specified backend failed: %s'), $e->getMessage()));
|
2013-08-26 11:23:31 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
2014-08-11 04:43:54 -04:00
|
|
|
|
2013-08-26 10:56:23 -04:00
|
|
|
return true;
|
|
|
|
|
}
|
2014-09-29 05:06:16 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the configuration for the chosen resource
|
|
|
|
|
*
|
2014-11-18 07:11:52 -05:00
|
|
|
* @return ConfigObject
|
2014-09-29 05:06:16 -04:00
|
|
|
*/
|
|
|
|
|
public function getResourceConfig()
|
|
|
|
|
{
|
|
|
|
|
return ResourceFactory::getResourceConfig($this->getValue('resource'));
|
|
|
|
|
}
|
2013-08-16 10:24:12 -04:00
|
|
|
}
|