Merge branch 'bugifx/add-none-preferences-store-8234'

fixes #8234
This commit is contained in:
Johannes Meyer 2015-01-23 16:25:41 +01:00
commit 113658a23c
8 changed files with 30 additions and 38 deletions

View File

@ -7,7 +7,6 @@ use Icinga\Web\Url;
use Icinga\Web\Widget\Tab; use Icinga\Web\Widget\Tab;
use Icinga\Application\Config; use Icinga\Application\Config;
use Icinga\Forms\PreferenceForm; use Icinga\Forms\PreferenceForm;
use Icinga\Exception\ConfigurationError;
use Icinga\User\Preferences\PreferencesStore; use Icinga\User\Preferences\PreferencesStore;
/** /**
@ -40,14 +39,13 @@ class PreferenceController extends BasePreferenceController
public function indexAction() public function indexAction()
{ {
$storeConfig = Config::app()->getSection('preferences'); $storeConfig = Config::app()->getSection('preferences');
if ($storeConfig->isEmpty()) {
throw new ConfigurationError(t('You need to configure how to store preferences first.'));
}
$user = $this->getRequest()->getUser(); $user = $this->getRequest()->getUser();
$form = new PreferenceForm(); $form = new PreferenceForm();
$form->setPreferences($user->getPreferences()); $form->setPreferences($user->getPreferences());
$form->setStore(PreferencesStore::create($storeConfig, $user)); if ($storeConfig->get('store', 'ini') !== 'none') {
$form->setStore(PreferencesStore::create($storeConfig, $user));
}
$form->handleRequest(); $form->handleRequest();
$this->view->form = $form; $this->view->form = $form;

View File

@ -4,10 +4,8 @@
namespace Icinga\Forms\Config\General; namespace Icinga\Forms\Config\General;
use DateTimeZone;
use Icinga\Application\Icinga; use Icinga\Application\Icinga;
use Icinga\Data\ResourceFactory; use Icinga\Data\ResourceFactory;
use Icinga\Util\Translator;
use Icinga\Web\Form; use Icinga\Web\Form;
@ -46,7 +44,7 @@ class ApplicationConfigForm extends Form
$this->addElement( $this->addElement(
'select', 'select',
'preferences_type', 'preferences_store',
array( array(
'required' => true, 'required' => true,
'autosubmit' => true, 'autosubmit' => true,
@ -54,11 +52,11 @@ class ApplicationConfigForm extends Form
'multiOptions' => array( 'multiOptions' => array(
'ini' => $this->translate('File System (INI Files)'), 'ini' => $this->translate('File System (INI Files)'),
'db' => $this->translate('Database'), 'db' => $this->translate('Database'),
'null' => $this->translate('Don\'t Store Preferences') 'none' => $this->translate('Don\'t Store Preferences')
) )
) )
); );
if (isset($formData['preferences_type']) && $formData['preferences_type'] === 'db') { if (isset($formData['preferences_store']) && $formData['preferences_store'] === 'db') {
$backends = array(); $backends = array();
foreach (ResourceFactory::getResourceConfigs()->toArray() as $name => $resource) { foreach (ResourceFactory::getResourceConfigs()->toArray() as $name => $resource) {
if ($resource['type'] === 'db') { if ($resource['type'] === 'db') {

View File

@ -86,7 +86,7 @@ class PreferenceForm extends Form
*/ */
public function onSuccess() public function onSuccess()
{ {
$this->preferences = new Preferences($this->store->load()); $this->preferences = new Preferences($this->store ? $this->store->load() : array());
$webPreferences = $this->preferences->get('icingaweb', array()); $webPreferences = $this->preferences->get('icingaweb', array());
foreach ($this->getValues() as $key => $value) { foreach ($this->getValues() as $key => $value) {
@ -103,7 +103,7 @@ class PreferenceForm extends Form
Session::getSession()->user->setPreferences($this->preferences); Session::getSession()->user->setPreferences($this->preferences);
try { try {
if ($this->getElement('btn_submit_preferences')->isChecked()) { if ($this->store && $this->getElement('btn_submit_preferences')->isChecked()) {
$this->save(); $this->save();
Notification::success($this->translate('Preferences successfully saved')); Notification::success($this->translate('Preferences successfully saved'));
} else { } else {
@ -186,18 +186,20 @@ class PreferenceForm extends Form
) )
); );
$this->addElement( if ($this->store) {
'submit', $this->addElement(
'btn_submit_preferences', 'submit',
array( 'btn_submit_preferences',
'ignore' => true, array(
'label' => $this->translate('Save to the Preferences'), 'ignore' => true,
'decorators' => array( 'label' => $this->translate('Save to the Preferences'),
'ViewHelper', 'decorators' => array(
array('HtmlTag', array('tag' => 'div')) 'ViewHelper',
array('HtmlTag', array('tag' => 'div'))
)
) )
) );
); }
$this->addElement( $this->addElement(
'submit', 'submit',

View File

@ -63,7 +63,7 @@ class Manager
); );
$config = new Config(); $config = new Config();
} }
if ($config->hasSection('preferences')) { if ($config->get('preferences', 'store', 'ini') !== 'none') {
$preferencesConfig = $config->getSection('preferences'); $preferencesConfig = $config->getSection('preferences');
try { try {
$preferencesStore = PreferencesStore::create( $preferencesStore = PreferencesStore::create(

View File

@ -26,7 +26,7 @@ use Icinga\Data\Db\DbConnection;
* // Create a INI store * // Create a INI store
* $store = PreferencesStore::create( * $store = PreferencesStore::create(
* new ConfigObject( * new ConfigObject(
* 'type' => 'ini', * 'store' => 'ini',
* 'config_path' => '/path/to/preferences' * 'config_path' => '/path/to/preferences'
* ), * ),
* $user // Instance of \Icinga\User * $user // Instance of \Icinga\User
@ -117,13 +117,7 @@ abstract class PreferencesStore
*/ */
public static function create(ConfigObject $config, User $user) public static function create(ConfigObject $config, User $user)
{ {
if (($type = $config->type) === null) { $type = ucfirst(strtolower($config->get('store', 'ini')));
throw new ConfigurationError(
'Preferences configuration is missing the type directive'
);
}
$type = ucfirst(strtolower($type));
$storeClass = 'Icinga\\User\\Preferences\\Store\\' . $type . 'Store'; $storeClass = 'Icinga\\User\\Preferences\\Store\\' . $type . 'Store';
if (!class_exists($storeClass)) { if (!class_exists($storeClass)) {
throw new ConfigurationError( throw new ConfigurationError(

View File

@ -66,11 +66,11 @@ class PreferencesPage extends Form
if (Platform::hasMysqlSupport() || Platform::hasPostgresqlSupport()) { if (Platform::hasMysqlSupport() || Platform::hasPostgresqlSupport()) {
$storageTypes['db'] = $this->translate('Database'); $storageTypes['db'] = $this->translate('Database');
} }
$storageTypes['null'] = $this->translate('Don\'t Store Preferences'); $storageTypes['none'] = $this->translate('Don\'t Store Preferences');
$this->addElement( $this->addElement(
'select', 'select',
'type', 'store',
array( array(
'required' => true, 'required' => true,
'label' => $this->translate('User Preference Storage Type'), 'label' => $this->translate('User Preference Storage Type'),

View File

@ -29,7 +29,7 @@ class GeneralConfigStep extends Step
$config[$section][$property] = $value; $config[$section][$property] = $value;
} }
$config['preferences']['type'] = $this->data['preferencesType']; $config['preferences']['store'] = $this->data['preferencesStore'];
if (isset($this->data['preferencesResource'])) { if (isset($this->data['preferencesResource'])) {
$config['preferences']['resource'] = $this->data['preferencesResource']; $config['preferences']['resource'] = $this->data['preferencesResource'];
} }
@ -58,11 +58,11 @@ class GeneralConfigStep extends Step
$generalHtml = '' $generalHtml = ''
. '<ul>' . '<ul>'
. '<li>' . sprintf( . '<li>' . sprintf(
$this->data['preferencesType'] === 'ini' ? sprintf( $this->data['preferencesStore'] === 'ini' ? sprintf(
t('Preferences will be stored per user account in INI files at: %s'), t('Preferences will be stored per user account in INI files at: %s'),
Config::resolvePath('preferences') Config::resolvePath('preferences')
) : ( ) : (
$this->data['preferencesType'] === 'db' ? t('Preferences will be stored using a database.') : ( $this->data['preferencesStore'] === 'db' ? t('Preferences will be stored using a database.') : (
t('Preferences will not be persisted across browser sessions.') t('Preferences will not be persisted across browser sessions.')
) )
) )

View File

@ -293,7 +293,7 @@ class WebWizard extends Wizard implements SetupWizard
$setup->addStep( $setup->addStep(
new GeneralConfigStep(array( new GeneralConfigStep(array(
'generalConfig' => $pageData['setup_general_config'], 'generalConfig' => $pageData['setup_general_config'],
'preferencesType' => $pageData['setup_preferences_type']['type'], 'preferencesStore' => $pageData['setup_preferences_type']['store'],
'preferencesResource' => isset($pageData['setup_db_resource']['name']) 'preferencesResource' => isset($pageData['setup_db_resource']['name'])
? $pageData['setup_db_resource']['name'] ? $pageData['setup_db_resource']['name']
: null : null