diff options
author | Thomas Steur <thomas.steur@gmail.com> | 2013-10-24 01:14:58 +0400 |
---|---|---|
committer | Thomas Steur <thomas.steur@gmail.com> | 2013-10-24 01:14:58 +0400 |
commit | 0abe2b8e7de4ada6d1d8c9e693e6c17dbe986bbc (patch) | |
tree | 4c4dc96dc84a35b7e060b07eba70a1f4e7d2be86 /core/Settings | |
parent | c9702361428ba308d4ac6158c3b097590c1a28a0 (diff) |
refs #4126 refactored plugin settings to work with User and System setting data objects, the plugin settings does not know anything abotu User/System settings so it is possible to add different kind of settings in the future. Also added nonce when saving settings
Diffstat (limited to 'core/Settings')
-rw-r--r-- | core/Settings/Manager.php | 2 | ||||
-rw-r--r-- | core/Settings/Setting.php | 110 | ||||
-rw-r--r-- | core/Settings/SystemSetting.php | 31 | ||||
-rw-r--r-- | core/Settings/UserSetting.php | 88 |
4 files changed, 230 insertions, 1 deletions
diff --git a/core/Settings/Manager.php b/core/Settings/Manager.php index 9dc1cc7a8d..3e5c77c237 100644 --- a/core/Settings/Manager.php +++ b/core/Settings/Manager.php @@ -15,7 +15,7 @@ namespace Piwik\Settings; * Settings manager * * @package Piwik - * @subpackage Manager + * @subpackage Settings */ class Manager { diff --git a/core/Settings/Setting.php b/core/Settings/Setting.php new file mode 100644 index 0000000000..4cc443b532 --- /dev/null +++ b/core/Settings/Setting.php @@ -0,0 +1,110 @@ +<?php +/** + * Piwik - Open source web analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + * @category Piwik + * @package Piwik + */ + +namespace Piwik\Settings; +use Piwik\Common; +use Piwik\Piwik; +use Piwik\Plugin\Settings; + +/** + * Settings manager + * + * @package Piwik + * @subpackage Settings + */ +abstract class Setting +{ + public $type = Settings::TYPE_STRING; + public $field = Settings::FIELD_TEXT; + public $fieldAttributes = array(); + public $fieldOptions = null; + public $introduction = null; + public $description = null; + public $inlineHelp = null; + public $filter = null; + public $validate = null; + public $defaultValue = null; + public $title = ''; + + protected $key; + protected $name; + protected $displayedForCurrentUser = false; + + public function canBeDisplayedForCurrentUser() + { + return $this->displayedForCurrentUser; + } + + public function __construct($name, $title) + { + if (!ctype_alnum($name)) { + // TODO escape name? + $msg = sprintf('The setting name %s is not valid. Only alpha and numerical characters are allowed', $name); + throw new \Exception($msg); + } + + $this->key = $name; + $this->name = $name; + $this->title = $title; + } + + public function getName() + { + return $this->name; + } + + public function getTitle() + { + return $this->title; + } + + /** + * Returns the key under which property name the setting will be stored. + * + * @return string + */ + public function getKey() + { + return $this->key; + } + + public function hasKey($key) + { + return $key == $this->getKey(); + } + + public function getDefaultType($field) + { + $defaultTypes = array( + Settings::FIELD_TEXT => Settings::TYPE_STRING, + Settings::FIELD_TEXTAREA => Settings::TYPE_STRING, + Settings::FIELD_PASSWORD => Settings::TYPE_STRING, + Settings::FIELD_CHECKBOX => Settings::TYPE_BOOL, + Settings::FIELD_MULTI_SELECT => Settings::TYPE_ARRAY, + Settings::FIELD_SINGLE_SELECT => Settings::TYPE_STRING, + ); + + return $defaultTypes[$field]; + } + + public function getDefaultField($type) + { + $defaultFields = array( + Settings::TYPE_INT => Settings::FIELD_TEXT, + Settings::TYPE_FLOAT => Settings::FIELD_TEXT, + Settings::TYPE_STRING => Settings::FIELD_TEXT, + Settings::TYPE_BOOL => Settings::FIELD_CHECKBOX, + Settings::TYPE_ARRAY => Settings::FIELD_MULTI_SELECT, + ); + + return $defaultFields[$type]; + } +} diff --git a/core/Settings/SystemSetting.php b/core/Settings/SystemSetting.php new file mode 100644 index 0000000000..f9ce5614fb --- /dev/null +++ b/core/Settings/SystemSetting.php @@ -0,0 +1,31 @@ +<?php +/** + * Piwik - Open source web analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + * @category Piwik + * @package Piwik + */ + +namespace Piwik\Settings; + +use Piwik\Piwik; + +/** + * System Setting. + * + * @package Piwik + * @subpackage Settings + */ +class SystemSetting extends Setting +{ + public function __construct($name, $title) + { + parent::__construct($name, $title); + + $this->displayedForCurrentUser = !Piwik::isUserIsAnonymous(); + } + +} diff --git a/core/Settings/UserSetting.php b/core/Settings/UserSetting.php new file mode 100644 index 0000000000..7ff3aaf399 --- /dev/null +++ b/core/Settings/UserSetting.php @@ -0,0 +1,88 @@ +<?php +/** + * Piwik - Open source web analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + * @category Piwik + * @package Piwik + */ + +namespace Piwik\Settings; +use Piwik\Common; +use Piwik\Piwik; + +/** + * Per User Setting. + * + * @package Piwik + * @subpackage Settings + */ +class UserSetting extends Setting +{ + private $userLogin = null; + + public function __construct($name, $title, $userLogin = null) + { + parent::__construct($name, $title); + + $this->setUserLogin($userLogin); + + $this->displayedForCurrentUser = !Piwik::isUserIsAnonymous(); + } + + public function hasKey($key, $userLogin = null) + { + $thisKey = $this->buildUserSettingName($key, $userLogin); + + return ($key == $thisKey); + } + + private function buildUserSettingName($name, $userLogin = null) + { + if (empty($userLogin)) { + $userLogin = Piwik::getCurrentUserLogin(); + } + + // the asterisk tag is indeed important here and better than an underscore. Imagine a plugin has the settings + // "api_password" and "api". A user having the login "_password" could otherwise under circumstances change the + // setting for "api" although he is not allowed to. It is not so important at the moment because only alNum is + // currently allowed as a name this might change in the future. + $appendix = '#' . $userLogin . '#'; + + if (Common::stringEndsWith($name, $appendix)) { + return $name; + } + + return $name . $appendix; + } + + public function setUserLogin($userLogin) + { + $this->userLogin = $userLogin; + $this->key = $this->buildUserSettingName($this->name, $userLogin); + } + + public static function removeAllUserSettingsForUser($userLogin) + { + $pluginsSettings = Manager::getAllPluginSettings(); + + foreach ($pluginsSettings as $pluginSettings) { + + $settings = $pluginSettings->getSettings(); + + foreach ($settings as $setting) { + + if ($setting instanceof UserSetting) { + $setting->setUserLogin($userLogin); + $pluginSettings->removeValue($setting); + } + + } + + $pluginSettings->save(); + } + } + +} |