2014-02-14 17:28:11 +01:00
|
|
|
<?php
|
2016-02-08 15:41:00 +01:00
|
|
|
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
|
2014-02-14 17:28:11 +01:00
|
|
|
|
|
|
|
namespace Icinga\User\Preferences;
|
|
|
|
|
|
|
|
use Icinga\User;
|
|
|
|
use Icinga\User\Preferences;
|
2014-11-18 13:11:52 +01:00
|
|
|
use Icinga\Data\ConfigObject;
|
2014-04-08 13:28:45 +02:00
|
|
|
use Icinga\Data\ResourceFactory;
|
2014-04-07 15:13:28 +02:00
|
|
|
use Icinga\Exception\ConfigurationError;
|
2022-04-29 15:11:48 +02:00
|
|
|
use Icinga\User\Preferences\Store\DbStore;
|
2014-02-14 17:28:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Preferences store factory
|
|
|
|
*
|
|
|
|
* Usage example:
|
|
|
|
* <code>
|
|
|
|
* <?php
|
|
|
|
*
|
2014-11-18 13:11:52 +01:00
|
|
|
* use Icinga\Data\ConfigObject;
|
2014-02-14 17:28:11 +01:00
|
|
|
* use Icinga\User\Preferences;
|
|
|
|
* use Icinga\User\Preferences\PreferencesStore;
|
|
|
|
*
|
|
|
|
* // Create a INI store
|
2014-04-07 15:13:28 +02:00
|
|
|
* $store = PreferencesStore::create(
|
2014-11-18 13:11:52 +01:00
|
|
|
* new ConfigObject(
|
2014-06-02 12:09:16 +02:00
|
|
|
* 'config_path' => '/path/to/preferences'
|
2014-02-14 17:28:11 +01:00
|
|
|
* ),
|
2014-02-17 14:11:55 +01:00
|
|
|
* $user // Instance of \Icinga\User
|
2014-02-14 17:28:11 +01:00
|
|
|
* );
|
|
|
|
*
|
|
|
|
* $preferences = new Preferences($store->load());
|
2014-02-17 14:11:55 +01:00
|
|
|
* $preferences->aPreference = 'value';
|
2014-02-14 17:28:11 +01:00
|
|
|
* $store->save($preferences);
|
2014-04-07 15:13:28 +02:00
|
|
|
* </code>
|
2014-02-14 17:28:11 +01:00
|
|
|
*/
|
|
|
|
abstract class PreferencesStore
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Store config
|
|
|
|
*
|
2014-11-18 13:11:52 +01:00
|
|
|
* @var ConfigObject
|
2014-02-14 17:28:11 +01:00
|
|
|
*/
|
2014-04-07 15:13:28 +02:00
|
|
|
protected $config;
|
2014-02-14 17:28:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given user
|
|
|
|
*
|
|
|
|
* @var User
|
|
|
|
*/
|
2014-04-07 15:13:28 +02:00
|
|
|
protected $user;
|
2014-02-14 17:28:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new store
|
|
|
|
*
|
2014-11-18 13:11:52 +01:00
|
|
|
* @param ConfigObject $config The config for this adapter
|
|
|
|
* @param User $user The user to which these preferences belong
|
2014-02-14 17:28:11 +01:00
|
|
|
*/
|
2014-11-18 13:11:52 +01:00
|
|
|
public function __construct(ConfigObject $config, User $user)
|
2014-02-14 17:28:11 +01:00
|
|
|
{
|
|
|
|
$this->config = $config;
|
|
|
|
$this->user = $user;
|
|
|
|
$this->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter for the store config
|
|
|
|
*
|
2014-11-18 13:11:52 +01:00
|
|
|
* @return ConfigObject
|
2014-02-14 17:28:11 +01:00
|
|
|
*/
|
2014-04-07 15:13:28 +02:00
|
|
|
public function getStoreConfig()
|
2014-02-14 17:28:11 +01:00
|
|
|
{
|
|
|
|
return $this->config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter for the user
|
|
|
|
*
|
2014-04-07 15:13:28 +02:00
|
|
|
* @return User
|
2014-02-14 17:28:11 +01:00
|
|
|
*/
|
2014-04-07 15:13:28 +02:00
|
|
|
public function getUser()
|
2014-02-14 17:28:11 +01:00
|
|
|
{
|
|
|
|
return $this->user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-07 15:13:28 +02:00
|
|
|
* Initialize the store
|
2014-02-14 17:28:11 +01:00
|
|
|
*/
|
2014-04-07 15:13:28 +02:00
|
|
|
abstract protected function init();
|
2014-02-14 17:28:11 +01:00
|
|
|
|
|
|
|
/**
|
2014-04-07 15:13:28 +02:00
|
|
|
* Load preferences from source
|
2014-02-14 17:28:11 +01:00
|
|
|
*
|
2014-04-07 15:13:28 +02:00
|
|
|
* @return array
|
2014-02-14 17:28:11 +01:00
|
|
|
*/
|
2014-04-07 15:13:28 +02:00
|
|
|
abstract public function load();
|
2014-02-14 17:28:11 +01:00
|
|
|
|
|
|
|
/**
|
2014-04-07 15:13:28 +02:00
|
|
|
* Save the given preferences
|
2014-02-14 17:28:11 +01:00
|
|
|
*
|
2014-04-07 15:13:28 +02:00
|
|
|
* @param Preferences $preferences The preferences to save
|
2014-02-14 17:28:11 +01:00
|
|
|
*/
|
2014-04-07 15:13:28 +02:00
|
|
|
abstract public function save(Preferences $preferences);
|
2014-02-14 17:28:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create preferences storage adapter from config
|
|
|
|
*
|
2014-11-18 13:11:52 +01:00
|
|
|
* @param ConfigObject $config The config for the adapter
|
|
|
|
* @param User $user The user to which these preferences belong
|
2014-02-14 17:28:11 +01:00
|
|
|
*
|
2014-04-07 15:13:28 +02:00
|
|
|
* @return self
|
|
|
|
*
|
|
|
|
* @throws ConfigurationError When the configuration defines an invalid storage type
|
2014-02-14 17:28:11 +01:00
|
|
|
*/
|
2014-11-18 13:11:52 +01:00
|
|
|
public static function create(ConfigObject $config, User $user)
|
2014-02-14 17:28:11 +01:00
|
|
|
{
|
2022-04-29 15:11:48 +02:00
|
|
|
$resourceConfig = ResourceFactory::getResourceConfig($config->resource);
|
|
|
|
if ($resourceConfig->db === 'mysql') {
|
|
|
|
$resourceConfig->charset = 'utf8mb4';
|
2014-02-14 17:28:11 +01:00
|
|
|
}
|
2014-04-07 15:13:28 +02:00
|
|
|
|
2022-04-29 15:11:48 +02:00
|
|
|
$config->connection = ResourceFactory::createResource($resourceConfig);
|
2021-07-26 16:36:48 +02:00
|
|
|
|
2022-04-29 15:11:48 +02:00
|
|
|
return new DbStore($config, $user);
|
2014-02-14 17:28:11 +01:00
|
|
|
}
|
|
|
|
}
|