Make configuration of custom user-group backends possible

refs #2840
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

View File

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

View File

@ -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 * Return the class for the given custom user group backend
* *

View File

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