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
|
|
|
|
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: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: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: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;
|
|
|
|
default:
|
|
|
|
throw new ConfigurationError(
|
2015-04-22 09:35:06 +02:00
|
|
|
'Configuration for user group backend "%s" defines an invalid backend type.'
|
|
|
|
. ' Backend type "%s" is not supported',
|
2014-10-20 13:41:33 +02:00
|
|
|
$name,
|
|
|
|
$backendType
|
|
|
|
);
|
|
|
|
}
|
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);
|
|
|
|
}
|