diff options
author | Thomas Steur <tsteur@users.noreply.github.com> | 2020-10-01 03:18:11 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-01 03:18:11 +0300 |
commit | fd14a155269038f6fe12ce31db20f8ebffaf00c5 (patch) | |
tree | 9afb319c7d8907fd5797d4cd6d9d6429c1a1c31b /plugins/SegmentEditor | |
parent | a79fccc1c7321ba0c3a2f3ceb9f5201be98e9476 (diff) |
Add check for real time segments enabled/disabled in API (#16465)
Diffstat (limited to 'plugins/SegmentEditor')
-rw-r--r-- | plugins/SegmentEditor/API.php | 42 | ||||
-rw-r--r-- | plugins/SegmentEditor/tests/System/UnprocessedSegmentsTest.php | 23 |
2 files changed, 44 insertions, 21 deletions
diff --git a/plugins/SegmentEditor/API.php b/plugins/SegmentEditor/API.php index 961ae10821..f2c85c19bc 100644 --- a/plugins/SegmentEditor/API.php +++ b/plugins/SegmentEditor/API.php @@ -87,36 +87,36 @@ class API extends \Piwik\Plugin\API protected function checkAutoArchive($autoArchive, $idSite) { $autoArchive = (int)$autoArchive; - if (!$autoArchive) { - return $autoArchive; - } - - $exception = new Exception( - "Please contact Support to make these changes on your behalf. ". - " To modify a pre-processed segment, a user must have admin access or super user access. " - ); // Segment 'All websites' and pre-processed requires Super User - if (empty($idSite)) { + if (empty($idSite) && $autoArchive) { if (!Piwik::hasUserSuperUserAccess()) { - throw $exception; + throw new Exception( + "Please contact Support to make these changes on your behalf. ". + " To modify a pre-processed segment for all websites, a user must have super user access. " + ); } - return $autoArchive; } // if real-time segments are disabled, then allow user to create pre-processed report - $realTimeSegmentsDisabled = !Config::getInstance()->General['enable_create_realtime_segments']; - if($realTimeSegmentsDisabled) { - // User is at least view - if(!Piwik::isUserHasViewAccess($idSite)) { - throw $exception; - } - return $autoArchive; + $realTimeSegmentsEnabled = Config::getInstance()->General['enable_create_realtime_segments']; + if (!$realTimeSegmentsEnabled && !$autoArchive) { + throw new Exception( + "Real time segments are disabled. You need to enable auto archiving." + ); } - // pre-processed segment for a given website requires admin access - if(!Piwik::isUserHasAdminAccess($idSite)) { - throw $exception; + if ($autoArchive) { + if ($realTimeSegmentsEnabled && !Piwik::isUserHasAdminAccess($idSite)) { + // pre-processed segment for a given website requires admin access + throw new Exception( + "Please contact Support to make these changes on your behalf. ". + " To modify a pre-processed segment, a user must have admin access or super user access. " + ); + } elseif (!$realTimeSegmentsEnabled) { + // we require view access only when only pre processed can be created + Piwik::checkUserHasViewAccess($idSite); + } } return $autoArchive; diff --git a/plugins/SegmentEditor/tests/System/UnprocessedSegmentsTest.php b/plugins/SegmentEditor/tests/System/UnprocessedSegmentsTest.php index 81a1a77d53..ebb5656e5f 100644 --- a/plugins/SegmentEditor/tests/System/UnprocessedSegmentsTest.php +++ b/plugins/SegmentEditor/tests/System/UnprocessedSegmentsTest.php @@ -215,6 +215,29 @@ class UnprocessedSegmentsTest extends IntegrationTestCase ]); } + public function test_add_realTimeEnabledInApi_whenRealTimeDisabledInConfig() + { + $this->expectExceptionMessage('Real time segments are disabled. You need to enable auto archiving.'); + $this->expectException(\Exception::class); + $config = Config::getInstance(); + $general = $config->General; + $general['enable_create_realtime_segments'] = 0; + $config->General = $general; + + API::getInstance()->add('testsegment', self::TEST_SEGMENT, $idSite = false, $autoArchive = false); + } + + public function test_add_realTimeEnabledInApi_whenRealTimeEnabledInConfigShouldWork() + { + $config = Config::getInstance(); + $general = $config->General; + $general['enable_create_realtime_segments'] = 1; + $config->General = $general; + + $id = API::getInstance()->add('testsegment', self::TEST_SEGMENT, $idSite = false, $autoArchive = false); + $this->assertNotEmpty($id); + } + public static function getOutputPrefix() { return ''; |