LdapBackendForm: replace create with createElements and getResources with __construct

refs #5525
This commit is contained in:
Alexander Klimov 2014-07-29 12:21:58 +02:00
parent 5a738112ea
commit 5e6ef57590

View File

@ -9,6 +9,7 @@ use \Zend_Config;
use Icinga\Web\Form; use Icinga\Web\Form;
use Icinga\Data\ResourceFactory; use Icinga\Data\ResourceFactory;
use Icinga\Authentication\Backend\LdapUserBackend; use Icinga\Authentication\Backend\LdapUserBackend;
use Icinga\Exception\ConfigurationError;
/** /**
* Form for adding or modifying LDAP authentication backends * Form for adding or modifying LDAP authentication backends
@ -16,84 +17,70 @@ use Icinga\Authentication\Backend\LdapUserBackend;
class LdapBackendForm extends BaseBackendForm class LdapBackendForm extends BaseBackendForm
{ {
/** /**
* Return content of the resources.ini or previously set resources * @var array
*
* @return array
*/ */
public function getResources() protected $resources;
public function __construct()
{ {
if ($this->resources === null) { $ldapResources = array_keys(
$res = ResourceFactory::getResourceConfigs('ldap')->toArray(); ResourceFactory::getResourceConfigs('ldap')->toArray()
);
if (empty($ldapResources)) {
throw new ConfigurationError(
t('There are no LDAP resources')
);
}
$this->resources = array_combine($ldapResources, $ldapResources);
foreach (array_keys($res) as $key) { parent::__construct();
$res[$key] = $key;
} }
return $res; public function createElements(array $formData)
} else {
return $this->resources;
}
}
/**
* Create this form and add all required elements
*
* @see Form::create()
*/
public function create()
{ {
$this->setName('form_modify_backend'); return array(
$name = $this->filterName($this->getBackendName()); $this->createElement(
$backend = $this->getBackend();
$this->addElement(
'text', 'text',
'backend_' . $name . '_name', 'name',
array( array(
'required' => true, 'required' => true,
'allowEmpty' => false, 'allowEmpty' => false,
'label' => t('Backend Name'), 'label' => t('Backend Name'),
'helptext' => t('The name of this authentication backend'), 'helptext' => t('The name of this authentication backend')
'value' => $this->getBackendName()
) )
); ),
$this->createElement(
$this->addElement(
'select', 'select',
'backend_' . $name . '_resource', 'resource',
array( array(
'required' => true, 'required' => true,
'allowEmpty' => false, 'allowEmpty' => false,
'label' => t('LDAP Resource'), 'label' => t('LDAP Resource'),
'helptext' => t('The resource to use for authenticating with this provider'), 'helptext' => t('The resource to use for authenticating with this provider'),
'value' => $this->getBackend()->get('resource'), 'multiOptions' => $this->resources
'multiOptions' => $this->getResources()
) )
); ),
$this->createElement(
$this->addElement(
'text', 'text',
'backend_' . $name . '_user_class', 'user_class',
array( array(
'required' => true, 'required' => true,
'label' => t('LDAP User Object Class'), 'label' => t('LDAP User Object Class'),
'helptext' => t('The object class used for storing users on the ldap server'), 'helptext' => t('The object class used for storing users on the ldap server'),
'value' => $backend->get('user_class', 'inetOrgPerson') 'value' => 'inetOrgPerson'
) )
); ),
$this->createElement(
$this->addElement(
'text', 'text',
'backend_' . $name . '_user_name_attribute', 'user_name_attribute',
array( array(
'required' => true, 'required' => true,
'label' => t('LDAP User Name Attribute'), 'label' => t('LDAP User Name Attribute'),
'helptext' => t('The attribute name used for storing the user name on the ldap server'), 'helptext' => t('The attribute name used for storing the user name on the ldap server'),
'value' => $backend->get('user_name_attribute', 'uid') 'value' => 'uid'
) )
); ),
$this->createElement(
$this->addElement(
'button', 'button',
'btn_submit', 'btn_submit',
array( array(
@ -103,6 +90,15 @@ class LdapBackendForm extends BaseBackendForm
'class' => 'btn btn-cta btn-wide', 'class' => 'btn btn-cta btn-wide',
'label' => '<i class="icinga-icon-save"></i> Save Backend' 'label' => '<i class="icinga-icon-save"></i> Save Backend'
) )
),
$this->createElement(
'hidden',
'backend',
array(
'required' => true,
'value' => 'ldap'
)
)
); );
} }
@ -115,15 +111,14 @@ class LdapBackendForm extends BaseBackendForm
*/ */
public function getConfig() public function getConfig()
{ {
$prefix = 'backend_' . $this->filterName($this->getBackendName()) . '_'; return array(
$section = $this->getValue($prefix . 'name'); $this->getValue('name') => array(
$cfg = array(
'backend' => 'ldap', 'backend' => 'ldap',
'resource' => $this->getValue($prefix . 'resource'), 'resource' => $this->getValue('resource'),
'user_class' => $this->getValue($prefix . 'user_class'), 'user_class' => $this->getValue('user_class'),
'user_name_attribute' => $this->getValue($prefix . 'user_name_attribute') 'user_name_attribute' => $this->getValue('user_name_attribute')
)
); );
return array($section => $cfg);
} }
/** /**
@ -146,8 +141,7 @@ class LdapBackendForm extends BaseBackendForm
} }
try { try {
$cfg = $this->getConfig(); $cfg = $this->getConfig();
$backendName = 'backend_' . $this->filterName($this->getBackendName()) . '_name'; $backendConfig = new Zend_Config($cfg[$this->getValue('name')]);
$backendConfig = new Zend_Config($cfg[$this->getValue($backendName)]);
$backend = ResourceFactory::createResource(ResourceFactory::getResourceConfig($backendConfig->resource)); $backend = ResourceFactory::createResource(ResourceFactory::getResourceConfig($backendConfig->resource));
$testConn = new LdapUserBackend( $testConn = new LdapUserBackend(
$backend, $backend,