diff options
author | Zoltan Flamis <zoltan@innocraft.com> | 2021-02-23 05:41:15 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-23 05:41:15 +0300 |
commit | d288c96baf2b4c30b022364f6fdefe00b41e3821 (patch) | |
tree | e2625897d0c7da55abfdbe1733d2400d0e6fed14 | |
parent | 45fbd45ba47ed2d8ed0ea96e46d5c62d8d4f5264 (diff) |
13976 segments not deleted (#17231)
* transfer segments when user deleted
* test transferAllUserSegments
* test deleted user lost segments
* fix test errors
* remove static modifier
Co-authored-by: dizzy <diosmosis@users.noreply.github.com>
4 files changed, 101 insertions, 1 deletions
diff --git a/plugins/Feedback/tests/Integration/ControllerTest.php b/plugins/Feedback/tests/Integration/ControllerTest.php index 67f41da8d9..533dc533f5 100644 --- a/plugins/Feedback/tests/Integration/ControllerTest.php +++ b/plugins/Feedback/tests/Integration/ControllerTest.php @@ -47,6 +47,7 @@ class ControllerTest extends IntegrationTestCase public function tearDown(): void { + FakeAccess::$identity = 'user1'; Option::deleteLike('Feedback.nextFeedbackReminder.%'); $this->userModel->deleteUserOnly('user1'); Date::$now = $this->now; diff --git a/plugins/Feedback/tests/Integration/FeedbackTest.php b/plugins/Feedback/tests/Integration/FeedbackTest.php index f044592a84..5b926e57d1 100644 --- a/plugins/Feedback/tests/Integration/FeedbackTest.php +++ b/plugins/Feedback/tests/Integration/FeedbackTest.php @@ -48,6 +48,7 @@ class FeedbackTest extends IntegrationTestCase public function tearDown(): void { + FakeAccess::$identity = 'user1'; Option::deleteLike('Feedback.nextFeedbackReminder.%'); $this->userModel->deleteUserOnly('user1'); Date::$now = $this->now; diff --git a/plugins/SegmentEditor/SegmentEditor.php b/plugins/SegmentEditor/SegmentEditor.php index f9387fcb04..3a8a830454 100644 --- a/plugins/SegmentEditor/SegmentEditor.php +++ b/plugins/SegmentEditor/SegmentEditor.php @@ -29,6 +29,8 @@ use Piwik\Site; use Piwik\Period; use Piwik\Url; use Piwik\View; +use Piwik\Plugins\UsersManager\API as UsersManagerApi; +use Piwik\Date; /** */ @@ -52,7 +54,8 @@ class SegmentEditor extends \Piwik\Plugin 'Visualization.onNoData' => 'onNoData', 'Archive.noArchivedData' => 'onNoArchiveData', 'Db.getTablesInstalled' => 'getTablesInstalled', - 'SitesManager.deleteSite.end' => 'onDeleteSite' + 'SitesManager.deleteSite.end' => 'onDeleteSite', + 'UsersManager.deleteUser' => 'onDeleteUser', ); } @@ -358,4 +361,38 @@ class SegmentEditor extends \Piwik\Plugin } return $segments; } + + public function onDeleteUser($userLogin) + { + $this->transferAllUserSegmentsToSuperUser($userLogin); + } + + public function transferAllUserSegmentsToSuperUser($userLogin) + { + $model = new Model(); + $updatedAt = Date::factory('now')->toString('Y-m-d H:i:s'); + + $superUsers = UsersManagerApi::getInstance()->getUsersHavingSuperUserAccess(); + $superUserLogin = false; + + foreach ($superUsers as $superUser) { + if ($superUser['login'] !== $userLogin) { + $superUserLogin = $superUser['login']; + break; + } + } + + if (!$superUserLogin) { + return; + } + + foreach ($model->getAllSegments($userLogin) as $segment) { + if ($segment['login'] === $userLogin) { + $model->updateSegment($segment['idsegment'], array( + 'login' => $superUserLogin, + 'ts_last_edit' => $updatedAt + )); + } + } + } } diff --git a/plugins/SegmentEditor/tests/Integration/SegmentEditorTest.php b/plugins/SegmentEditor/tests/Integration/SegmentEditorTest.php index d6b594280d..a536243a2d 100644 --- a/plugins/SegmentEditor/tests/Integration/SegmentEditorTest.php +++ b/plugins/SegmentEditor/tests/Integration/SegmentEditorTest.php @@ -17,6 +17,9 @@ use Piwik\Plugins\SitesManager\API as APISitesManager; use Piwik\Tests\Framework\Mock\FakeAccess; use Piwik\Tests\Framework\TestCase\IntegrationTestCase; use Exception; +use Piwik\Plugins\UsersManager\UserUpdater; +use Piwik\Plugins\SegmentEditor\SegmentEditor; +use Piwik\Plugins\UsersManager\API as UsersManagerAPI; /** * Class Plugins_SegmentEditorTest @@ -180,6 +183,49 @@ class SegmentEditorTest extends IntegrationTestCase API::getInstance()->get($idSegment1); } + public function test_transferAllUserSegmentsToSuperUser() + { + Rules::setBrowserTriggerArchiving(false); + + $this->addUser('user1'); + $this->addUser('super', true); + + FakeAccess::$identity = 'user1'; + + $idSegment = API::getInstance()->add('name 1', 'searches==0', $idSite = 1, $autoArchive = 1, $enabledAllUsers = 0); + $segment = API::getInstance()->get($idSegment); + + $this->assertEquals('user1', $segment['login']); + + $segmentEditor = new SegmentEditor(); + $segmentEditor->transferAllUserSegmentsToSuperUser('user1'); + + $segment = API::getInstance()->get($idSegment); + + $this->assertEquals('super', $segment['login']); + } + + public function test_deletedUserLostTheSegments() + { + Rules::setBrowserTriggerArchiving(false); + $model = new Model(); + + $this->addUser('user1'); + $this->addUser('super', true); + + FakeAccess::$identity = 'user1'; + API::getInstance()->add('name 1', 'searches==0', $idSite = 1, $autoArchive = 1, $enabledAllUsers = 0); + + $segments = $model->getAllSegments('user1'); + $this->assertNotEmpty($segments); + + FakeAccess::$identity = 'super'; + $this->deleteUser('user1'); + + $segments = $model->getAllSegments('user1'); + $this->assertEmpty($segments); + } + private function removeSecondsFromSegmentInfo(&$segmentInfo) { $timestampProperties = array('ts_last_edit', 'ts_created'); @@ -196,4 +242,19 @@ class SegmentEditorTest extends IntegrationTestCase 'Piwik\Access' => new FakeAccess() ); } + + private function addUser($login, $isSuper = false) + { + UsersManagerAPI::getInstance()->addUser($login, 'password', "{$login}@test.com"); + + if ($isSuper) { + $userUpdater = new UserUpdater(); + $userUpdater->setSuperUserAccessWithoutCurrentPassword($login, true); + } + } + + private function deleteUser($login) + { + UsersManagerAPI::getInstance()->deleteUser($login); + } } |