UserBackend: Implement interface ConfigAwareFactory

refs #9609
This commit is contained in:
Johannes Meyer 2015-07-29 13:44:26 +02:00
parent 22bcc790ef
commit 13edbf901d
4 changed files with 68 additions and 4 deletions

View File

@ -7,6 +7,7 @@ use ErrorException;
use Exception;
use LogicException;
use Icinga\Application\Modules\Manager as ModuleManager;
use Icinga\Authentication\User\UserBackend;
use Icinga\Data\ConfigObject;
use Icinga\Data\ResourceFactory;
use Icinga\Exception\ConfigurationError;
@ -544,6 +545,24 @@ abstract class ApplicationBootstrap
return $this;
}
/**
* Set up the user backend factory
*
* @return $this
*/
protected function setupUserBackendFactory()
{
try {
UserBackend::setConfig(Config::app('authentication'));
} catch (NotReadableError $e) {
Logger::error(
new IcingaException('Cannot load user backend configuration. An exception was thrown:', $e)
);
}
return $this;
}
/**
* Detect the timezone
*

View File

@ -43,6 +43,7 @@ class Cli extends ApplicationBootstrap
->setupLogger()
->setupResourceFactory()
->setupModuleManager()
->setupUserBackendFactory()
->loadSetupModuleIfNecessary();
}

View File

@ -99,6 +99,7 @@ class Web extends ApplicationBootstrap
->setupZendMvc()
->setupFormNamespace()
->setupModuleManager()
->setupUserBackendFactory()
->loadSetupModuleIfNecessary()
->loadEnabledModules()
->setupRoute()

View File

@ -9,11 +9,12 @@ use Icinga\Application\Icinga;
use Icinga\Data\ConfigObject;
use Icinga\Data\ResourceFactory;
use Icinga\Exception\ConfigurationError;
use Icinga\Util\ConfigAwareFactory;
/**
* Factory for user backends
*/
class UserBackend
class UserBackend implements ConfigAwareFactory
{
/**
* The default user backend types provided by Icinga Web 2
@ -34,6 +35,48 @@ class UserBackend
*/
protected static $customBackends;
/**
* User backend configuration
*
* @var Config
*/
private static $backends;
/**
* Set user backend configuration
*
* @param Config $config
*/
public static function setConfig($config)
{
self::$backends = $config;
}
/**
* Return the configuration of all existing user backends
*
* @return Config
*/
public static function getBackendConfigs()
{
self::assertBackendsExist();
return self::$backends;
}
/**
* Check if any user backends exist. If not, throw an error.
*
* @throws ConfigurationError
*/
private static function assertBackendsExist()
{
if (self::$backends === null) {
throw new ConfigurationError(
'User backends not set up. Please contact your Icinga Web administrator'
);
}
}
/**
* Register all custom user backends from all loaded modules
*/
@ -110,9 +153,9 @@ class UserBackend
public static function create($name, ConfigObject $backendConfig = null)
{
if ($backendConfig === null) {
$authConfig = Config::app('authentication');
if ($authConfig->hasSection($name)) {
$backendConfig = $authConfig->getSection($name);
self::assertBackendsExist();
if (self::$backends->hasSection($name)) {
$backendConfig = self::$backends->getSection($name);
} else {
throw new ConfigurationError('User backend "%s" does not exist', $name);
}