From e79784e0f1274fa76c395e9a84357f7ca3dd6ad6 Mon Sep 17 00:00:00 2001 From: diosmosis <benaka@piwik.pro> Date: Wed, 10 Sep 2014 02:33:37 -0700 Subject: [PATCH] Fixes #5923, add INI config option allow_adding_segments_for_all_websites that allows users to disable the ability to add segments affecting all websites. --- config/global.ini.php | 5 +++++ plugins/SegmentEditor/API.php | 8 +++++++- plugins/SegmentEditor/SegmentEditor.php | 11 +++++++++++ plugins/SegmentEditor/SegmentSelectorControl.php | 2 ++ plugins/SegmentEditor/lang/en.json | 3 ++- plugins/SegmentEditor/templates/_segmentSelector.twig | 2 +- 6 files changed, 28 insertions(+), 3 deletions(-) diff --git a/config/global.ini.php b/config/global.ini.php index ff987583d2..d72d5e6150 100644 --- a/config/global.ini.php +++ b/config/global.ini.php @@ -177,6 +177,11 @@ enable_segment_suggested_values = 1 ; Possible values are "view", "admin", "superuser" adding_segment_requires_access = "view" +; Whether it is allowed for users to add segments that affect all websites or not. If there are many websites +; this admin option can be used to prevent users from performing an action that will have a major impact +; on Piwik performance. +allow_adding_segments_for_all_websites = 1 + ; this action name is used when the URL ends with a slash / ; it is useful to have an actual string to write in the UI action_default_name = index diff --git a/plugins/SegmentEditor/API.php b/plugins/SegmentEditor/API.php index 301fe44a50..98b752cc2a 100644 --- a/plugins/SegmentEditor/API.php +++ b/plugins/SegmentEditor/API.php @@ -115,7 +115,13 @@ class API extends \Piwik\Plugin\API protected function checkUserCanAddNewSegment($idSite) { - if(!$this->isUserCanAddNewSegment($idSite)) { + if (empty($idSite) + && !SegmentEditor::isAddingSegmentsForAllWebsitesEnabled() + ) { + throw new Exception(Piwik::translate('SegmentEditor_AddingSegmentForAllWebsitesDisabled')); + } + + if (!$this->isUserCanAddNewSegment($idSite)) { throw new Exception(Piwik::translate('SegmentEditor_YouDontHaveAccessToCreateSegments')); } } diff --git a/plugins/SegmentEditor/SegmentEditor.php b/plugins/SegmentEditor/SegmentEditor.php index a79cc0be24..bd957c54c0 100644 --- a/plugins/SegmentEditor/SegmentEditor.php +++ b/plugins/SegmentEditor/SegmentEditor.php @@ -8,6 +8,7 @@ */ namespace Piwik\Plugins\SegmentEditor; +use Piwik\Config; use Piwik\Db; use Piwik\DbHelper; use Piwik\Version; @@ -98,4 +99,14 @@ class SegmentEditor extends \Piwik\Plugin { $stylesheets[] = "plugins/SegmentEditor/stylesheets/segmentation.less"; } + + /** + * Returns whether adding segments for all websites is enabled or not. + * + * @return bool + */ + public static function isAddingSegmentsForAllWebsitesEnabled() + { + return Config::getInstance()->General['allow_adding_segments_for_all_websites'] == 1; + } } diff --git a/plugins/SegmentEditor/SegmentSelectorControl.php b/plugins/SegmentEditor/SegmentSelectorControl.php index bb58caa297..4e88d2191f 100644 --- a/plugins/SegmentEditor/SegmentSelectorControl.php +++ b/plugins/SegmentEditor/SegmentSelectorControl.php @@ -37,6 +37,8 @@ class SegmentSelectorControl extends UIControl $this->selectedSegment = Common::getRequestVar('segment', false, 'string'); + $this->isAddingSegmentsForAllWebsitesEnabled = SegmentEditor::isAddingSegmentsForAllWebsitesEnabled(); + $segments = APIMetadata::getInstance()->getSegmentsMetadata($this->idSite); $segmentsByCategory = array(); diff --git a/plugins/SegmentEditor/lang/en.json b/plugins/SegmentEditor/lang/en.json index 999b0d41bb..a3f03751d8 100644 --- a/plugins/SegmentEditor/lang/en.json +++ b/plugins/SegmentEditor/lang/en.json @@ -24,6 +24,7 @@ "VisibleToMe": "me", "YouMayChangeSetting": "Alternatively you may change the setting in the config file (%s), or edit this Segment and choose '%s'.", "YouMustBeLoggedInToCreateSegments": "You must be logged in to create and edit custom visitor segments.", - "YouDontHaveAccessToCreateSegments": "You don't have the required access level to create and edit segments." + "YouDontHaveAccessToCreateSegments": "You don't have the required access level to create and edit segments.", + "AddingSegmentForAllWebsitesDisabled": "Adding segments for all websites has been disabled." } } \ No newline at end of file diff --git a/plugins/SegmentEditor/templates/_segmentSelector.twig b/plugins/SegmentEditor/templates/_segmentSelector.twig index 89c8a314bf..1d1b2275d8 100644 --- a/plugins/SegmentEditor/templates/_segmentSelector.twig +++ b/plugins/SegmentEditor/templates/_segmentSelector.twig @@ -122,7 +122,7 @@ {{ 'SegmentEditor_SegmentIsDisplayedForWebsite'|translate }}<span class="visible_to_website"><strong> <select class="visible_to_website_select"> <option selected="" value="{{ idSite }}">{{ 'SegmentEditor_SegmentDisplayedThisWebsiteOnly'|translate }}</option> - <option value="0">{{ 'SegmentEditor_SegmentDisplayedAllWebsites'|translate }}</option> + {% if isAddingSegmentsForAllWebsitesEnabled %}<option value="0">{{ 'SegmentEditor_SegmentDisplayedAllWebsites'|translate }}</option>{% endif %} </select> </strong></span> {{ 'General_And'|translate }} <span class="auto_archive"><strong> -- GitLab