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;
|
|
|
|
|
|
|
|
/**
|
2015-05-05 08:26:38 +02:00
|
|
|
* Factory for user group backends
|
2014-10-20 13:41:33 +02:00
|
|
|
*/
|
2015-05-05 08:26:38 +02:00
|
|
|
class UserGroupBackend
|
2014-10-20 13:41:33 +02:00
|
|
|
{
|
2015-04-22 09:52:08 +02:00
|
|
|
/**
|
|
|
|
* The default user group backend types provided by Icinga Web 2
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2015-05-05 08:26:38 +02:00
|
|
|
protected static $defaultBackends = array(
|
2015-04-22 09:52:08 +02:00
|
|
|
'db',
|
2015-06-03 16:27:50 +02:00
|
|
|
'ldap',
|
|
|
|
'msldap',
|
2015-05-13 10:45:54 +02:00
|
|
|
//'ini'
|
2015-04-22 09:52:08 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-05-05 08:26:38 +02:00
|
|
|
* Register all custom user group backends from all loaded modules
|
2015-04-22 09:52:08 +02:00
|
|
|
*/
|
2015-05-05 08:26:38 +02:00
|
|
|
public static function registerCustomUserGroupBackends()
|
2015-04-22 09:52:08 +02:00
|
|
|
{
|
|
|
|
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(
|
2015-05-05 08:26:38 +02:00
|
|
|
'Cannot register user group backend of type "%s" provided by module "%s".'
|
2015-04-22 09:52:08 +02:00
|
|
|
. ' The type is already provided by module "%s"',
|
|
|
|
$identifier,
|
|
|
|
$module->getName(),
|
|
|
|
$providedBy[$identifier]
|
|
|
|
);
|
|
|
|
} elseif (in_array($identifier, static::$defaultBackends)) {
|
|
|
|
Logger::warning(
|
2015-05-05 08:26:38 +02:00
|
|
|
'Cannot register user group backend of type "%s" provided by module "%s".'
|
2015-04-22 09:52:08 +02:00
|
|
|
. ' The type is a default type provided by Icinga Web 2',
|
|
|
|
$identifier,
|
|
|
|
$module->getName()
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$providedBy[$identifier] = $module->getName();
|
|
|
|
static::$customBackends[$identifier] = $className;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-05 08:26:38 +02:00
|
|
|
* Return the class for the given custom user group backend
|
2015-04-22 09:52:08 +02:00
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*
|
2015-05-05 08:26:38 +02:00
|
|
|
* @throws ConfigurationError In case the class associated to the given identifier does not exist
|
2015-04-22 09:52:08 +02:00
|
|
|
*/
|
|
|
|
protected static function getCustomUserGroupBackend($identifier)
|
|
|
|
{
|
2015-05-05 08:26:38 +02:00
|
|
|
static::registerCustomUserGroupBackends();
|
2015-04-22 09:52:08 +02:00
|
|
|
if (array_key_exists($identifier, static::$customBackends)) {
|
|
|
|
$className = static::$customBackends[$identifier];
|
|
|
|
if (! class_exists($className)) {
|
|
|
|
throw new ConfigurationError(
|
2015-05-05 08:26:38 +02:00
|
|
|
'Cannot utilize user group backend of type "%s". Class "%s" does not exist',
|
2015-04-22 09:52:08 +02:00
|
|
|
$identifier,
|
|
|
|
$className
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $className;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-20 13:41:33 +02:00
|
|
|
/**
|
2015-05-05 08:26:38 +02:00
|
|
|
* Create and return a user group backend 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-05-05 08:26:38 +02:00
|
|
|
* @return UserGroupBackendInterface
|
2015-04-22 09:35:06 +02:00
|
|
|
*
|
|
|
|
* @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);
|
2015-05-05 08:26:38 +02:00
|
|
|
if (! is_a($backend, 'Icinga\Authentication\UserGroup\UserGroupBackendInterface')) {
|
|
|
|
throw new ConfigurationError(
|
|
|
|
'Cannot utilize user group backend of type "%s".'
|
|
|
|
. ' Class "%s" does not implement UserGroupBackendInterface',
|
|
|
|
$backendType,
|
|
|
|
$customClass
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-04-22 09:52:08 +02:00
|
|
|
$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-06-03 16:27:50 +02:00
|
|
|
case 'ldap':
|
|
|
|
$backend = new LdapUserGroupBackend($resource);
|
|
|
|
$backend
|
|
|
|
->setGroupBaseDn($backendConfig->base_dn)
|
|
|
|
->setUserBaseDn($backendConfig->get('user_base_dn', $backend->getGroupBaseDn()))
|
|
|
|
->setGroupClass($backendConfig->get('group_class', 'group'))
|
|
|
|
->setUserClass($backendConfig->get('user_class', 'inetOrgPerson'))
|
|
|
|
->setGroupNameAttribute($backendConfig->get('group_name_attribute', 'gid'))
|
|
|
|
->setUserNameAttribute($backendConfig->get('user_name_attribute', 'uid'))
|
|
|
|
->setGroupMemberAttribute($backendConfig->get('group_member_attribute', 'member'));
|
|
|
|
break;
|
|
|
|
case 'msldap':
|
|
|
|
$backend = new LdapUserGroupBackend($resource);
|
|
|
|
$backend
|
|
|
|
->setGroupBaseDn($backendConfig->base_dn)
|
|
|
|
->setUserBaseDn($backendConfig->get('user_base_dn', $backend->getGroupBaseDn()))
|
|
|
|
->setGroupClass($backendConfig->get('group_class', 'group'))
|
|
|
|
->setUserClass($backendConfig->get('user_class', 'user'))
|
|
|
|
->setGroupNameAttribute($backendConfig->get('group_name_attribute', 'sAMAccountName'))
|
|
|
|
->setUserNameAttribute($backendConfig->get('user_name_attribute', $backend->getGroupNameAttribute()))
|
|
|
|
->setGroupMemberAttribute($backendConfig->get('group_member_attribute', 'member'));
|
|
|
|
break;
|
2014-10-20 13:41:33 +02:00
|
|
|
}
|
2015-04-22 09:35:06 +02:00
|
|
|
|
2014-10-20 13:41:33 +02:00
|
|
|
$backend->setName($name);
|
|
|
|
return $backend;
|
|
|
|
}
|
|
|
|
}
|