Make configuration of custom user-group backends possible

refs 
This commit is contained in:
Johannes Meyer 2021-06-02 10:55:57 +02:00
parent a972751e23
commit 0756797fbb
3 changed files with 41 additions and 1 deletions
application/forms/Config/UserGroup
library/Icinga/Authentication/UserGroup

@ -27,6 +27,13 @@ class UserGroupBackendForm extends ConfigForm
*/
protected $backendToLoad;
/**
* Known custom backends
*
* @var array
*/
protected $customBackends;
/**
* Create a user group backend by using the given form's values and return its inspection results
*
@ -51,6 +58,7 @@ class UserGroupBackendForm extends ConfigForm
{
$this->setName('form_config_usergroupbackend');
$this->setSubmitLabel($this->translate('Save Changes'));
$this->customBackends = UserGroupBackend::getCustomBackendConfigForms();
}
/**
@ -71,6 +79,10 @@ class UserGroupBackendForm extends ConfigForm
case 'msldap':
return new LdapUserGroupBackendForm();
default:
if (isset($this->customBackends[$type])) {
return new $this->customBackends[$type]();
}
throw new InvalidArgumentException(
sprintf($this->translate('Invalid backend type "%s" provided'), $type)
);
@ -174,13 +186,15 @@ class UserGroupBackendForm extends ConfigForm
*/
public function createElements(array $formData)
{
// TODO(jom): We did not think about how to configure custom group backends yet!
$backendTypes = array(
'db' => $this->translate('Database'),
'ldap' => 'LDAP',
'msldap' => 'ActiveDirectory'
);
$customBackendTypes = array_keys($this->customBackends);
$backendTypes += array_combine($customBackendTypes, $customBackendTypes);
$backendType = isset($formData['type']) ? $formData['type'] : null;
if ($backendType === null) {
$backendType = key($backendTypes);

@ -68,6 +68,22 @@ class UserGroupBackend
}
}
/**
* Get config forms of all custom user group backends
*/
public static function getCustomBackendConfigForms()
{
$customBackendConfigForms = [];
static::registerCustomUserGroupBackends();
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 group backend
*

@ -43,4 +43,14 @@ interface UserGroupBackendInterface
* @return null|string The name of the backend or null in case this information is not available
*/
public function getUserBackendName($username);
/**
* 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();
}