Move the file sharing app to migration

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2017-05-11 12:37:34 +02:00
parent 995fed8d8a
commit 643b4a4ec4
No known key found for this signature in database
GPG Key ID: E166FD8976B3BAC8
6 changed files with 227 additions and 526 deletions

View File

@ -51,4 +51,10 @@ Turning the feature off removes shared files and folders on the server for all s
<commands>
<command>OCA\Files_Sharing\Command\CleanupRemoteStorages</command>
</commands>
<repair-steps>
<post-migration>
<step>OCA\Files_Sharing\Migration\SetPasswordColumn</step>
</post-migration>
</repair-steps>
</info>

View File

@ -1,30 +0,0 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Björn Schießle <bjoern@schiessle.org>
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
use OCA\Files_Sharing\Migration;
$installedVersion = \OC::$server->getConfig()->getAppValue('files_sharing', 'installed_version');
if (version_compare($installedVersion, '1.4.0', '<')) {
$m = new Migration(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig());
$m->addPasswordColumn();
}

View File

@ -1,308 +0,0 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Björn Schießle <bjoern@schiessle.org>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <robin@icewind.nl>
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Files_Sharing;
use Doctrine\DBAL\Connection;
use OCP\ICache;
use OCP\IConfig;
use OCP\IDBConnection;
use OC\Cache\CappedMemoryCache;
/**
* Class Migration
*
* @package OCA\Files_Sharing
* @group DB
*/
class Migration {
/** @var IDBConnection */
private $connection;
/** @var IConfig */
private $config;
/** @var ICache with all shares we already saw */
private $shareCache;
/** @var string */
private $table = 'share';
public function __construct(IDBConnection $connection, IConfig $config) {
$this->connection = $connection;
$this->config = $config;
// We cache up to 10k share items (~20MB)
$this->shareCache = new CappedMemoryCache(10000);
}
/**
* move all re-shares to the owner in order to have a flat list of shares
* upgrade from oC 8.2 to 9.0 with the new sharing
*/
public function removeReShares() {
$stmt = $this->getReShares();
$owners = [];
while($share = $stmt->fetch()) {
$this->shareCache[$share['id']] = $share;
$owners[$share['id']] = [
'owner' => $this->findOwner($share),
'initiator' => $share['uid_owner'],
'type' => $share['share_type'],
];
if (count($owners) === 1000) {
$this->updateOwners($owners);
$owners = [];
}
}
$stmt->closeCursor();
if (count($owners)) {
$this->updateOwners($owners);
}
}
/**
* update all owner information so that all shares have an owner
* and an initiator for the upgrade from oC 8.2 to 9.0 with the new sharing
*/
public function updateInitiatorInfo() {
while (true) {
$shares = $this->getMissingInitiator(1000);
if (empty($shares)) {
break;
}
$owners = [];
foreach ($shares as $share) {
$owners[$share['id']] = [
'owner' => $share['uid_owner'],
'initiator' => $share['uid_owner'],
'type' => $share['share_type'],
];
}
$this->updateOwners($owners);
}
}
/**
* this was dropped for Nextcloud 11 in favour of share by mail
*/
public function removeSendMailOption() {
$this->config->deleteAppValue('core', 'shareapi_allow_mail_notification');
$this->config->deleteAppValue('core', 'shareapi_allow_public_notification');
}
public function addPasswordColumn() {
$query = $this->connection->getQueryBuilder();
$query
->update('share')
->set('password', 'share_with')
->where($query->expr()->eq('share_type', $query->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK)))
->andWhere($query->expr()->isNotNull('share_with'));
$query->execute();
$clearQuery = $this->connection->getQueryBuilder();
$clearQuery
->update('share')->set('share_with', $clearQuery->createNamedParameter(null))
->where($clearQuery->expr()->eq('share_type', $clearQuery->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK)));
$clearQuery->execute();
}
/**
* find the owner of a re-shared file/folder
*
* @param array $share
* @return array
*/
private function findOwner($share) {
$currentShare = $share;
while(!is_null($currentShare['parent'])) {
if (isset($this->shareCache[$currentShare['parent']])) {
$currentShare = $this->shareCache[$currentShare['parent']];
} else {
$currentShare = $this->getShare((int)$currentShare['parent']);
$this->shareCache[$currentShare['id']] = $currentShare;
}
}
return $currentShare['uid_owner'];
}
/**
* Get $n re-shares from the database
*
* @param int $n The max number of shares to fetch
* @return \Doctrine\DBAL\Driver\Statement
*/
private function getReShares() {
$query = $this->connection->getQueryBuilder();
$query->select(['id', 'parent', 'uid_owner', 'share_type'])
->from($this->table)
->where($query->expr()->in(
'share_type',
$query->createNamedParameter(
[
\OCP\Share::SHARE_TYPE_USER,
\OCP\Share::SHARE_TYPE_GROUP,
\OCP\Share::SHARE_TYPE_LINK,
\OCP\Share::SHARE_TYPE_REMOTE,
],
Connection::PARAM_INT_ARRAY
)
))
->andWhere($query->expr()->in(
'item_type',
$query->createNamedParameter(
['file', 'folder'],
Connection::PARAM_STR_ARRAY
)
))
->andWhere($query->expr()->isNotNull('parent'))
->orderBy('id', 'asc');
return $query->execute();
$shares = $result->fetchAll();
$result->closeCursor();
$ordered = [];
foreach ($shares as $share) {
$ordered[(int)$share['id']] = $share;
}
return $ordered;
}
/**
* Get $n re-shares from the database
*
* @param int $n The max number of shares to fetch
* @return array
*/
private function getMissingInitiator($n = 1000) {
$query = $this->connection->getQueryBuilder();
$query->select(['id', 'uid_owner', 'share_type'])
->from($this->table)
->where($query->expr()->in(
'share_type',
$query->createNamedParameter(
[
\OCP\Share::SHARE_TYPE_USER,
\OCP\Share::SHARE_TYPE_GROUP,
\OCP\Share::SHARE_TYPE_LINK,
\OCP\Share::SHARE_TYPE_REMOTE,
],
Connection::PARAM_INT_ARRAY
)
))
->andWhere($query->expr()->in(
'item_type',
$query->createNamedParameter(
['file', 'folder'],
Connection::PARAM_STR_ARRAY
)
))
->andWhere($query->expr()->isNull('uid_initiator'))
->orderBy('id', 'asc')
->setMaxResults($n);
$result = $query->execute();
$shares = $result->fetchAll();
$result->closeCursor();
$ordered = [];
foreach ($shares as $share) {
$ordered[(int)$share['id']] = $share;
}
return $ordered;
}
/**
* get a specific share
*
* @param int $id
* @return array
*/
private function getShare($id) {
$query = $this->connection->getQueryBuilder();
$query->select(['id', 'parent', 'uid_owner'])
->from($this->table)
->where($query->expr()->eq('id', $query->createNamedParameter($id)));
$result = $query->execute();
$share = $result->fetchAll();
$result->closeCursor();
return $share[0];
}
/**
* update database with the new owners
*
* @param array $owners
* @throws \Exception
*/
private function updateOwners($owners) {
$this->connection->beginTransaction();
try {
foreach ($owners as $id => $owner) {
$query = $this->connection->getQueryBuilder();
$query->update($this->table)
->set('uid_owner', $query->createNamedParameter($owner['owner']))
->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();
} catch (\Exception $e) {
$this->connection->rollBack();
throw $e;
}
}
}

View File

@ -0,0 +1,97 @@
<?php
/**
* @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
*
* @author Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Files_Sharing\Migration;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
use OCP\Share;
/**
* Class SetPasswordColumn
*
* @package OCA\Files_Sharing\Migration
*/
class SetPasswordColumn implements IRepairStep {
/** @var IDBConnection */
private $connection;
/** @var IConfig */
private $config;
public function __construct(IDBConnection $connection, IConfig $config) {
$this->connection = $connection;
$this->config = $config;
}
/**
* Returns the step's name
*
* @return string
* @since 9.1.0
*/
public function getName() {
return 'Copy the share password into the dedicated column';
}
/**
* @param IOutput $output
*/
public function run(IOutput $output) {
if (!$this->shouldRun()) {
return;
}
$query = $this->connection->getQueryBuilder();
$query
->update('share')
->set('password', 'share_with')
->where($query->expr()->eq('share_type', $query->createNamedParameter(Share::SHARE_TYPE_LINK)))
->andWhere($query->expr()->isNotNull('share_with'));
$result = $query->execute();
if ($result === 0) {
// No link updated, no need to run the second query
return;
}
$clearQuery = $this->connection->getQueryBuilder();
$clearQuery
->update('share')
->set('share_with', $clearQuery->createNamedParameter(null))
->where($clearQuery->expr()->eq('share_type', $clearQuery->createNamedParameter(Share::SHARE_TYPE_LINK)));
$clearQuery->execute();
}
protected function shouldRun() {
$appVersion = $this->config->getAppValue('files_sharing', 'installed_version', '0.0.0');
return version_compare($appVersion, '1.4.0', '<');
}
}

View File

@ -0,0 +1,124 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Björn Schießle <bjoern@schiessle.org>
* @author Joas Schilling <coding@schilljs.com>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Thomas Müller <thomas.mueller@tmit.eu>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Files_Sharing\Tests\Migration;
use OCA\Files_Sharing\Migration\SetPasswordColumn;
use OCA\Files_Sharing\Tests\TestCase;
use OCP\IConfig;
use OCP\Migration\IOutput;
use OCP\Share;
/**
* Class SetPasswordColumnTest
*
* @group DB
*/
class SetPasswordColumnTest extends TestCase {
/** @var \OCP\IDBConnection */
private $connection;
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
private $config;
/** @var SetPasswordColumn */
private $migration;
private $table = 'share';
public function setUp() {
parent::setUp();
$this->connection = \OC::$server->getDatabaseConnection();
$this->config = $this->createMock(IConfig::class);
$this->migration = new SetPasswordColumn($this->connection, $this->config);
$this->cleanDB();
}
public function tearDown() {
parent::tearDown();
$this->cleanDB();
}
private function cleanDB() {
$query = $this->connection->getQueryBuilder();
$query->delete($this->table)->execute();
}
public function testAddPasswordColumn() {
$this->config->expects($this->once())
->method('getAppValue')
->with('files_sharing', 'installed_version', '0.0.0')
->willReturn('1.3.0');
$shareTypes = [Share::SHARE_TYPE_USER, Share::SHARE_TYPE_GROUP, Share::SHARE_TYPE_REMOTE, Share::SHARE_TYPE_EMAIL, Share::SHARE_TYPE_LINK];
foreach ($shareTypes as $shareType) {
for ($i = 0; $i < 5; $i++) {
$query = $this->connection->getQueryBuilder();
$query->insert($this->table)
->values([
'share_type' => $query->createNamedParameter($shareType),
'share_with' => $query->createNamedParameter('shareWith'),
'uid_owner' => $query->createNamedParameter('user' . $i),
'uid_initiator' => $query->createNamedParameter(null),
'parent' => $query->createNamedParameter(0),
'item_type' => $query->createNamedParameter('file'),
'item_source' => $query->createNamedParameter('2'),
'item_target' => $query->createNamedParameter('/2'),
'file_source' => $query->createNamedParameter(2),
'file_target' => $query->createNamedParameter('/foobar'),
'permissions' => $query->createNamedParameter(31),
'stime' => $query->createNamedParameter(time()),
]);
$this->assertSame(1, $query->execute());
}
}
/** @var IOutput $output */
$output = $this->createMock(IOutput::class);
$this->migration->run($output);
$query = $this->connection->getQueryBuilder();
$query->select('*')
->from('share');
$allShares = $query->execute()->fetchAll();
foreach ($allShares as $share) {
if ((int)$share['share_type'] === Share::SHARE_TYPE_LINK) {
$this->assertNull( $share['share_with']);
$this->assertSame('shareWith', $share['password']);
} else {
$this->assertSame('shareWith', $share['share_with']);
$this->assertNull($share['password']);
}
}
}
}

View File

@ -1,188 +0,0 @@
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Björn Schießle <bjoern@schiessle.org>
* @author Joas Schilling <coding@schilljs.com>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Thomas Müller <thomas.mueller@tmit.eu>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Files_Sharing\Tests;
use OCA\Files_Sharing\Migration;
use OCP\Share;
/**
* Class MigrationTest
*
* @group DB
*/
class MigrationTest extends TestCase {
/** @var \OCP\IDBConnection */
private $connection;
/** @var \OCP\IConfig */
private $config;
/** @var Migration */
private $migration;
private $table = 'share';
public function setUp() {
parent::setUp();
$this->connection = \OC::$server->getDatabaseConnection();
$this->config = \OC::$server->getConfig();
$this->migration = new Migration($this->connection, $this->config);
$this->cleanDB();
}
public function tearDown() {
parent::tearDown();
$this->cleanDB();
}
private function cleanDB() {
$query = $this->connection->getQueryBuilder();
$query->delete($this->table)->execute();
}
public function verifyResult() {
$query = $this->connection->getQueryBuilder();
$query->select('*')->from($this->table)->orderBy('id');
$result = $query->execute()->fetchAll();
$this->assertSame(10, count($result));
// shares which shouldn't be modified
for ($i = 0; $i < 4; $i++) {
$this->assertSame('owner1', $result[$i]['uid_owner']);
$this->assertEmpty($result[$i]['uid_initiator']);
$this->assertNull($result[$i]['parent']);
}
// group share with unique target
$this->assertSame('owner1', $result[4]['uid_owner']);
$this->assertEmpty($result[4]['uid_initiator']);
$this->assertNotEmpty($result[4]['parent']);
// initial user share which was re-shared
$this->assertSame('owner2', $result[5]['uid_owner']);
$this->assertEmpty($result[5]['uid_initiator']);
$this->assertNull($result[5]['parent']);
// flatted re-shares
for($i = 6; $i < 9; $i++) {
$this->assertSame('owner2', $result[$i]['uid_owner']);
$user = 'user' . ($i - 5);
$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']);
}
/**
* test that we really remove the "shareapi_allow_mail_notification" setting only
*/
public function testRemoveSendMailOption() {
$this->config->setAppValue('core', 'shareapi_setting1', 'dummy-value');
$this->config->setAppValue('core', 'shareapi_allow_mail_notification', 'no');
$this->config->setAppValue('core', 'shareapi_allow_public_notification', 'no');
$this->migration->removeSendMailOption();
$this->assertNull(
$this->config->getAppValue('core', 'shareapi_allow_mail_notification', null)
);
$this->assertNull(
$this->config->getAppValue('core', 'shareapi_allow_public_notification', null)
);
$this->assertSame('dummy-value',
$this->config->getAppValue('core', 'shareapi_setting1', null)
);
}
public function testAddPasswordColumn() {
$shareTypes = [Share::SHARE_TYPE_USER, Share::SHARE_TYPE_GROUP, Share::SHARE_TYPE_REMOTE, Share::SHARE_TYPE_EMAIL, Share::SHARE_TYPE_LINK];
foreach ($shareTypes as $shareType) {
for ($i = 0; $i < 5; $i++) {
$query = $this->connection->getQueryBuilder();
$query->insert($this->table)
->values(
[
'share_type' => $query->createParameter('share_type'),
'share_with' => $query->createParameter('share_with'),
'uid_owner' => $query->createParameter('uid_owner'),
'uid_initiator' => $query->createParameter('uid_initiator'),
'parent' => $query->createParameter('parent'),
'item_type' => $query->createParameter('item_type'),
'item_source' => $query->createParameter('item_source'),
'item_target' => $query->createParameter('item_target'),
'file_source' => $query->createParameter('file_source'),
'file_target' => $query->createParameter('file_target'),
'permissions' => $query->createParameter('permissions'),
'stime' => $query->createParameter('stime'),
]
)
->setParameter('share_type', $shareType)
->setParameter('share_with', 'shareWith')
->setParameter('uid_owner', 'user' . ($i))
->setParameter('uid_initiator', null)
->setParameter('parent', 0)
->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());
}
}
$this->migration->addPasswordColumn();
$query = $this->connection->getQueryBuilder();
$query->select('*')->from('share');
$allShares = $query->execute()->fetchAll();
foreach ($allShares as $share) {
if ((int)$share['share_type'] === Share::SHARE_TYPE_LINK) {
$this->assertNull( $share['share_with']);
$this->assertSame('shareWith', $share['password']);
} else {
$this->assertSame('shareWith', $share['share_with']);
$this->assertNull($share['password']);
}
}
}
}