Make configuration of custom user backends possible

refs Icinga/icingaweb2#2840
This commit is contained in:
Gianluca Piccolo 2021-05-27 16:42:33 +02:00 committed by Johannes Meyer
parent 9270633b23
commit 1e7f700102
4 changed files with 43 additions and 0 deletions

View File

@ -37,6 +37,13 @@ class UserBackendConfigForm extends ConfigForm
*/
protected $backendToLoad;
/**
* The loaded custom backends list
*
* @var array
*/
protected $customBackends;
/**
* Initialize this form
*/
@ -45,6 +52,7 @@ class UserBackendConfigForm extends ConfigForm
$this->setName('form_config_authbackend');
$this->setSubmitLabel($this->translate('Save Changes'));
$this->setValidatePartial(true);
$this->customBackends = UserBackend::getCustomBackendConfigForms();
}
/**
@ -114,6 +122,10 @@ class UserBackendConfigForm extends ConfigForm
$form = new ExternalBackendForm();
break;
default:
if (isset($this->customBackends[$type])) {
return new $this->customBackends[$type]();
}
throw new InvalidArgumentException(
sprintf($this->translate('Invalid backend type "%s" provided'), $type)
);
@ -274,6 +286,9 @@ class UserBackendConfigForm extends ConfigForm
$backendTypes['external'] = $this->translate('External');
}
$customBackendTypes = array_keys($this->customBackends);
$backendTypes += array_combine($customBackendTypes, $customBackendTypes);
if ($backendType === null) {
$backendType = key($backendTypes);
}

View File

@ -113,6 +113,22 @@ class UserBackend implements ConfigAwareFactory
}
}
/**
* Get config forms of all custom user backends
*/
public static function getCustomBackendConfigForms()
{
$customBackendConfigForms = [];
static::registerCustomUserBackends();
foreach (self::$customBackends as $customBackendType => $customBackendClass) {
if (method_exists($customBackendClass, 'getConfigurationFormClass')) {
$customBackendConfigForms[$customBackendType] = $customBackendClass::getConfigurationFormClass();
}
}
return $customBackendConfigForms;
}
/**
* Return the class for the given custom user backend
*

View File

@ -26,4 +26,14 @@ interface UserBackendInterface extends Authenticatable
* @return string
*/
public function getName();
/**
* Return this backend's configuration form class path
*
* This is not part of the interface to not break existing implementations.
* If you need a custom backend form, implement this method.
*
* @return string
*/
//public static function getConfigurationFormClass();
}

View File

@ -7,6 +7,7 @@ use Icinga\Test\BaseTestCase;
use Icinga\Application\Config;
use Icinga\Forms\Config\UserBackendConfigForm;
use Icinga\Forms\Config\UserBackendReorderForm;
use Icinga\Application\Icinga;
class UserBackendConfigFormWithoutSave extends UserBackendConfigForm
{
@ -41,6 +42,7 @@ class AuthenticationBackendReorderFormTest extends BaseTestCase
)
);
Icinga::app()->shouldReceive('getModuleManager->getLoadedModules')->andReturn([]);
$this->getRequestMock()->shouldReceive('getMethod')->andReturn('POST')
->shouldReceive('isPost')->andReturn(true)
->shouldReceive('getPost')->andReturn(array('backend_newpos' => 'test3|1'));