Make class UserBackend being just a factory for user backends

refs #8826
This commit is contained in:
Johannes Meyer 2015-05-04 11:43:53 +02:00
parent 68657c02ee
commit 1824eb9c3b

View File

@ -3,25 +3,23 @@
namespace Icinga\Authentication\User; namespace Icinga\Authentication\User;
use Countable;
use Icinga\Application\Logger; use Icinga\Application\Logger;
use Icinga\Application\Icinga; use Icinga\Application\Icinga;
use Icinga\Data\ConfigObject; use Icinga\Data\ConfigObject;
use Icinga\Data\ResourceFactory; use Icinga\Data\ResourceFactory;
use Icinga\Exception\ConfigurationError; use Icinga\Exception\ConfigurationError;
use Icinga\User;
/** /**
* Base class for concrete user backends * Factory for user backends
*/ */
abstract class UserBackend implements Countable class UserBackend
{ {
/** /**
* The default user backend types provided by Icinga Web 2 * The default user backend types provided by Icinga Web 2
* *
* @var array * @var array
*/ */
private static $defaultBackends = array( // I would have liked it if I were able to declare this as constant :'( protected static $defaultBackends = array(
'external', 'external',
'db', 'db',
'ldap', 'ldap',
@ -36,39 +34,9 @@ abstract class UserBackend implements Countable
protected static $customBackends; protected static $customBackends;
/** /**
* The name of this backend * Register all custom user backends from all loaded modules
*
* @var string
*/ */
protected $name; protected static function registerCustomUserBackends()
/**
* Set this backend's name
*
* @param string $name
*
* @return $this
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Return this backend's name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Fetch all custom user backends from all loaded modules
*/
public static function loadCustomUserBackends()
{ {
if (static::$customBackends !== null) { if (static::$customBackends !== null) {
return; return;
@ -80,7 +48,7 @@ abstract class UserBackend implements Countable
foreach ($module->getUserBackends() as $identifier => $className) { foreach ($module->getUserBackends() as $identifier => $className) {
if (array_key_exists($identifier, $providedBy)) { if (array_key_exists($identifier, $providedBy)) {
Logger::warning( Logger::warning(
'Cannot register UserBackend of type "%s" provided by module "%s".' 'Cannot register user backend of type "%s" provided by module "%s".'
. ' The type is already provided by module "%s"', . ' The type is already provided by module "%s"',
$identifier, $identifier,
$module->getName(), $module->getName(),
@ -88,7 +56,7 @@ abstract class UserBackend implements Countable
); );
} elseif (in_array($identifier, static::$defaultBackends)) { } elseif (in_array($identifier, static::$defaultBackends)) {
Logger::warning( Logger::warning(
'Cannot register UserBackend of type "%s" provided by module "%s".' 'Cannot register user backend of type "%s" provided by module "%s".'
. ' The type is a default type provided by Icinga Web 2', . ' The type is a default type provided by Icinga Web 2',
$identifier, $identifier,
$module->getName() $module->getName()
@ -102,29 +70,23 @@ abstract class UserBackend implements Countable
} }
/** /**
* Validate and return the class for the given custom user backend * Return the class for the given custom user backend
* *
* @param string $identifier The identifier of the custom user backend * @param string $identifier The identifier of the custom user backend
* *
* @return string|null The name of the class or null in case there was no * @return string|null The name of the class or null in case there was no
* backend found with the given identifier * backend found with the given identifier
* *
* @throws ConfigurationError In case the class could not be successfully validated * @throws ConfigurationError In case the class associated to the given identifier does not exist
*/ */
protected static function getCustomUserBackend($identifier) protected static function getCustomUserBackend($identifier)
{ {
static::loadCustomUserBackends(); static::registerCustomUserBackends();
if (array_key_exists($identifier, static::$customBackends)) { if (array_key_exists($identifier, static::$customBackends)) {
$className = static::$customBackends[$identifier]; $className = static::$customBackends[$identifier];
if (! class_exists($className)) { if (! class_exists($className)) {
throw new ConfigurationError( throw new ConfigurationError(
'Cannot utilize UserBackend of type "%s". Class "%s" does not exist', 'Cannot utilize user backend of type "%s". Class "%s" does not exist',
$identifier,
$className
);
} elseif (! is_subclass_of($className, __CLASS__)) {
throw new ConfigurationError(
'Cannot utilize UserBackend of type "%s". Class "%s" is not a sub-type of UserBackend',
$identifier, $identifier,
$className $className
); );
@ -135,12 +97,12 @@ abstract class UserBackend implements Countable
} }
/** /**
* Create and return a UserBackend with the given name and given configuration applied to it * Create and return a user backend with the given name and given configuration applied to it
* *
* @param string $name * @param string $name
* @param ConfigObject $backendConfig * @param ConfigObject $backendConfig
* *
* @return UserBackend * @return UserBackendInterface
* *
* @throws ConfigurationError * @throws ConfigurationError
*/ */
@ -152,7 +114,7 @@ abstract class UserBackend implements Countable
if (! ($backendType = strtolower($backendConfig->backend))) { if (! ($backendType = strtolower($backendConfig->backend))) {
throw new ConfigurationError( throw new ConfigurationError(
'Authentication configuration for backend "%s" is missing the \'backend\' directive', 'Authentication configuration for user backend "%s" is missing the \'backend\' directive',
$name $name
); );
} }
@ -166,11 +128,19 @@ abstract class UserBackend implements Countable
// Do not attempt to load a custom user backend unless it's actually required // Do not attempt to load a custom user backend unless it's actually required
} elseif (($customClass = static::getCustomUserBackend($backendType)) !== null) { } elseif (($customClass = static::getCustomUserBackend($backendType)) !== null) {
$backend = new $customClass($backendConfig); $backend = new $customClass($backendConfig);
if (! is_a($backend, 'Icinga\Authentication\User\UserBackendInterface')) {
throw new ConfigurationError(
'Cannot utilize user backend of type "%s". Class "%s" does not implement UserBackendInterface',
$backendType,
$customClass
);
}
$backend->setName($name); $backend->setName($name);
return $backend; return $backend;
} else { } else {
throw new ConfigurationError( throw new ConfigurationError(
'Authentication configuration for backend "%s" defines an invalid backend type.' 'Authentication configuration for user backend "%s" defines an invalid backend type.'
. ' Backend type "%s" is not supported', . ' Backend type "%s" is not supported',
$name, $name,
$backendType $backendType
@ -179,7 +149,7 @@ abstract class UserBackend implements Countable
if ($backendConfig->resource === null) { if ($backendConfig->resource === null) {
throw new ConfigurationError( throw new ConfigurationError(
'Authentication configuration for backend "%s" is missing the \'resource\' directive', 'Authentication configuration for user backend "%s" is missing the \'resource\' directive',
$name $name
); );
} }
@ -239,14 +209,4 @@ abstract class UserBackend implements Countable
$backend->setName($name); $backend->setName($name);
return $backend; return $backend;
} }
/**
* Authenticate the given user
*
* @param User $user
* @param string $password
*
* @return bool
*/
abstract public function authenticate(User $user, $password);
} }