Move configuration and preference handling to Form base class

Moved setConfiguration, setUserPreferences and getUserPreferences
to our Form base class due to some redundancies.

refs #4581
This commit is contained in:
Johannes Meyer 2013-08-27 14:27:31 +02:00
parent 30e36f1e09
commit 1a003f8c8b
8 changed files with 112 additions and 149 deletions

View File

@ -46,13 +46,6 @@ use \Icinga\Web\Form\Decorator\ConditionalHidden;
*/
class GeneralForm extends Form
{
/**
* The configuration to use for populating this form
*
* @var IcingaConfig
*/
private $config = null;
/**
* The base directory of the icingaweb configuration
*
@ -60,7 +53,6 @@ class GeneralForm extends Form
*/
private $configDir = null;
/**
* The resources to use instead of the factory provided ones (use for testing)
*
@ -75,16 +67,6 @@ class GeneralForm extends Form
*/
private $dateHelper;
/**
* Set the configuration to be used for this form
*
* @param IcingaConfig $cfg
*/
public function setConfiguration($cfg)
{
$this->config = $cfg;
}
/**
* Set a specific configuration directory to use for configuration specific default paths
*
@ -349,15 +331,12 @@ class GeneralForm extends Form
*/
public function create()
{
if ($this->config === null) {
$this->config = new Zend_Config(array());
}
$global = $this->config->global;
$config = $this->getConfiguration();
$global = $config->global;
if ($global === null) {
$global = new Zend_Config(array());
}
$preferences = $this->config->preferences;
$preferences = $config->preferences;
if ($preferences === null) {
$preferences = new Zend_Config(array());
}
@ -378,18 +357,16 @@ class GeneralForm extends Form
*/
public function getConfig()
{
if ($this->config === null) {
$this->config = new Zend_Config(array());
$config = $this->getConfiguration();
if ($config->global === null) {
$config->global = new Zend_Config(array());
}
if ($this->config->global === null) {
$this->config->global = new Zend_Config(array());
}
if ($this->config->preferences === null) {
$this->config->preferences = new Zend_Config(array());
if ($config->preferences === null) {
$config->preferences = new Zend_Config(array());
}
$values = $this->getValues();
$cfg = clone $this->config;
$cfg = clone $config;
$cfg->global->environment = ($values['environment'] == 1) ? 'development' : 'production';
$cfg->global->timezone = $values['timezone'];
$cfg->global->moduleFolder = $values['module_folder'];

View File

@ -41,13 +41,6 @@ use \Icinga\Web\Form\Decorator\ConditionalHidden;
*/
class LoggingForm extends Form
{
/**
* The configuration to use for this form
*
* @var Zend_Config
*/
private $config = null;
/**
* Base directory to use instead of the one provided by Icinga::app (used for testing)
*
@ -55,18 +48,6 @@ class LoggingForm extends Form
*/
private $baseDir = null;
/**
* Set the configuration of this form
*
* If not called, default values are used instead
*
* @param Zend_Config $cfg The config.ini to set with this form
*/
public function setConfiguration(Zend_Config $cfg)
{
$this->config = $cfg;
}
/**
* Set a different base directory to use for default paths instead of the one provided by Icinga::app()
*
@ -123,15 +104,13 @@ class LoggingForm extends Form
public function create()
{
$this->setName('form_config_logging');
if ($this->config === null) {
$this->config = new Zend_Config(array());
}
$logging = $this->config->logging;
$config = $this->getConfiguration();
$logging = $config->logging;
if ($logging === null) {
$logging = new IcingaConfig(array());
}
$debug = $logging->debug;
$debug = $config->logging->debug;
if ($debug === null) {
$debug = new IcingaConfig(array());
}
@ -177,7 +156,10 @@ class LoggingForm extends Form
'label' => 'Debug Log Path',
'required' => $this->shouldDisplayDebugLog($debug),
'condition' => $this->shouldDisplayDebugLog($debug),
'value' => $debug->get('target', $this->getBaseDir() . '/var/log/icinga2.debug.log'),
'value' => $debug->get(
'target',
$this->getBaseDir() . '/var/log/icinga2.debug.log'
),
'helptext' => 'Set the path to the debug log'
)
);
@ -200,19 +182,16 @@ class LoggingForm extends Form
*/
public function getConfig()
{
if ($this->config === null) {
$this->config = new Zend_Config(array());
$config = $this->getConfiguration();
if ($config->logging === null) {
$config->logging = new IcingaConfig(array());
}
if ($this->config->logging === null) {
$this->config->logging = new Zend_Config(array());
}
if ($this->config->logging->debug === null) {
$this->config->logging->debug = new Zend_Config(array());
if ($config->logging->debug === null) {
$config->logging->debug = new IcingaConfig(array());
}
$values = $this->getValues();
$cfg = $this->config->toArray();
$cfg = $config->toArray();
$cfg['logging']['enable'] = 1;
$cfg['logging']['type'] = 'stream';

View File

@ -33,9 +33,6 @@ use \Zend_Config;
use \Zend_Form_Element_Text;
use \Zend_Form_Element_Select;
use \Zend_View_Helper_DateFormat;
use \Icinga\Application\Config as IcingaConfig;
use \Icinga\Application\Icinga;
use \Icinga\User\Preferences;
use \Icinga\Web\Form;
use \Icinga\Web\Form\Validator\TimeFormatValidator;
use \Icinga\Web\Form\Validator\DateFormatValidator;
@ -45,20 +42,6 @@ use \Icinga\Web\Form\Validator\DateFormatValidator;
*/
class GeneralForm extends Form
{
/**
* The configuration to use for populating this form
*
* @var IcingaConfig
*/
private $config;
/**
* The preference object to use instead of the one from the user (used for testing)
*
* @var Zend_Config
*/
private $preferences;
/**
* The view helper to format date/time strings
*
@ -66,39 +49,6 @@ class GeneralForm extends Form
*/
private $dateHelper;
/**
* Set the configuration to be used for this form when no preferences are set yet
*
* @param IcingaConfig $cfg
*/
public function setConfiguration($cfg)
{
$this->config = $cfg;
}
/**
* Set preferences to be used instead of the one from the user object (used for testing)
*
* @param Zend_Config $prefs
*/
public function setUserPreferences($prefs)
{
$this->preferences = $prefs;
}
/**
* Return the preferences of the user or the overwritten ones
*
* @return Zend_Config
*/
public function getUserPreferences()
{
if ($this->preferences) {
return $this->preferences;
}
return $this->getRequest()->getUser()->getPreferences();
}
/**
* Return the view helper to format date/time strings
*
@ -252,11 +202,10 @@ class GeneralForm extends Form
*/
public function create()
{
if ($this->config === null) {
$this->config = new Zend_Config(array());
}
$this->setName('form_preference_set');
$global = $this->config->global;
$config = $this->getConfiguration();
$global = $config->global;
if ($global === null) {
$global = new Zend_Config(array());
}

View File

@ -27,13 +27,15 @@ namespace Icinga\Web;
use \Zend_Controller_Request_Abstract;
use \Zend_Form;
use \Zend_Config;
use \Zend_Form_Element_Submit;
use \Zend_Form_Element_Reset;
use \Zend_View_Interface;
use \Icinga\Web\Form\Element\Note;
use \Icinga\Exception\ProgrammingError;
use \Icinga\Web\Form\Decorator\HelpText;
use \Icinga\Web\Form\InvalidCSRFTokenException;
use \Icinga\Web\Form\Element\Note;
use \Icinga\Application\Config as IcingaConfig;
/**
* Base class for forms providing CSRF protection, confirmation logic and auto submission
@ -47,6 +49,22 @@ class Form extends Zend_Form
*/
private $request;
/**
* Main configuration
*
* Used as fallback if user preferences are not available.
*
* @var IcingaConfig
*/
private $config;
/**
* The preference object to use instead of the one from the user (used for testing)
*
* @var Zend_Config
*/
private $preferences;
/**
* Whether this form should NOT add random generated "challenge" tokens that are associated with the user's current
* session in order to prevent Cross-Site Request Forgery (CSRF). It is the form's responsibility to verify the
@ -192,6 +210,54 @@ class Form extends Zend_Form
return $this->request;
}
/**
* Set the configuration to be used for this form when no preferences are set yet
*
* @param IcingaConfig $cfg
*/
public function setConfiguration($cfg)
{
$this->config = $cfg;
}
/**
* Get the main configuration
*
* Returns the set configuration or an empty default one.
*
* @return Zend_Config
*/
public function getConfiguration()
{
if ($this->config === null) {
$this->config = new Zend_Config(array());
}
return $this->config;
}
/**
* Set preferences to be used instead of the one from the user object (used for testing)
*
* @param Zend_Config $prefs
*/
public function setUserPreferences($prefs)
{
$this->preferences = $prefs;
}
/**
* Return the preferences of the user or the overwritten ones
*
* @return Zend_Config
*/
public function getUserPreferences()
{
if ($this->preferences) {
return $this->preferences;
}
return $this->getRequest()->getUser()->getPreferences();
}
/**
* Create the form if not done already
*

View File

@ -49,10 +49,9 @@ require_once BaseTestCase::$appDir . '/forms/Config/Authentication/DbBackendForm
require_once BaseTestCase::$appDir . '/forms/Config/Authentication/LdapBackendForm.php';
// @codingStandardsIgnoreEnd
use \Icinga\Web\Form;
use Icinga\Web\Url;
use Tests\Icinga\Web\RequestMock;
use \Zend_Config;
use \Icinga\Web\Url;
use \Tests\Icinga\Web\RequestMock;
/**
* Test for the authentication provider form
@ -60,7 +59,6 @@ use \Zend_Config;
*/
class AuthenticationFormTest extends BaseTestCase
{
/**
* Return a test configuration containing a database and a ldap backend
*
@ -91,7 +89,6 @@ class AuthenticationFormTest extends BaseTestCase
/**
* Test the ldap provider form population from config
*
*/
public function testLdapProvider()
{
@ -126,7 +123,7 @@ class AuthenticationFormTest extends BaseTestCase
);
}
}
/*
/**
* Test the database provider form population from config
*/

View File

@ -46,7 +46,6 @@ require_once BaseTestCase::$libDir . '/Util/DateTimeFactory.php';
// @codingStandardsIgnoreEnd
use \DateTimeZone;
use \Icinga\Web\Form;
use \DOMDocument;
use \Zend_Config;
use \Zend_View;

View File

@ -41,7 +41,6 @@ require_once BaseTestCase::$libDir . '/Web/Form.php';
require_once BaseTestCase::$appDir . '/forms/Config/GeneralForm.php';
// @codingStandardsIgnoreEnd
use \Icinga\Web\Form;
use \Zend_Config;
/**
@ -50,7 +49,6 @@ use \Zend_Config;
*/
class LoggingFormTest extends BaseTestCase
{
/**
* Test the logging form to be correctly populated from configuration
*

View File

@ -49,7 +49,6 @@ require_once BaseTestCase::$libDir . '/Util/DateTimeFactory.php';
// @codingStandardsIgnoreEnd
use \DateTimeZone;
use \Icinga\Web\Form;
use \Zend_Config;
use \Icinga\User\Preferences;
use \Zend_View_Helper_DateFormat;
@ -60,7 +59,6 @@ use \Icinga\Util\DateTimeFactory;
*/
class GeneralFormTest extends BaseTestCase
{
/**
* Test whether fields using the default values have input disabled
*