Let the form decide where to get the resource configuration from

refs #7163
This commit is contained in:
Johannes Meyer 2014-09-29 11:06:16 +02:00
parent 0e92e333aa
commit 4f688fa544
2 changed files with 29 additions and 9 deletions

View File

@ -102,19 +102,27 @@ class DbBackendForm extends Form
*/ */
public static function isValidAuthenticationBackend(Form $form) public static function isValidAuthenticationBackend(Form $form)
{ {
$element = $form->getElement('resource');
try { try {
$dbUserBackend = new DbUserBackend(ResourceFactory::create($element->getValue())); $dbUserBackend = new DbUserBackend(ResourceFactory::createResource($form->getResourceConfig()));
if ($dbUserBackend->count() < 1) { if ($dbUserBackend->count() < 1) {
$element->addError(t('No users found under the specified database backend')); $form->addError(t('No users found under the specified database backend'));
return false; return false;
} }
} catch (Exception $e) { } catch (Exception $e) {
$element->addError(sprintf(t('Using the specified backend failed: %s'), $e->getMessage())); $form->addError(sprintf(t('Using the specified backend failed: %s'), $e->getMessage()));
return false; return false;
} }
return true; return true;
} }
/**
* Return the configuration for the chosen resource
*
* @return Zend_Config
*/
public function getResourceConfig()
{
return ResourceFactory::getResourceConfig($this->getValue('resource'));
}
} }

View File

@ -8,6 +8,7 @@ use Exception;
use Icinga\Web\Form; use Icinga\Web\Form;
use Icinga\Web\Request; use Icinga\Web\Request;
use Icinga\Data\ResourceFactory; use Icinga\Data\ResourceFactory;
use Icinga\Exception\AuthenticationException;
use Icinga\Authentication\Backend\LdapUserBackend; use Icinga\Authentication\Backend\LdapUserBackend;
/** /**
@ -122,20 +123,31 @@ class LdapBackendForm extends Form
*/ */
public static function isValidAuthenticationBackend(Form $form) public static function isValidAuthenticationBackend(Form $form)
{ {
$element = $form->getElement('resource');
try { try {
$ldapUserBackend = new LdapUserBackend( $ldapUserBackend = new LdapUserBackend(
ResourceFactory::create($element->getValue()), ResourceFactory::createResource($form->getResourceConfig()),
$form->getElement('user_class')->getValue(), $form->getElement('user_class')->getValue(),
$form->getElement('user_name_attribute')->getValue() $form->getElement('user_name_attribute')->getValue()
); );
$ldapUserBackend->assertAuthenticationPossible(); $ldapUserBackend->assertAuthenticationPossible();
} catch (AuthenticationException $e) {
$form->addError($e->getMessage());
return false;
} catch (Exception $e) { } catch (Exception $e) {
$element->addError(sprintf(t('Connection validation failed: %s'), $e->getMessage())); $form->addError(sprintf(t('Unable to validate authentication: %s'), $e->getMessage()));
return false; return false;
} }
return true; return true;
} }
/**
* Return the configuration for the chosen resource
*
* @return Zend_Config
*/
public function getResourceConfig()
{
return ResourceFactory::getResourceConfig($this->getValue('resource'));
}
} }