2013-08-16 16:24:12 +02:00
|
|
|
<?php
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
|
|
|
namespace Icinga\Form\Config\Authentication;
|
|
|
|
|
2014-08-27 15:51:49 +02:00
|
|
|
use Exception;
|
|
|
|
use Zend_Config;
|
2014-04-16 11:50:58 +02:00
|
|
|
use Icinga\Data\ResourceFactory;
|
2014-06-30 18:14:55 +02:00
|
|
|
use Icinga\Authentication\DbConnection;
|
|
|
|
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-04-16 11:50:58 +02:00
|
|
|
* Return content of the resources.ini or previously set resources
|
2013-08-16 16:24:12 +02:00
|
|
|
*
|
2014-04-16 11:50:58 +02:00
|
|
|
* @return array
|
2013-08-16 16:24:12 +02:00
|
|
|
*/
|
2014-04-16 11:50:58 +02:00
|
|
|
public function getResources()
|
2013-08-16 16:24:12 +02:00
|
|
|
{
|
2014-04-16 11:50:58 +02:00
|
|
|
if ($this->resources === null) {
|
|
|
|
$res = ResourceFactory::getResourceConfigs('db')->toArray();
|
|
|
|
|
|
|
|
foreach (array_keys($res) as $key) {
|
|
|
|
$res[$key] = $key;
|
2013-08-16 16:24:12 +02:00
|
|
|
}
|
2014-04-16 11:50:58 +02:00
|
|
|
|
|
|
|
return $res;
|
|
|
|
} else {
|
|
|
|
return $this->resources;
|
2013-08-16 16:24:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-21 11:02:53 +02:00
|
|
|
* Create this form and add all required elements
|
|
|
|
*
|
|
|
|
* @see Form::create()
|
|
|
|
*/
|
2013-08-16 16:24:12 +02:00
|
|
|
public function create()
|
|
|
|
{
|
2013-08-28 10:12:27 +02:00
|
|
|
$this->setName('form_modify_backend');
|
2013-08-16 16:24:12 +02:00
|
|
|
$name = $this->filterName($this->getBackendName());
|
|
|
|
$this->addElement(
|
|
|
|
'text',
|
|
|
|
'backend_' . $name . '_name',
|
|
|
|
array(
|
2013-08-21 11:02:53 +02:00
|
|
|
'required' => true,
|
2014-04-16 11:50:58 +02:00
|
|
|
'allowEmpty' => false,
|
|
|
|
'label' => t('Backend Name'),
|
|
|
|
'helptext' => t('The name of this authentication provider'),
|
2013-08-21 11:02:53 +02:00
|
|
|
'value' => $this->getBackendName()
|
2013-08-16 16:24:12 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->addElement(
|
|
|
|
'select',
|
|
|
|
'backend_' . $name . '_resource',
|
|
|
|
array(
|
2014-04-16 11:50:58 +02:00
|
|
|
'required' => true,
|
|
|
|
'allowEmpty' => false,
|
|
|
|
'label' => t('Database Connection'),
|
|
|
|
'helptext' => t('The database connection to use for authenticating with this provider'),
|
|
|
|
'value' => $this->getBackend()->get('resource'),
|
|
|
|
'multiOptions' => $this->getResources()
|
2013-08-16 16:24:12 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2013-10-23 12:25:51 +02:00
|
|
|
$this->addElement(
|
|
|
|
'button',
|
|
|
|
'btn_submit',
|
|
|
|
array(
|
|
|
|
'type' => 'submit',
|
|
|
|
'value' => '1',
|
|
|
|
'escape' => false,
|
|
|
|
'class' => 'btn btn-cta btn-wide',
|
|
|
|
'label' => '<i class="icinga-icon-save"></i> Save Backend'
|
|
|
|
)
|
|
|
|
);
|
2013-08-16 16:24:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the datatbase authentication backend configuration for this form
|
|
|
|
*
|
2013-08-21 11:02:53 +02:00
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
* @see BaseBackendForm::getConfig()
|
2013-08-16 16:24:12 +02:00
|
|
|
*/
|
|
|
|
public function getConfig()
|
|
|
|
{
|
2014-04-16 11:50:58 +02:00
|
|
|
$prefix = 'backend_' . $this->filterName($this->getBackendName()) . '_';
|
2013-08-16 16:24:12 +02:00
|
|
|
$section = $this->getValue($prefix . 'name');
|
|
|
|
$cfg = array(
|
2014-04-16 11:50:58 +02:00
|
|
|
'backend' => 'db',
|
|
|
|
'resource' => $this->getValue($prefix . 'resource'),
|
2013-08-16 16:24:12 +02:00
|
|
|
);
|
2014-04-16 11:50:58 +02:00
|
|
|
|
|
|
|
return array($section => $cfg);
|
2013-08-16 16:24:12 +02:00
|
|
|
}
|
2013-08-26 16:56:23 +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
|
|
|
|
*
|
2013-08-27 14:37:22 +02:00
|
|
|
* @see BaseBackendForm::isValidAuthenticationBackend
|
|
|
|
*/
|
|
|
|
public function isValidAuthenticationBackend()
|
2013-08-26 16:56:23 +02:00
|
|
|
{
|
2013-08-26 17:23:31 +02:00
|
|
|
try {
|
2014-08-28 11:42:22 +02:00
|
|
|
$dbUserBackend = new DbUserBackend(ResourceFactory::create(
|
2014-04-28 16:45:37 +02:00
|
|
|
$this->getValue('backend_' . $this->filterName($this->getBackendName()) . '_resource')
|
|
|
|
));
|
|
|
|
if ($dbUserBackend->count() < 1) {
|
|
|
|
$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;
|
|
|
|
}
|
2013-08-26 16:56:23 +02:00
|
|
|
return true;
|
|
|
|
}
|
2013-08-16 16:24:12 +02:00
|
|
|
}
|