From 745bfda41fb033e679ebb43bb347d403b8eb1193 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Thu, 11 Feb 2016 13:28:15 +0100 Subject: [PATCH] Fix migration step to avoind invisible shares --- apps/files_sharing/lib/migration.php | 68 ++++++++++++---------- apps/files_sharing/tests/migrationtest.php | 27 ++++++++- 2 files changed, 63 insertions(+), 32 deletions(-) diff --git a/apps/files_sharing/lib/migration.php b/apps/files_sharing/lib/migration.php index e734638551..31a76687d4 100644 --- a/apps/files_sharing/lib/migration.php +++ b/apps/files_sharing/lib/migration.php @@ -55,32 +55,30 @@ class Migration { */ public function removeReShares() { - while(true) { - $reShares = $this->getReShares(1000); + $stmt = $this->getReShares(); - if (empty($reShares)) { - break; - } + $owners = []; + while($share = $stmt->fetch()) { - // Update the cache - foreach($reShares as $reShare) { - $this->shareCache[$reShare['id']] = $reShare; - } + $this->shareCache[$share['id']] = $share; - $owners = []; - foreach ($reShares as $share) { - $owners[$share['id']] = [ + $owners[$share['id']] = [ 'owner' => $this->findOwner($share), - 'initiator' => $share['uid_owner'] - ]; - } - $this->updateOwners($owners); + 'initiator' => $share['uid_owner'], + 'type' => $share['share_type'], + ]; - //Clear the cache of the shares we just updated so we have more room - foreach($owners as $id => $owner) { - unset($this->shareCache[$id]); + if (count($owners) === 1000) { + $this->updateOwners($owners); + $owners = []; } } + + $stmt->closeCursor(); + + if (count($owners)) { + $this->updateOwners($owners); + } } /** @@ -99,7 +97,8 @@ class Migration { foreach ($shares as $share) { $owners[$share['id']] = [ 'owner' => $share['uid_owner'], - 'initiator' => $share['uid_owner'] + 'initiator' => $share['uid_owner'], + 'type' => $share['share_type'], ]; } $this->updateOwners($owners); @@ -130,11 +129,11 @@ class Migration { * Get $n re-shares from the database * * @param int $n The max number of shares to fetch - * @return array + * @return \Doctrine\DBAL\Driver\Statement */ - private function getReShares($n = 1000) { + private function getReShares() { $query = $this->connection->getQueryBuilder(); - $query->select(['id', 'parent', 'uid_owner']) + $query->select(['id', 'parent', 'uid_owner', 'share_type']) ->from($this->table) ->where($query->expr()->in( 'share_type', @@ -156,9 +155,10 @@ class Migration { ) )) ->andWhere($query->expr()->isNotNull('parent')) - ->orderBy('id', 'asc') - ->setMaxResults($n); - $result = $query->execute(); + ->orderBy('id', 'asc'); + return $query->execute(); + + $shares = $result->fetchAll(); $result->closeCursor(); @@ -178,7 +178,7 @@ class Migration { */ private function getMissingInitiator($n = 1000) { $query = $this->connection->getQueryBuilder(); - $query->select(['id', 'uid_owner']) + $query->select(['id', 'uid_owner', 'share_type']) ->from($this->table) ->where($query->expr()->in( 'share_type', @@ -247,11 +247,17 @@ class Migration { foreach ($owners as $id => $owner) { $query = $this->connection->getQueryBuilder(); $query->update($this->table) - ->set('parent', $query->createNamedParameter(null)) ->set('uid_owner', $query->createNamedParameter($owner['owner'])) - ->set('uid_initiator', $query->createNamedParameter($owner['initiator'])) - ->where($query->expr()->eq('id', $query->createNamedParameter($id))) - ->execute(); + ->set('uid_initiator', $query->createNamedParameter($owner['initiator'])); + + + if ((int)$owner['type'] !== \OCP\Share::SHARE_TYPE_LINK) { + $query->set('parent', $query->createNamedParameter(null)); + } + + $query->where($query->expr()->eq('id', $query->createNamedParameter($id))); + + $query->execute(); } $this->connection->commit(); diff --git a/apps/files_sharing/tests/migrationtest.php b/apps/files_sharing/tests/migrationtest.php index 8a40b76a64..1bca4a419f 100644 --- a/apps/files_sharing/tests/migrationtest.php +++ b/apps/files_sharing/tests/migrationtest.php @@ -226,6 +226,23 @@ class MigrationTest extends TestCase { $this->assertSame(1, $query->execute() ); + + // Link reshare should keep its parent + $query->setParameter('share_type', \OCP\Share::SHARE_TYPE_LINK) + ->setParameter('share_with', null) + ->setParameter('uid_owner', 'user3') + ->setParameter('uid_initiator', '') + ->setParameter('parent', $parent) + ->setParameter('item_type', 'file') + ->setParameter('item_source', '2') + ->setParameter('item_target', '/2') + ->setParameter('file_source', 2) + ->setParameter('file_target', '/foobar') + ->setParameter('permissions', 31) + ->setParameter('stime', time()); + $this->assertSame(1, + $query->execute() + ); } public function testRemoveReShares() { @@ -238,7 +255,7 @@ class MigrationTest extends TestCase { $query = $this->connection->getQueryBuilder(); $query->select('*')->from($this->table)->orderBy('id'); $result = $query->execute()->fetchAll(); - $this->assertSame(9, count($result)); + $this->assertSame(10, count($result)); // shares which shouldn't be modified for ($i = 0; $i < 4; $i++) { @@ -261,6 +278,14 @@ class MigrationTest extends TestCase { $this->assertSame($user, $result[$i]['uid_initiator']); $this->assertNull($result[$i]['parent']); } + + /* + * The link share is flattend but has an owner to avoid invisible shares + * see: https://github.com/owncloud/core/pull/22317 + */ + $this->assertSame('owner2', $result[9]['uid_owner']); + $this->assertSame('user3', $result[9]['uid_initiator']); + $this->assertSame($result[7]['id'], $result[9]['parent']); } public function test1001DeepReshares() {