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,92 +17,87 @@ 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()
);
foreach (array_keys($res) as $key) { if (empty($ldapResources)) {
$res[$key] = $key; throw new ConfigurationError(
} t('There are no LDAP resources')
);
return $res;
} else {
return $this->resources;
} }
$this->resources = array_combine($ldapResources, $ldapResources);
parent::__construct();
} }
/** public function createElements(array $formData)
* 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(); 'text',
'name',
$this->addElement( array(
'text', 'required' => true,
'backend_' . $name . '_name', 'allowEmpty' => false,
array( 'label' => t('Backend Name'),
'required' => true, 'helptext' => t('The name of this authentication backend')
'allowEmpty' => false, )
'label' => t('Backend Name'), ),
'helptext' => t('The name of this authentication backend'), $this->createElement(
'value' => $this->getBackendName() 'select',
) 'resource',
); array(
'required' => true,
$this->addElement( 'allowEmpty' => false,
'select', 'label' => t('LDAP Resource'),
'backend_' . $name . '_resource', 'helptext' => t('The resource to use for authenticating with this provider'),
array( 'multiOptions' => $this->resources
'required' => true, )
'allowEmpty' => false, ),
'label' => t('LDAP Resource'), $this->createElement(
'helptext' => t('The resource to use for authenticating with this provider'), 'text',
'value' => $this->getBackend()->get('resource'), 'user_class',
'multiOptions' => $this->getResources() array(
) 'required' => true,
); 'label' => t('LDAP User Object Class'),
'helptext' => t('The object class used for storing users on the ldap server'),
$this->addElement( 'value' => 'inetOrgPerson'
'text', )
'backend_' . $name . '_user_class', ),
array( $this->createElement(
'required' => true, 'text',
'label' => t('LDAP User Object Class'), 'user_name_attribute',
'helptext' => t('The object class used for storing users on the ldap server'), array(
'value' => $backend->get('user_class', 'inetOrgPerson') 'required' => true,
) 'label' => t('LDAP User Name Attribute'),
); 'helptext' => t('The attribute name used for storing the user name on the ldap server'),
'value' => 'uid'
$this->addElement( )
'text', ),
'backend_' . $name . '_user_name_attribute', $this->createElement(
array( 'button',
'required' => true, 'btn_submit',
'label' => t('LDAP User Name Attribute'), array(
'helptext' => t('The attribute name used for storing the user name on the ldap server'), 'type' => 'submit',
'value' => $backend->get('user_name_attribute', 'uid') 'value' => '1',
) 'escape' => false,
); 'class' => 'btn btn-cta btn-wide',
'label' => '<i class="icinga-icon-save"></i> Save Backend'
$this->addElement( )
'button', ),
'btn_submit', $this->createElement(
array( 'hidden',
'type' => 'submit', 'backend',
'value' => '1', array(
'escape' => false, 'required' => true,
'class' => 'btn btn-cta btn-wide', 'value' => 'ldap'
'label' => '<i class="icinga-icon-save"></i> Save Backend' )
) )
); );
} }
@ -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('resource'),
'resource' => $this->getValue($prefix . 'resource'), 'user_class' => $this->getValue('user_class'),
'user_class' => $this->getValue($prefix . 'user_class'), 'user_name_attribute' => $this->getValue('user_name_attribute')
'user_name_attribute' => $this->getValue($prefix . '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,