2014-10-20 13:41:33 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2014-10-20 13:41:33 +02:00
|
|
|
|
2015-04-21 12:32:18 +02:00
|
|
|
namespace Icinga\Authentication\UserGroup;
|
2014-10-20 13:41:33 +02:00
|
|
|
|
2015-04-22 09:52:08 +02:00
|
|
|
use Icinga\Application\Logger;
|
|
|
|
use Icinga\Application\Icinga;
|
2014-11-18 13:11:52 +01:00
|
|
|
use Icinga\Data\ConfigObject;
|
2014-10-20 13:41:33 +02:00
|
|
|
use Icinga\Data\ResourceFactory;
|
|
|
|
use Icinga\Exception\ConfigurationError;
|
|
|
|
use Icinga\User;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class and factory for user group backends
|
|
|
|
*/
|
|
|
|
abstract class UserGroupBackend
|
|
|
|
{
|
2015-04-22 09:52:08 +02:00
|
|
|
/**
|
|
|
|
* The default user group backend types provided by Icinga Web 2
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $defaultBackends = array( // I would have liked it if I were able to declare this as constant :'(
|
|
|
|
'db',
|
|
|
|
'ini'
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The registered custom user group backends with their identifier as key and class name as value
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $customBackends;
|
|
|
|
|
2014-10-20 13:41:33 +02:00
|
|
|
/**
|
2015-04-22 09:35:06 +02:00
|
|
|
* The name of this backend
|
2014-10-20 13:41:33 +02:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $name;
|
|
|
|
|
|
|
|
/**
|
2015-04-22 09:35:06 +02:00
|
|
|
* Set this backend's name
|
2014-10-20 13:41:33 +02:00
|
|
|
*
|
2015-04-22 09:35:06 +02:00
|
|
|
* @param string $name
|
2014-10-20 13:41:33 +02:00
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setName($name)
|
|
|
|
{
|
|
|
|
$this->name = (string) $name;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-22 09:35:06 +02:00
|
|
|
* Return this backend's name
|
2014-10-20 13:41:33 +02:00
|
|
|
*
|
2015-04-22 09:35:06 +02:00
|
|
|
* @return string
|
2014-10-20 13:41:33 +02:00
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
2015-04-22 09:52:08 +02:00
|
|
|
/**
|
|
|
|
* Fetch all custom user group backends from all loaded modules
|
|
|
|
*/
|
|
|
|
public static function loadCustomUserGroupBackends()
|
|
|
|
{
|
|
|
|
if (static::$customBackends !== null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static::$customBackends = array();
|
|
|
|
$providedBy = array();
|
|
|
|
foreach (Icinga::app()->getModuleManager()->getLoadedModules() as $module) {
|
|
|
|
foreach ($module->getUserGroupBackends() as $identifier => $className) {
|
|
|
|
if (array_key_exists($identifier, $providedBy)) {
|
|
|
|
Logger::warning(
|
|
|
|
'Cannot register UserGroupBackend of type "%s" provided by module "%s".'
|
|
|
|
. ' The type is already provided by module "%s"',
|
|
|
|
$identifier,
|
|
|
|
$module->getName(),
|
|
|
|
$providedBy[$identifier]
|
|
|
|
);
|
|
|
|
} elseif (in_array($identifier, static::$defaultBackends)) {
|
|
|
|
Logger::warning(
|
|
|
|
'Cannot register UserGroupBackend of type "%s" provided by module "%s".'
|
|
|
|
. ' The type is a default type provided by Icinga Web 2',
|
|
|
|
$identifier,
|
|
|
|
$module->getName()
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$providedBy[$identifier] = $module->getName();
|
|
|
|
static::$customBackends[$identifier] = $className;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate and return the class for the given custom user group backend
|
|
|
|
*
|
|
|
|
* @param string $identifier The identifier of the custom user group backend
|
|
|
|
*
|
|
|
|
* @return string|null The name of the class or null in case there was no
|
|
|
|
* backend found with the given identifier
|
|
|
|
*
|
|
|
|
* @throws ConfigurationError In case the class could not be successfully validated
|
|
|
|
*/
|
|
|
|
protected static function getCustomUserGroupBackend($identifier)
|
|
|
|
{
|
|
|
|
static::loadCustomUserGroupBackends();
|
|
|
|
if (array_key_exists($identifier, static::$customBackends)) {
|
|
|
|
$className = static::$customBackends[$identifier];
|
|
|
|
if (! class_exists($className)) {
|
|
|
|
throw new ConfigurationError(
|
|
|
|
'Cannot utilize UserGroupBackend of type "%s". Class "%s" does not exist',
|
|
|
|
$identifier,
|
|
|
|
$className
|
|
|
|
);
|
|
|
|
} elseif (! is_subclass_of($className, __CLASS__)) {
|
|
|
|
throw new ConfigurationError(
|
|
|
|
'Cannot utilize UserGroupBackend of type "%s". Class "%s" is not a sub-type of UserGroupBackend',
|
|
|
|
$identifier,
|
|
|
|
$className
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $className;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-20 13:41:33 +02:00
|
|
|
/**
|
2015-04-22 09:35:06 +02:00
|
|
|
* Create and return a UserGroupBackend with the given name and given configuration applied to it
|
2014-10-20 13:41:33 +02:00
|
|
|
*
|
2014-11-18 13:11:52 +01:00
|
|
|
* @param string $name
|
|
|
|
* @param ConfigObject $backendConfig
|
2014-10-20 13:41:33 +02:00
|
|
|
*
|
2015-04-22 09:35:06 +02:00
|
|
|
* @return UserGroupBackend
|
|
|
|
*
|
|
|
|
* @throws ConfigurationError
|
2014-10-20 13:41:33 +02:00
|
|
|
*/
|
2014-11-18 13:11:52 +01:00
|
|
|
public static function create($name, ConfigObject $backendConfig)
|
2014-10-20 13:41:33 +02:00
|
|
|
{
|
|
|
|
if ($backendConfig->name !== null) {
|
|
|
|
$name = $backendConfig->name;
|
|
|
|
}
|
2015-04-22 09:35:06 +02:00
|
|
|
|
|
|
|
if (! ($backendType = strtolower($backendConfig->backend))) {
|
2014-10-20 13:41:33 +02:00
|
|
|
throw new ConfigurationError(
|
2015-04-22 09:35:06 +02:00
|
|
|
'Configuration for user group backend "%s" is missing the \'backend\' directive',
|
2014-10-20 13:41:33 +02:00
|
|
|
$name
|
|
|
|
);
|
|
|
|
}
|
2015-04-22 09:52:08 +02:00
|
|
|
if (in_array($backendType, static::$defaultBackends)) {
|
|
|
|
// The default backend check is the first one because of performance reasons:
|
|
|
|
// Do not attempt to load a custom user group backend unless it's actually required
|
|
|
|
} elseif (($customClass = static::getCustomUserGroupBackend($backendType)) !== null) {
|
|
|
|
$backend = new $customClass($backendConfig);
|
|
|
|
$backend->setName($name);
|
|
|
|
return $backend;
|
|
|
|
} else {
|
|
|
|
throw new ConfigurationError(
|
|
|
|
'Configuration for user group backend "%s" defines an invalid backend type.'
|
|
|
|
. ' Backend type "%s" is not supported',
|
|
|
|
$name,
|
|
|
|
$backendType
|
|
|
|
);
|
|
|
|
}
|
2015-04-22 09:35:06 +02:00
|
|
|
|
|
|
|
if ($backendConfig->resource === null) {
|
2014-10-20 13:41:33 +02:00
|
|
|
throw new ConfigurationError(
|
2015-04-22 09:35:06 +02:00
|
|
|
'Configuration for user group backend "%s" is missing the \'resource\' directive',
|
2014-10-20 13:41:33 +02:00
|
|
|
$name
|
|
|
|
);
|
|
|
|
}
|
2015-04-22 09:35:06 +02:00
|
|
|
$resource = ResourceFactory::create($backendConfig->resource);
|
|
|
|
|
2014-10-20 13:41:33 +02:00
|
|
|
switch ($backendType) {
|
|
|
|
case 'db':
|
|
|
|
$backend = new DbUserGroupBackend($resource);
|
|
|
|
break;
|
|
|
|
case 'ini':
|
|
|
|
$backend = new IniUserGroupBackend($resource);
|
|
|
|
break;
|
|
|
|
}
|
2015-04-22 09:35:06 +02:00
|
|
|
|
2014-10-20 13:41:33 +02:00
|
|
|
$backend->setName($name);
|
|
|
|
return $backend;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the groups the given user is a member of
|
|
|
|
*
|
2015-04-22 09:35:06 +02:00
|
|
|
* @param User $user
|
2014-10-20 13:41:33 +02:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
abstract public function getMemberships(User $user);
|
|
|
|
}
|