From 60a659c87e1a3cb2c65dc330cb64c3414fd4b648 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 22 May 2014 01:39:24 +0200 Subject: Add a system for (re)movable mount points --- lib/private/files/mount/manager.php | 9 +++++++ lib/private/files/mount/mount.php | 12 ++++----- lib/private/files/mount/moveablemount.php | 30 ++++++++++++++++++++++ lib/private/files/view.php | 41 ++++++++++++++----------------- 4 files changed, 64 insertions(+), 28 deletions(-) create mode 100644 lib/private/files/mount/moveablemount.php (limited to 'lib') diff --git a/lib/private/files/mount/manager.php b/lib/private/files/mount/manager.php index db1f4600c74..45a9f339fba 100644 --- a/lib/private/files/mount/manager.php +++ b/lib/private/files/mount/manager.php @@ -30,6 +30,15 @@ class Manager { unset($this->mounts[$mountPoint]); } + /** + * @param string $mountPoint + * @param string $target + */ + public function moveMount($mountPoint, $target){ + $this->mounts[$target] = $this->mounts[$mountPoint]; + unset($this->mounts[$mountPoint]); + } + /** * Find the mount for $path * diff --git a/lib/private/files/mount/mount.php b/lib/private/files/mount/mount.php index 7c40853ac95..7561cd7311b 100644 --- a/lib/private/files/mount/mount.php +++ b/lib/private/files/mount/mount.php @@ -16,11 +16,11 @@ class Mount { /** * @var \OC\Files\Storage\Storage $storage */ - private $storage = null; - private $class; - private $storageId; - private $arguments = array(); - private $mountPoint; + protected $storage = null; + protected $class; + protected $storageId; + protected $arguments = array(); + protected $mountPoint; /** * @var \OC\Files\Storage\Loader $loader @@ -142,7 +142,7 @@ class Mount { } else { $internalPath = substr($path, strlen($this->mountPoint)); } - return $internalPath; + return (string)$internalPath; } /** diff --git a/lib/private/files/mount/moveablemount.php b/lib/private/files/mount/moveablemount.php new file mode 100644 index 00000000000..117649339e9 --- /dev/null +++ b/lib/private/files/mount/moveablemount.php @@ -0,0 +1,30 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Files\Mount; + +/** + * Defines the mount point to be (re)moved by the user + */ +interface MoveableMount { + /** + * Move the mount point to $target + * + * @param string $target the target mount point + * @return bool + */ + public function moveMount($target); + + /** + * Remove the mount points + * + * @return mixed + * @return bool + */ + public function removeMount(); +} diff --git a/lib/private/files/view.php b/lib/private/files/view.php index 1dc6c405bcf..a2188f393fa 100644 --- a/lib/private/files/view.php +++ b/lib/private/files/view.php @@ -26,6 +26,7 @@ namespace OC\Files; use OC\Files\Cache\Updater; +use OC\Files\Mount\MoveableMount; class View { private $fakeRoot = ''; @@ -357,10 +358,8 @@ class View { } $postFix = (substr($path, -1, 1) === '/') ? '/' : ''; $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); - list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); - if (!($storage instanceof \OC\Files\Storage\Shared) && - (!$internalPath || $internalPath === '' || $internalPath === '/') - ) { + $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); + if (!($mount instanceof MoveableMount) && $mount->getInternalPath($absolutePath) === '') { // do not allow deleting the storage's root / the mount point // because for some storages it might delete the whole contents // but isn't supposed to work that way @@ -411,18 +410,19 @@ class View { if ($run) { $mp1 = $this->getMountPoint($path1 . $postFix1); $mp2 = $this->getMountPoint($path2 . $postFix2); - list($storage1, $internalPath1) = Filesystem::resolvePath($absolutePath1 . $postFix1); + $manager = Filesystem::getMountManager(); + $mount = $manager->find($absolutePath1 . $postFix1); + $storage1 = $mount->getStorage(); + $internalPath1 = $mount->getInternalPath($absolutePath1 . $postFix1); list(, $internalPath2) = Filesystem::resolvePath($absolutePath2 . $postFix2); - // if source and target are on the same storage we can call the rename operation from the - // storage. If it is a "Shared" file/folder we call always the rename operation of the - // shared storage to handle mount point renaming, etc correctly - if ($storage1 instanceof \OC\Files\Storage\Shared) { - if ($storage1) { - $result = $storage1->rename($absolutePath1, $absolutePath2); - \OC_FileProxy::runPostProxies('rename', $absolutePath1, $absolutePath2); - } else { - $result = false; - } + if ($internalPath1 == '' and $mount instanceof MoveableMount) { + /** + * @var \OC\Files\Mount\Mount | \OC\Files\Mount\MoveableMount $mount + */ + $sourceMountPoint = $mount->getMountPoint(); + $result = $mount->moveMount($absolutePath2); + $manager->moveMount($sourceMountPoint, $mount->getMountPoint()); + \OC_FileProxy::runPostProxies('rename', $absolutePath1, $absolutePath2); } elseif ($mp1 == $mp2) { if ($storage1) { $result = $storage1->rename($internalPath1, $internalPath2); @@ -888,10 +888,6 @@ class View { return $result; } $path = Filesystem::normalizePath($this->fakeRoot . '/' . $directory); - /** - * @var \OC\Files\Storage\Storage $storage - * @var string $internalPath - */ list($storage, $internalPath) = Filesystem::resolvePath($path); if ($storage) { $cache = $storage->getCache($internalPath); @@ -924,9 +920,10 @@ class View { } //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders - $mountPoints = Filesystem::getMountPoints($path); + $mounts = Filesystem::getMountManager()->findIn($path); $dirLength = strlen($path); - foreach ($mountPoints as $mountPoint) { + foreach ($mounts as $mount) { + $mountPoint = $mount->getMountPoint(); $subStorage = Filesystem::getStorage($mountPoint); if ($subStorage) { $subCache = $subStorage->getCache(''); @@ -953,7 +950,7 @@ class View { $permissions = $rootEntry['permissions']; // do not allow renaming/deleting the mount point if they are not shared files/folders // for shared files/folders we use the permissions given by the owner - if ($subStorage instanceof \OC\Files\Storage\Shared) { + if ($mount instanceof MoveableMount) { $rootEntry['permissions'] = $permissions; } else { $rootEntry['permissions'] = $permissions & (\OCP\PERMISSION_ALL - (\OCP\PERMISSION_UPDATE | \OCP\PERMISSION_DELETE)); -- cgit v1.2.3 From 8abe1c3f1a5f5da36ab6ac8d1c39b966991387ce Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 22 May 2014 01:40:04 +0200 Subject: Don't do rename hooks for cache when moving the mountpoint --- lib/private/files/cache/updater.php | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'lib') diff --git a/lib/private/files/cache/updater.php b/lib/private/files/cache/updater.php index f6feb6624b2..f15c203cd58 100644 --- a/lib/private/files/cache/updater.php +++ b/lib/private/files/cache/updater.php @@ -83,6 +83,10 @@ class Updater { * @var string $internalTo */ list($storageFrom, $internalFrom) = self::resolvePath($from); + // if it's a moved mountpoint we dont need to do anything + if ($internalFrom === '') { + return; + } list($storageTo, $internalTo) = self::resolvePath($to); if ($storageFrom && $storageTo) { if ($storageFrom === $storageTo) { -- cgit v1.2.3 From 4fbc991ea2bd66f1f918c9c6abf501ca767a8a8d Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 22 May 2014 01:52:55 +0200 Subject: Add the removing logic for mounts --- lib/private/files/view.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/private/files/view.php b/lib/private/files/view.php index a2188f393fa..1515769116f 100644 --- a/lib/private/files/view.php +++ b/lib/private/files/view.php @@ -359,11 +359,15 @@ class View { $postFix = (substr($path, -1, 1) === '/') ? '/' : ''; $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); - if (!($mount instanceof MoveableMount) && $mount->getInternalPath($absolutePath) === '') { - // do not allow deleting the storage's root / the mount point - // because for some storages it might delete the whole contents - // but isn't supposed to work that way - return false; + if ($mount->getInternalPath($absolutePath) === '') { + if ($mount instanceof MoveableMount) { + return $mount->removeMount(); + } else { + // do not allow deleting the storage's root / the mount point + // because for some storages it might delete the whole contents + // but isn't supposed to work that way + return false; + } } return $this->basicOperation('unlink', $path, array('delete')); } -- cgit v1.2.3 From bf5e9357fc5dacc0bc951e7c60fe7105533a56fb Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Tue, 27 May 2014 11:05:31 +0200 Subject: don't allow to share single files with delete permissions, user should only be possible to unshare a single file but never to delete it --- apps/files_sharing/appinfo/update.php | 38 +++++ apps/files_sharing/appinfo/version | 2 +- apps/files_sharing/js/share.js | 9 +- apps/files_sharing/lib/sharedstorage.php | 23 ++- apps/files_sharing/tests/share.php | 108 ++++++++++++++ apps/files_sharing/tests/update.php | 233 +++++++++++++++++++++++++++++++ apps/files_sharing/tests/updater.php | 125 ----------------- lib/private/share/share.php | 5 + 8 files changed, 413 insertions(+), 130 deletions(-) create mode 100644 apps/files_sharing/tests/share.php create mode 100644 apps/files_sharing/tests/update.php (limited to 'lib') diff --git a/apps/files_sharing/appinfo/update.php b/apps/files_sharing/appinfo/update.php index bc8cda42313..bc17915613c 100644 --- a/apps/files_sharing/appinfo/update.php +++ b/apps/files_sharing/appinfo/update.php @@ -1,6 +1,11 @@ execute(array('file')); + + $updatedRows = array(); + + while ($row = $result->fetchRow()) { + if ($row['permissions'] & \OCP\PERMISSION_DELETE) { + $updatedRows[$row['id']] = (int)$row['permissions'] & ~\OCP\PERMISSION_DELETE; + } + } + + $chunkedPermissionList = array_chunk($updatedRows, $chunkSize, true); + + foreach ($chunkedPermissionList as $subList) { + $statement = "UPDATE `*PREFIX*share` SET `permissions` = CASE `id` "; + //update share table + $ids = implode(',', array_keys($subList)); + foreach ($subList as $id => $permission) { + $statement .= "WHEN " . $id . " THEN " . $permission . " "; + } + $statement .= ' END WHERE `id` IN (' . $ids . ')'; + + $query = OCP\DB::prepare($statement); + $query->execute(); + } + +} + /** * update script for the removal of the logical "Shared" folder, we create physical "Shared" folder and * update the users file_target so that it doesn't make any difference for the user diff --git a/apps/files_sharing/appinfo/version b/apps/files_sharing/appinfo/version index 267577d47e4..2eb3c4fe4ee 100644 --- a/apps/files_sharing/appinfo/version +++ b/apps/files_sharing/appinfo/version @@ -1 +1 @@ -0.4.1 +0.5 diff --git a/apps/files_sharing/js/share.js b/apps/files_sharing/js/share.js index d63a590fb8e..47fe0bd2c57 100644 --- a/apps/files_sharing/js/share.js +++ b/apps/files_sharing/js/share.js @@ -18,12 +18,17 @@ var oldCreateRow = OCA.Files.FileList.prototype._createRow; OCA.Files.FileList.prototype._createRow = function(fileData) { var tr = oldCreateRow.apply(this, arguments); + var sharePermissions = fileData.permissions; + if (fileData.type === 'file') { + // files can't be shared with delete permissions + sharePermissions = sharePermissions & ~OC.PERMISSION_DELETE; + } + tr.attr('data-share-permissions', sharePermissions); if (fileData.shareOwner) { tr.attr('data-share-owner', fileData.shareOwner); // user should always be able to rename a mount point if (fileData.isShareMountPoint) { tr.attr('data-permissions', fileData.permissions | OC.PERMISSION_UPDATE); - tr.attr('data-reshare-permissions', fileData.permissions); } } if (fileData.recipientsDisplayName) { @@ -94,7 +99,7 @@ if ($tr.data('type') === 'dir') { itemType = 'folder'; } - var possiblePermissions = $tr.data('reshare-permissions'); + var possiblePermissions = $tr.data('share-permissions'); if (_.isUndefined(possiblePermissions)) { possiblePermissions = $tr.data('permissions'); } diff --git a/apps/files_sharing/lib/sharedstorage.php b/apps/files_sharing/lib/sharedstorage.php index 2418b830cbc..c5b5060893a 100644 --- a/apps/files_sharing/lib/sharedstorage.php +++ b/apps/files_sharing/lib/sharedstorage.php @@ -128,7 +128,18 @@ class Shared extends \OC\Files\Storage\Common { return false; } + /** + * Delete the directory if DELETE permission is granted + * @param string $path + * @return boolean + */ public function rmdir($path) { + + // never delete a share mount point + if(empty($path)) { + return false; + } + if (($source = $this->getSourcePath($path)) && $this->isDeletable($path)) { list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source); return $storage->rmdir($internalPath); @@ -256,9 +267,17 @@ class Shared extends \OC\Files\Storage\Common { return false; } + /** + * Delete the file if DELETE permission is granted + * @param string $path + * @return boolean + */ public function unlink($path) { - // Delete the file if DELETE permission is granted - $path = ($path === false) ? '' : $path; + + // never delete a share mount point + if (empty($path)) { + return false; + } if ($source = $this->getSourcePath($path)) { if ($this->isDeletable($path)) { list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source); diff --git a/apps/files_sharing/tests/share.php b/apps/files_sharing/tests/share.php new file mode 100644 index 00000000000..a81f84ef061 --- /dev/null +++ b/apps/files_sharing/tests/share.php @@ -0,0 +1,108 @@ + + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This library 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 library. If not, see . + * + */ + +require_once __DIR__ . '/base.php'; + +use OCA\Files\Share; + +/** + * Class Test_Files_Sharing + */ +class Test_Files_Sharing extends Test_Files_Sharing_Base { + + const TEST_FOLDER_NAME = '/folder_share_api_test'; + + private static $tempStorage; + + function setUp() { + parent::setUp(); + + $this->folder = self::TEST_FOLDER_NAME; + $this->subfolder = '/subfolder_share_api_test'; + $this->subsubfolder = '/subsubfolder_share_api_test'; + + $this->filename = '/share-api-test.txt'; + + // save file with content + $this->view->file_put_contents($this->filename, $this->data); + $this->view->mkdir($this->folder); + $this->view->mkdir($this->folder . $this->subfolder); + $this->view->mkdir($this->folder . $this->subfolder . $this->subsubfolder); + $this->view->file_put_contents($this->folder.$this->filename, $this->data); + $this->view->file_put_contents($this->folder . $this->subfolder . $this->filename, $this->data); + } + + function tearDown() { + $this->view->unlink($this->filename); + $this->view->deleteAll($this->folder); + + self::$tempStorage = null; + + parent::tearDown(); + } + + /** + * shared files should never have delete permissions + * @dataProvider DataProviderTestFileSharePermissions + */ + function testFileSharePermissions($permission, $expectedPermissions) { + + $fileinfo = $this->view->getFileInfo($this->filename); + + $result = \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER, + \Test_Files_Sharing::TEST_FILES_SHARING_API_USER2, $permission); + + $this->assertTrue($result); + + $result = \OCP\Share::getItemShared('file', null); + + $this->assertTrue(is_array($result)); + + // test should return exactly one shares created from testCreateShare() + $this->assertTrue(count($result) === 1); + + $share = reset($result); + $this->assertSame($expectedPermissions, $share['permissions']); + + \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER, + \Test_Files_Sharing::TEST_FILES_SHARING_API_USER2); + } + + function DataProviderTestFileSharePermissions() { + $permission1 = \OCP\PERMISSION_ALL; + $permission2 = \OCP\PERMISSION_DELETE; + $permission3 = \OCP\PERMISSION_READ; + $permission4 = \OCP\PERMISSION_READ | \OCP\PERMISSION_UPDATE; + $permission5 = \OCP\PERMISSION_READ | \OCP\PERMISSION_DELETE; + $permission6 = \OCP\PERMISSION_READ | \OCP\PERMISSION_UPDATE | \OCP\PERMISSION_DELETE; + + return array( + array($permission1, \OCP\PERMISSION_ALL & ~\OCP\PERMISSION_DELETE), + array($permission2, 0), + array($permission3, $permission3), + array($permission4, $permission4), + array($permission5, $permission3), + array($permission6, $permission4), + ); + } + +} diff --git a/apps/files_sharing/tests/update.php b/apps/files_sharing/tests/update.php new file mode 100644 index 00000000000..b0215d68176 --- /dev/null +++ b/apps/files_sharing/tests/update.php @@ -0,0 +1,233 @@ + + * 2014 Bjoern Schiessle + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This library 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 library. If not, see . + * + */ + +require_once __DIR__ . '/../appinfo/update.php'; +require_once __DIR__ . '/base.php'; + +/** + * Class Test_Files_Sharing_Update + */ +class Test_Files_Sharing_Update_Routine extends Test_Files_Sharing_Base { + + const TEST_FOLDER_NAME = '/folder_share_api_test'; + + function setUp() { + parent::setUp(); + + $this->folder = self::TEST_FOLDER_NAME; + + $this->filename = '/share-api-test.txt'; + + // save file with content + $this->view->file_put_contents($this->filename, $this->data); + $this->view->mkdir($this->folder); + $this->view->file_put_contents($this->folder . '/' . $this->filename, $this->data); + } + + function tearDown() { + $this->view->unlink($this->filename); + $this->view->deleteAll($this->folder); + + $removeShares = \OC_DB::prepare('DELETE FROM `*PREFIX*share`'); + $removeShares->execute(); + $removeItems = \OC_DB::prepare('DELETE FROM `*PREFIX*filecache`'); + $removeItems->execute(); + + parent::tearDown(); + } + + /** + * test update of file permission. The update should remove from all shared + * files the delete permission + */ + function testUpdateFilePermissions() { + + self::prepareDBUpdateFilePermissions(); + // run the update routine to update the share permission + updateFilePermissions(2); + + // verify results + $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*share`'); + $result = $query->execute(array()); + + while ($row = $result->fetchRow()) { + if ($row['item_type'] === 'file') { + // for all files the delete permission should be removed + $this->assertSame(0, (int)$row['permissions'] & \OCP\PERMISSION_DELETE); + } else { + // for all other the permission shouldn't change + $this->assertSame(31, (int)$row['permissions'] & \OCP\PERMISSION_ALL); + } + } + + // cleanup + $this->cleanupSharedTable(); + } + + /** + * @medium + */ + function testRemoveBrokenShares() { + + $this->prepareFileCache(); + + // check if there are just 3 shares (see setUp - precondition: empty table) + $countShares = \OC_DB::prepare('SELECT COUNT(`id`) FROM `*PREFIX*share`'); + $result = $countShares->execute()->fetchOne(); + $this->assertEquals(3, $result); + + // check if there are just 2 items (see setUp - precondition: empty table) + $countItems = \OC_DB::prepare('SELECT COUNT(`fileid`) FROM `*PREFIX*filecache`'); + $result = $countItems->execute()->fetchOne(); + $this->assertEquals(2, $result); + + // execute actual code which should be tested + \OC\Files\Cache\Shared_Updater::fixBrokenSharesOnAppUpdate(); + + // check if there are just 2 shares (one gets killed by the code as there is no filecache entry for this) + $countShares = \OC_DB::prepare('SELECT COUNT(`id`) FROM `*PREFIX*share`'); + $result = $countShares->execute()->fetchOne(); + $this->assertEquals(2, $result); + + // check if the share of file '200' is removed as there is no entry for this in filecache table + $countShares = \OC_DB::prepare('SELECT COUNT(`id`) FROM `*PREFIX*share` WHERE `file_source` = 200'); + $result = $countShares->execute()->fetchOne(); + $this->assertEquals(0, $result); + + // check if there are just 2 items + $countItems = \OC_DB::prepare('SELECT COUNT(`fileid`) FROM `*PREFIX*filecache`'); + $result = $countItems->execute()->fetchOne(); + $this->assertEquals(2, $result); + } + + /** + * test update for the removal of the logical "Shared" folder. It should update + * the file_target for every share and create a physical "Shared" folder for each user + */ + function testRemoveSharedFolder() { + self::prepareDB(); + // run the update routine to remove the shared folder and replace it with a real folder + removeSharedFolder(false, 2); + + // verify results + $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*share`'); + $result = $query->execute(array()); + + $newDBContent = $result->fetchAll(); + + foreach ($newDBContent as $row) { + if ((int)$row['share_type'] === \OCP\Share::SHARE_TYPE_USER) { + $this->assertSame('/Shared', substr($row['file_target'], 0, strlen('/Shared'))); + } else { + $this->assertSame('/ShouldNotChange', $row['file_target']); + } + } + + // cleanup + $this->cleanupSharedTable(); + + } + + private function cleanupSharedTable() { + $query = \OC_DB::prepare('DELETE FROM `*PREFIX*share`'); + $query->execute(); + } + + /** + * prepare sharing table for testRemoveSharedFolder() + */ + private function prepareDB() { + $this->cleanupSharedTable(); + // add items except one - because this is the test case for the broken share table + $addItems = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (`share_type`, `item_type`, ' . + '`share_with`, `uid_owner` , `file_target`) ' . + 'VALUES (?, ?, ?, ?, ?)'); + $items = array( + array(\OCP\Share::SHARE_TYPE_USER, 'file', 'user1', 'admin' , '/foo'), + array(\OCP\Share::SHARE_TYPE_USER, 'folder', 'user2', 'admin', '/foo2'), + array(\OCP\Share::SHARE_TYPE_USER, 'file', 'user3', 'admin', '/foo3'), + array(\OCP\Share::SHARE_TYPE_USER, 'folder', 'user4', 'admin', '/foo4'), + array(\OCP\Share::SHARE_TYPE_LINK, 'file', 'user1', 'admin', '/ShouldNotChange'), + array(\OCP\Share::SHARE_TYPE_CONTACT, 'contact', 'admin', 'user1', '/ShouldNotChange'), + + ); + foreach($items as $item) { + // the number is used as path_hash + $addItems->execute($item); + } + } + + /** + * prepare sharing table for testUpdateFilePermissions() + */ + private function prepareDBUpdateFilePermissions() { + $this->cleanupSharedTable(); + // add items except one - because this is the test case for the broken share table + $addItems = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (`share_type`, `item_type`, ' . + '`share_with`, `uid_owner` , `file_target`, `permissions`) ' . + 'VALUES (?, ?, ?, ?, ?, ?)'); + $items = array( + array(\OCP\Share::SHARE_TYPE_LINK, 'file', 'user1', 'admin', '/foo', \OCP\PERMISSION_ALL), + array(\OCP\Share::SHARE_TYPE_CONTACT, 'contact', 'admin', 'user1', '/foo', \OCP\PERMISSION_ALL), + array(\OCP\Share::SHARE_TYPE_USER, 'folder', 'user4', 'admin', '/foo', \OCP\PERMISSION_ALL), + array(\OCP\Share::SHARE_TYPE_USER, 'file', 'user3', 'admin', '/foo3', \OCP\PERMISSION_ALL), + array(\OCP\Share::SHARE_TYPE_USER, 'file', 'user1', 'admin' , '/foo', \OCP\PERMISSION_DELETE), + array(\OCP\Share::SHARE_TYPE_USER, 'file', 'user1', 'admin' , '/foo', \OCP\PERMISSION_READ & \OCP\PERMISSION_DELETE), + array(\OCP\Share::SHARE_TYPE_USER, 'file', 'user1', 'admin' , '/foo', \OCP\PERMISSION_SHARE & \OCP\PERMISSION_UPDATE), + array(\OCP\Share::SHARE_TYPE_USER, 'file', 'user1', 'admin' , '/foo', \OCP\PERMISSION_ALL), + array(\OCP\Share::SHARE_TYPE_USER, 'file', 'user1', 'admin' , '/foo', \OCP\PERMISSION_SHARE & \OCP\PERMISSION_READ & \OCP\PERMISSION_DELETE), + ); + foreach($items as $item) { + // the number is used as path_hash + $addItems->execute($item); + } + } + + /** + * prepare file cache for testRemoveBrokenShares() + */ + private function prepareFileCache() { + // some previous tests didn't clean up and therefore this has to be done here + // FIXME: DIRTY HACK - TODO: find tests, that don't clean up and fix it there + $this->tearDown(); + + // add items except one - because this is the test case for the broken share table + $addItems = \OC_DB::prepare('INSERT INTO `*PREFIX*filecache` (`storage`, `path_hash`, ' . + '`parent`, `mimetype`, `mimepart`, `size`, `mtime`, `storage_mtime`) ' . + 'VALUES (1, ?, 1, 1, 1, 1, 1, 1)'); + $items = array(1, 3); + $fileIds = array(); + foreach($items as $item) { + // the number is used as path_hash + $addItems->execute(array($item)); + $fileIds[] = \OC_DB::insertId('*PREFIX*filecache'); + } + + $addShares = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (`file_source`, `item_type`, `uid_owner`) VALUES (?, \'file\', 1)'); + // the number is used as item_source + $addShares->execute(array($fileIds[0])); + $addShares->execute(array(200)); // id of "deleted" file + $addShares->execute(array($fileIds[1])); + } + +} diff --git a/apps/files_sharing/tests/updater.php b/apps/files_sharing/tests/updater.php index 1b851cccf6c..8183e7067a4 100644 --- a/apps/files_sharing/tests/updater.php +++ b/apps/files_sharing/tests/updater.php @@ -47,11 +47,6 @@ class Test_Files_Sharing_Updater extends Test_Files_Sharing_Base { $this->view->unlink($this->filename); $this->view->deleteAll($this->folder); - $removeShares = \OC_DB::prepare('DELETE FROM `*PREFIX*share`'); - $removeShares->execute(); - $removeItems = \OC_DB::prepare('DELETE FROM `*PREFIX*filecache`'); - $removeItems->execute(); - parent::tearDown(); } @@ -111,124 +106,4 @@ class Test_Files_Sharing_Updater extends Test_Files_Sharing_Base { } } - /** - * @medium - */ - function testRemoveBrokenShares() { - - $this->prepareFileCache(); - - // check if there are just 3 shares (see setUp - precondition: empty table) - $countShares = \OC_DB::prepare('SELECT COUNT(`id`) FROM `*PREFIX*share`'); - $result = $countShares->execute()->fetchOne(); - $this->assertEquals(3, $result); - - // check if there are just 2 items (see setUp - precondition: empty table) - $countItems = \OC_DB::prepare('SELECT COUNT(`fileid`) FROM `*PREFIX*filecache`'); - $result = $countItems->execute()->fetchOne(); - $this->assertEquals(2, $result); - - // execute actual code which should be tested - \OC\Files\Cache\Shared_Updater::fixBrokenSharesOnAppUpdate(); - - // check if there are just 2 shares (one gets killed by the code as there is no filecache entry for this) - $countShares = \OC_DB::prepare('SELECT COUNT(`id`) FROM `*PREFIX*share`'); - $result = $countShares->execute()->fetchOne(); - $this->assertEquals(2, $result); - - // check if the share of file '200' is removed as there is no entry for this in filecache table - $countShares = \OC_DB::prepare('SELECT COUNT(`id`) FROM `*PREFIX*share` WHERE `file_source` = 200'); - $result = $countShares->execute()->fetchOne(); - $this->assertEquals(0, $result); - - // check if there are just 2 items - $countItems = \OC_DB::prepare('SELECT COUNT(`fileid`) FROM `*PREFIX*filecache`'); - $result = $countItems->execute()->fetchOne(); - $this->assertEquals(2, $result); - } - - /** - * test update for the removal of the logical "Shared" folder. It should update - * the file_target for every share and create a physical "Shared" folder for each user - */ - function testRemoveSharedFolder() { - self::prepareDB(); - // run the update routine to remove the shared folder and replace it with a real folder - removeSharedFolder(false, 2); - - // verify results - $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*share`'); - $result = $query->execute(array()); - - $newDBContent = $result->fetchAll(); - - foreach ($newDBContent as $row) { - if ((int)$row['share_type'] === \OCP\Share::SHARE_TYPE_USER) { - $this->assertSame('/Shared', substr($row['file_target'], 0, strlen('/Shared'))); - } else { - $this->assertSame('/ShouldNotChange', $row['file_target']); - } - } - - // cleanup - $this->cleanupSharedTable(); - - } - - private function cleanupSharedTable() { - $query = \OC_DB::prepare('DELETE FROM `*PREFIX*share`'); - $query->execute(); - } - - /** - * prepare sharing table for testRemoveSharedFolder() - */ - private function prepareDB() { - $this->cleanupSharedTable(); - // add items except one - because this is the test case for the broken share table - $addItems = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (`share_type`, `item_type`, ' . - '`share_with`, `uid_owner` , `file_target`) ' . - 'VALUES (?, ?, ?, ?, ?)'); - $items = array( - array(\OCP\Share::SHARE_TYPE_USER, 'file', 'user1', 'admin' , '/foo'), - array(\OCP\Share::SHARE_TYPE_USER, 'folder', 'user2', 'admin', '/foo2'), - array(\OCP\Share::SHARE_TYPE_USER, 'file', 'user3', 'admin', '/foo3'), - array(\OCP\Share::SHARE_TYPE_USER, 'folder', 'user4', 'admin', '/foo4'), - array(\OCP\Share::SHARE_TYPE_LINK, 'file', 'user1', 'admin', '/ShouldNotChange'), - array(\OCP\Share::SHARE_TYPE_CONTACT, 'contact', 'admin', 'user1', '/ShouldNotChange'), - - ); - foreach($items as $item) { - // the number is used as path_hash - $addItems->execute($item); - } - } - - /** - * prepare file cache for testRemoveBrokenShares() - */ - private function prepareFileCache() { - // some previous tests didn't clean up and therefore this has to be done here - // FIXME: DIRTY HACK - TODO: find tests, that don't clean up and fix it there - $this->tearDown(); - - // add items except one - because this is the test case for the broken share table - $addItems = \OC_DB::prepare('INSERT INTO `*PREFIX*filecache` (`storage`, `path_hash`, ' . - '`parent`, `mimetype`, `mimepart`, `size`, `mtime`, `storage_mtime`) ' . - 'VALUES (1, ?, 1, 1, 1, 1, 1, 1)'); - $items = array(1, 3); - $fileIds = array(); - foreach($items as $item) { - // the number is used as path_hash - $addItems->execute(array($item)); - $fileIds[] = \OC_DB::insertId('*PREFIX*filecache'); - } - - $addShares = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (`file_source`, `item_type`, `uid_owner`) VALUES (?, \'file\', 1)'); - // the number is used as item_source - $addShares->execute(array($fileIds[0])); - $addShares->execute(array(200)); // id of "deleted" file - $addShares->execute(array($fileIds[1])); - } - } diff --git a/lib/private/share/share.php b/lib/private/share/share.php index 2126a1d2dd4..10b3cc34467 100644 --- a/lib/private/share/share.php +++ b/lib/private/share/share.php @@ -519,6 +519,11 @@ class Share extends \OC\Share\Constants { } } + // single file shares should never have delete permissions + if ($itemType === 'file') { + $permissions = (int)$permissions & ~\OCP\PERMISSION_DELETE; + } + // Verify share type and sharing conditions are met if ($shareType === self::SHARE_TYPE_USER) { if ($shareWith == $uidOwner) { -- cgit v1.2.3 From dea5219244b5d931db102b02d95e72f784eec7d1 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 27 May 2014 14:24:35 +0200 Subject: Fix folder name for storage root --- apps/files/lib/helper.php | 2 +- lib/private/files/fileinfo.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/apps/files/lib/helper.php b/apps/files/lib/helper.php index 7d8906e2251..b84b6c06d30 100644 --- a/apps/files/lib/helper.php +++ b/apps/files/lib/helper.php @@ -113,7 +113,7 @@ class Helper if (\OC::$server->getPreviewManager()->isMimeSupported($i['mimetype'])) { $entry['isPreviewAvailable'] = true; } - $entry['name'] = $i['name']; + $entry['name'] = $i->getName(); $entry['permissions'] = $i['permissions']; $entry['mimetype'] = $i['mimetype']; $entry['size'] = $i['size']; diff --git a/lib/private/files/fileinfo.php b/lib/private/files/fileinfo.php index e7afeb4ccce..d012c0c5a63 100644 --- a/lib/private/files/fileinfo.php +++ b/lib/private/files/fileinfo.php @@ -108,7 +108,7 @@ class FileInfo implements \OCP\Files\FileInfo, \ArrayAccess { * @return string */ public function getName() { - return $this->data['name']; + return basename($this->getPath()); } /** -- cgit v1.2.3 From 329bfd81c33ed95fdc91658cd914611605cd114f Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Tue, 27 May 2014 15:09:43 +0200 Subject: remove encryption keys if user unshares a file --- apps/files_encryption/hooks/hooks.php | 55 +++++++++++++++++++++++++++++++++++ apps/files_encryption/lib/helper.php | 2 ++ apps/files_encryption/tests/hooks.php | 6 ++-- lib/private/files/view.php | 13 ++++++++- 4 files changed, 72 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/apps/files_encryption/hooks/hooks.php b/apps/files_encryption/hooks/hooks.php index 66e2bccd59f..99edcf25ec5 100644 --- a/apps/files_encryption/hooks/hooks.php +++ b/apps/files_encryption/hooks/hooks.php @@ -34,6 +34,8 @@ class Hooks { private static $renamedFiles = array(); // file for which we want to delete the keys after the delete operation was successful private static $deleteFiles = array(); + // file for which we want to delete the keys after the delete operation was successful + private static $umountedFiles = array(); /** * Startup encryption backend upon user login @@ -610,4 +612,57 @@ class Hooks { 'path' => $ownerPath); } + /** + * remember files/folders which get unmounted + */ + public static function preUmount($params) { + $path = $params[\OC\Files\Filesystem::signal_param_path]; + $user = \OCP\USER::getUser(); + + $view = new \OC\Files\View(); + $itemType = $view->is_dir('/' . $user . '/files' . $path) ? 'folder' : 'file'; + + $util = new Util($view, $user); + list($owner, $ownerPath) = $util->getUidAndFilename($path); + + self::$umountedFiles[$params[\OC\Files\Filesystem::signal_param_path]] = array( + 'uid' => $owner, + 'path' => $ownerPath, + 'itemType' => $itemType); + } + + public static function postUmount($params) { + + if (!isset(self::$umountedFiles[$params[\OC\Files\Filesystem::signal_param_path]])) { + return true; + } + + $umountedFile = self::$umountedFiles[$params[\OC\Files\Filesystem::signal_param_path]]; + $path = $umountedFile['path']; + $user = $umountedFile['uid']; + $itemType = $umountedFile['itemType']; + + $view = new \OC\Files\View(); + $util = new Util($view, $user); + + // we don't need to remember the file any longer + unset(self::$umountedFiles[$params[\OC\Files\Filesystem::signal_param_path]]); + + // if we unshare a folder we need a list of all (sub-)files + if ($itemType === 'folder') { + $allFiles = $util->getAllFiles($path); + } else { + $allFiles = array($path); + } + + foreach ($allFiles as $path) { + + // check if the user still has access to the file, otherwise delete share key + $sharingUsers = $result = \OCP\Share::getUsersSharingFile($path, $user); + if (!in_array(\OCP\User::getUser(), $sharingUsers['users'])) { + Keymanager::delShareKey($view, array(\OCP\User::getUser()), $path); + } + } + } + } diff --git a/apps/files_encryption/lib/helper.php b/apps/files_encryption/lib/helper.php index 564e97e0592..2684bf7be33 100755 --- a/apps/files_encryption/lib/helper.php +++ b/apps/files_encryption/lib/helper.php @@ -65,6 +65,8 @@ class Helper { \OCP\Util::connectHook('OC_Filesystem', 'post_rename', 'OCA\Encryption\Hooks', 'postRename'); \OCP\Util::connectHook('OC_Filesystem', 'post_delete', 'OCA\Encryption\Hooks', 'postDelete'); \OCP\Util::connectHook('OC_Filesystem', 'delete', 'OCA\Encryption\Hooks', 'preDelete'); + \OCP\Util::connectHook('OC_Filesystem', 'post_umount', 'OCA\Encryption\Hooks', 'postUmount'); + \OCP\Util::connectHook('OC_Filesystem', 'umount', 'OCA\Encryption\Hooks', 'preUmount'); } /** diff --git a/apps/files_encryption/tests/hooks.php b/apps/files_encryption/tests/hooks.php index 95f5996bb8e..a2e3ea30f04 100644 --- a/apps/files_encryption/tests/hooks.php +++ b/apps/files_encryption/tests/hooks.php @@ -257,14 +257,14 @@ class Test_Encryption_Hooks extends \PHPUnit_Framework_TestCase { $this->assertTrue($result); - // now keys from user1s home should be gone - $this->assertFalse($this->rootView->file_exists( + // share key for user2 from user1s home should be gone, all other keys should still exists + $this->assertTrue($this->rootView->file_exists( self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/share-keys/' . $this->filename . '.' . \Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER1 . '.shareKey')); $this->assertFalse($this->rootView->file_exists( self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/share-keys/' . $this->filename . '.' . \Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER2 . '.shareKey')); - $this->assertFalse($this->rootView->file_exists( + $this->assertTrue($this->rootView->file_exists( self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keyfiles/' . $this->filename . '.key')); // cleanup diff --git a/lib/private/files/view.php b/lib/private/files/view.php index 1515769116f..9b6a370fe3b 100644 --- a/lib/private/files/view.php +++ b/lib/private/files/view.php @@ -361,7 +361,18 @@ class View { $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); if ($mount->getInternalPath($absolutePath) === '') { if ($mount instanceof MoveableMount) { - return $mount->removeMount(); + \OC_Hook::emit( + Filesystem::CLASSNAME, "umount", + array(Filesystem::signal_param_path => $path) + ); + $result = $mount->removeMount(); + if ($result) { + \OC_Hook::emit( + Filesystem::CLASSNAME, "post_umount", + array(Filesystem::signal_param_path => $path) + ); + } + return $result; } else { // do not allow deleting the storage's root / the mount point // because for some storages it might delete the whole contents -- cgit v1.2.3 From b0a74edbb2fe91a8f82bb7437ff1a9ca4c3a03ec Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Tue, 27 May 2014 20:48:19 +0200 Subject: allow moving files to the root --- lib/private/connector/sabre/objecttree.php | 4 ---- 1 file changed, 4 deletions(-) (limited to 'lib') diff --git a/lib/private/connector/sabre/objecttree.php b/lib/private/connector/sabre/objecttree.php index a3de2efaa50..c55a392bca0 100644 --- a/lib/private/connector/sabre/objecttree.php +++ b/lib/private/connector/sabre/objecttree.php @@ -126,10 +126,6 @@ class ObjectTree extends \Sabre\DAV\ObjectTree { throw new \Sabre\DAV\Exception\Forbidden(); } if ($sourceDir !== $destinationDir) { - // for a full move we need update privileges on sourcePath and sourceDir as well as destinationDir - if (ltrim($destinationDir, '/') === '') { - throw new \Sabre\DAV\Exception\Forbidden(); - } if (!$this->fileView->isUpdatable($sourceDir)) { throw new \Sabre\DAV\Exception\Forbidden(); } -- cgit v1.2.3 From 42a362f2491e5731ce7cc242891c58dd41a76d00 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 28 May 2014 13:47:50 +0200 Subject: Fix phpdoc and comments --- apps/files_sharing/lib/sharedmount.php | 4 ++-- apps/files_sharing/lib/sharedstorage.php | 1 + apps/files_sharing/tests/share.php | 2 +- lib/private/files/mount/mount.php | 1 + 4 files changed, 5 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/apps/files_sharing/lib/sharedmount.php b/apps/files_sharing/lib/sharedmount.php index 84eb4e78773..79d6991264e 100644 --- a/apps/files_sharing/lib/sharedmount.php +++ b/apps/files_sharing/lib/sharedmount.php @@ -1,6 +1,6 @@ + * Copyright (c) 2014 Robin Appelman * This file is licensed under the Affero General Public License version 3 or * later. * See the COPYING-README file. @@ -14,7 +14,7 @@ use OC\Files\Mount\MoveableMount; use OC\Files\Storage\Shared; /** - * Person mount points can be moved by the user + * Shared mount points can be moved by the user */ class SharedMount extends Mount implements MoveableMount { /** diff --git a/apps/files_sharing/lib/sharedstorage.php b/apps/files_sharing/lib/sharedstorage.php index c98cde0a8e9..ea0e420a5c4 100644 --- a/apps/files_sharing/lib/sharedstorage.php +++ b/apps/files_sharing/lib/sharedstorage.php @@ -293,6 +293,7 @@ class Shared extends \OC\Files\Storage\Common { $relPath1 = $this->getMountPoint() . '/' . $path1; $relPath2 = $this->getMountPoint() . '/' . $path2; + // check for update permissions on the share if ($this->isUpdatable('')) { $pathinfo = pathinfo($relPath1); diff --git a/apps/files_sharing/tests/share.php b/apps/files_sharing/tests/share.php index a81f84ef061..195fac461be 100644 --- a/apps/files_sharing/tests/share.php +++ b/apps/files_sharing/tests/share.php @@ -3,7 +3,7 @@ * ownCloud * * @author Bjoern Schiessle - * @copyright 2013 Bjoern Schiessle + * @copyright 2014 Bjoern Schiessle * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE diff --git a/lib/private/files/mount/mount.php b/lib/private/files/mount/mount.php index 7561cd7311b..04bccbcab87 100644 --- a/lib/private/files/mount/mount.php +++ b/lib/private/files/mount/mount.php @@ -142,6 +142,7 @@ class Mount { } else { $internalPath = substr($path, strlen($this->mountPoint)); } + // substr returns false instead of an empty string, we always want a string return (string)$internalPath; } -- cgit v1.2.3 From a432459685a5afb3a9bb844f2bfab2c652fc0d4b Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 28 May 2014 13:52:18 +0200 Subject: use triple equals --- lib/private/files/view.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/private/files/view.php b/lib/private/files/view.php index 9b6a370fe3b..fac1e64e79e 100644 --- a/lib/private/files/view.php +++ b/lib/private/files/view.php @@ -430,7 +430,7 @@ class View { $storage1 = $mount->getStorage(); $internalPath1 = $mount->getInternalPath($absolutePath1 . $postFix1); list(, $internalPath2) = Filesystem::resolvePath($absolutePath2 . $postFix2); - if ($internalPath1 == '' and $mount instanceof MoveableMount) { + if ($internalPath1 === '' and $mount instanceof MoveableMount) { /** * @var \OC\Files\Mount\Mount | \OC\Files\Mount\MoveableMount $mount */ -- cgit v1.2.3 From e362373a30198b0dac6fbc9cd751a5eafe19e21a Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 28 May 2014 14:01:40 +0200 Subject: Movable storage root can always be moved and deleted --- lib/private/files/view.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/private/files/view.php b/lib/private/files/view.php index fac1e64e79e..d42f6cbf9fe 100644 --- a/lib/private/files/view.php +++ b/lib/private/files/view.php @@ -966,7 +966,7 @@ class View { // do not allow renaming/deleting the mount point if they are not shared files/folders // for shared files/folders we use the permissions given by the owner if ($mount instanceof MoveableMount) { - $rootEntry['permissions'] = $permissions; + $rootEntry['permissions'] = $permissions | \OCP\PERMISSION_UPDATE | \OCP\PERMISSION_DELETE; } else { $rootEntry['permissions'] = $permissions & (\OCP\PERMISSION_ALL - (\OCP\PERMISSION_UPDATE | \OCP\PERMISSION_DELETE)); } -- cgit v1.2.3 From 86d7371d0c797d5c97198eebf9152ad748961927 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Wed, 4 Jun 2014 18:41:47 +0200 Subject: fix unshareFromSelf() --- apps/files_sharing/lib/sharedstorage.php | 21 ++++++----- apps/files_sharing/tests/share.php | 65 ++++++++++++++++++++++++++++++++ lib/private/share/share.php | 61 +++++++++++++++++++----------- 3 files changed, 115 insertions(+), 32 deletions(-) (limited to 'lib') diff --git a/apps/files_sharing/lib/sharedstorage.php b/apps/files_sharing/lib/sharedstorage.php index 6b2873302a5..59de2dfa4c4 100644 --- a/apps/files_sharing/lib/sharedstorage.php +++ b/apps/files_sharing/lib/sharedstorage.php @@ -403,15 +403,18 @@ class Shared extends \OC\Files\Storage\Common { || $shares ) { foreach ($shares as $share) { - $mount = new SharedMount( - '\OC\Files\Storage\Shared', - $options['user_dir'] . '/' . $share['file_target'], - array( - 'share' => $share, - ), - $loader - ); - $manager->addMount($mount); + // don't mount shares where we have no permissions + if ($share['permissions'] > 0) { + $mount = new SharedMount( + '\OC\Files\Storage\Shared', + $options['user_dir'] . '/' . $share['file_target'], + array( + 'share' => $share, + ), + $loader + ); + $manager->addMount($mount); + } } } } diff --git a/apps/files_sharing/tests/share.php b/apps/files_sharing/tests/share.php index 195fac461be..d3ca1816c75 100644 --- a/apps/files_sharing/tests/share.php +++ b/apps/files_sharing/tests/share.php @@ -60,6 +60,71 @@ class Test_Files_Sharing extends Test_Files_Sharing_Base { parent::tearDown(); } + function testUnshareFromSelf() { + + \OC_Group::createGroup('testGroup'); + \OC_Group::addToGroup(self::TEST_FILES_SHARING_API_USER2, 'testGroup'); + \OC_Group::addToGroup(self::TEST_FILES_SHARING_API_USER3, 'testGroup'); + + $fileinfo = $this->view->getFileInfo($this->filename); + + $pathinfo = pathinfo($this->filename); + + $duplicate = '/' . $pathinfo['filename'] . ' (2).' . $pathinfo['extension']; + + $result = \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER, + \Test_Files_Sharing::TEST_FILES_SHARING_API_USER2, 31); + + $this->assertTrue($result); + + $result = \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, + 'testGroup', 31); + + $this->assertTrue($result); + + self::loginHelper(self::TEST_FILES_SHARING_API_USER2); + $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename)); + $this->assertTrue(\OC\Files\Filesystem::file_exists($duplicate)); + + self::loginHelper(self::TEST_FILES_SHARING_API_USER3); + $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename)); + $this->assertFalse(\OC\Files\Filesystem::file_exists($duplicate)); + + self::loginHelper(self::TEST_FILES_SHARING_API_USER2); + \OC\Files\Filesystem::unlink($this->filename); + self::loginHelper(self::TEST_FILES_SHARING_API_USER2); + $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename)); + $this->assertTrue(\OC\Files\Filesystem::file_exists($duplicate)); + + // for user3 nothing should change + self::loginHelper(self::TEST_FILES_SHARING_API_USER3); + $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename)); + $this->assertFalse(\OC\Files\Filesystem::file_exists($duplicate)); + + self::loginHelper(self::TEST_FILES_SHARING_API_USER2); + \OC\Files\Filesystem::unlink($duplicate); + self::loginHelper(self::TEST_FILES_SHARING_API_USER2); + $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename)); + $this->assertFalse(\OC\Files\Filesystem::file_exists($duplicate)); + + // for user3 nothing should change + self::loginHelper(self::TEST_FILES_SHARING_API_USER3); + $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename)); + $this->assertFalse(\OC\Files\Filesystem::file_exists($duplicate)); + + //cleanup + self::loginHelper(self::TEST_FILES_SHARING_API_USER1); + \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, + 'testGroup'); + \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER, + self::TEST_FILES_SHARING_API_USER2); + \OC_Group::removeFromGroup(self::TEST_FILES_SHARING_API_USER2, 'testGroup'); + \OC_Group::removeFromGroup(self::TEST_FILES_SHARING_API_USER2, 'testGroup'); + \OC_Group::deleteGroup('testGroup'); + + + } + /** * shared files should never have delete permissions * @dataProvider DataProviderTestFileSharePermissions diff --git a/lib/private/share/share.php b/lib/private/share/share.php index 10b3cc34467..646405a37b5 100644 --- a/lib/private/share/share.php +++ b/lib/private/share/share.php @@ -717,33 +717,48 @@ class Share extends \OC\Share\Constants { * Unsharing from self is not allowed for items inside collections */ public static function unshareFromSelf($itemType, $itemTarget) { - $item = self::getItemSharedWith($itemType, $itemTarget); - if (!empty($item)) { - if ((int)$item['share_type'] === self::SHARE_TYPE_GROUP) { - // Insert an extra row for the group share and set permission - // to 0 to prevent it from showing up for the user - $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share`' + + if ($itemType === 'file' || $itemType === 'folder') { + $statement = 'SELECT * FROM `*PREFIX*share` WHERE `item_type` = ? and `file_target` = ?'; + } else { + $statement = 'SELECT * FROM `*PREFIX*share` WHERE `item_type` = ? and `item_target` = ?'; + } + + $query = \OCP\DB::prepare($statement); + $result = $query->execute(array($itemType, $itemTarget)); + + $shares = $result->fetchAll(); + + $itemUnshared = false; + foreach ($shares as $share) { + if ((int)$share['share_type'] === \OCP\Share::SHARE_TYPE_USER) { + Helper::delete($share['id']); + $itemUnshared = true; + break; + } elseif ((int)$share['share_type'] === \OCP\Share::SHARE_TYPE_GROUP) { + $groupShare = $share; + } elseif ((int)$share['share_type'] === self::$shareTypeGroupUserUnique) { + $uniqueGroupShare = $share; + } + } + + if (!$itemUnshared && isset($groupShare)) { + $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share`' .' (`item_type`, `item_source`, `item_target`, `parent`, `share_type`,' .' `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`, `file_target`)' .' VALUES (?,?,?,?,?,?,?,?,?,?,?)'); - $query->execute(array($item['item_type'], $item['item_source'], $item['item_target'], - $item['id'], self::$shareTypeGroupUserUnique, - \OC_User::getUser(), $item['uid_owner'], 0, $item['stime'], $item['file_source'], - $item['file_target'])); - \OC_DB::insertid('*PREFIX*share'); - // Delete all reshares by this user of the group share - Helper::delete($item['id'], true, \OC_User::getUser()); - } else if ((int)$item['share_type'] === self::$shareTypeGroupUserUnique) { - // Set permission to 0 to prevent it from showing up for the user - $query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `permissions` = ? WHERE `id` = ?'); - $query->execute(array(0, $item['id'])); - Helper::delete($item['id'], true); - } else { - Helper::delete($item['id']); - } - return true; + $query->execute(array($groupShare['item_type'], $groupShare['item_source'], $groupShare['item_target'], + $groupShare['id'], self::$shareTypeGroupUserUnique, + \OC_User::getUser(), $groupShare['uid_owner'], 0, $groupShare['stime'], $groupShare['file_source'], + $groupShare['file_target'])); + $itemUnshared = true; + } elseif (!$itemUnshared && isset($uniqueGroupShare)) { + $query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `permissions` = ? WHERE `id` = ?'); + $query->execute(array(0, $uniqueGroupShare['id'])); + $itemUnshared = true; } - return false; + + return $itemUnshared; } /** -- cgit v1.2.3 From 5bbfdc35d7d26625f406e24149f1a45708049e34 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Wed, 4 Jun 2014 19:07:56 +0200 Subject: make sure that we remove all shares refering to the same parent --- lib/private/share/helper.php | 21 --------------------- 1 file changed, 21 deletions(-) (limited to 'lib') diff --git a/lib/private/share/helper.php b/lib/private/share/helper.php index ab9e0ca4926..c92aa15b4bf 100644 --- a/lib/private/share/helper.php +++ b/lib/private/share/helper.php @@ -166,27 +166,6 @@ class Helper extends \OC\Share\Constants { // Reset parents array, only go through loop again if items are found $parents = array(); while ($item = $result->fetchRow()) { - // Search for a duplicate parent share, this occurs when an - // item is shared to the same user through a group and user or the - // same item is shared by different users - $userAndGroups = array_merge(array($item['uid_owner']), \OC_Group::getUserGroups($item['uid_owner'])); - $query = \OC_DB::prepare('SELECT `id`, `permissions` FROM `*PREFIX*share`' - .' WHERE `item_type` = ?' - .' AND `item_target` = ?' - .' AND `share_type` IN (?,?,?)' - .' AND `share_with` IN (\''.implode('\',\'', $userAndGroups).'\')' - .' AND `uid_owner` != ? AND `id` != ?'); - $duplicateParent = $query->execute(array($item['item_type'], $item['item_target'], - self::SHARE_TYPE_USER, self::SHARE_TYPE_GROUP, self::$shareTypeGroupUserUnique, - $item['uid_owner'], $item['parent']))->fetchRow(); - if ($duplicateParent) { - // Change the parent to the other item id if share permission is granted - if ($duplicateParent['permissions'] & \OCP\PERMISSION_SHARE) { - $query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `parent` = ? WHERE `id` = ?'); - $query->execute(array($duplicateParent['id'], $item['id'])); - continue; - } - } $ids[] = $item['id']; $parents[] = $item['id']; } -- cgit v1.2.3 From eec1c4398aeee1cda8a26a3a325fd16148473d57 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Wed, 4 Jun 2014 19:45:10 +0200 Subject: only unshare user share if it was shared with the current user --- lib/private/share/share.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/private/share/share.php b/lib/private/share/share.php index 646405a37b5..172e61c5dc8 100644 --- a/lib/private/share/share.php +++ b/lib/private/share/share.php @@ -731,7 +731,8 @@ class Share extends \OC\Share\Constants { $itemUnshared = false; foreach ($shares as $share) { - if ((int)$share['share_type'] === \OCP\Share::SHARE_TYPE_USER) { + if ((int)$share['share_type'] === \OCP\Share::SHARE_TYPE_USER && + $share['share_with'] === \OCP\User::getUser()) { Helper::delete($share['id']); $itemUnshared = true; break; -- cgit v1.2.3 From 3459f99dd6b737d73881504fd7103679fc1d7fe6 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Thu, 5 Jun 2014 00:37:41 +0200 Subject: make sure that we only delete shares for the current user --- lib/private/share/share.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/private/share/share.php b/lib/private/share/share.php index 172e61c5dc8..f02eacfed74 100644 --- a/lib/private/share/share.php +++ b/lib/private/share/share.php @@ -718,6 +718,8 @@ class Share extends \OC\Share\Constants { */ public static function unshareFromSelf($itemType, $itemTarget) { + $uid = \OCP\User::getUser(); + if ($itemType === 'file' || $itemType === 'folder') { $statement = 'SELECT * FROM `*PREFIX*share` WHERE `item_type` = ? and `file_target` = ?'; } else { @@ -732,13 +734,16 @@ class Share extends \OC\Share\Constants { $itemUnshared = false; foreach ($shares as $share) { if ((int)$share['share_type'] === \OCP\Share::SHARE_TYPE_USER && - $share['share_with'] === \OCP\User::getUser()) { + $share['share_with'] === $uid) { Helper::delete($share['id']); $itemUnshared = true; break; } elseif ((int)$share['share_type'] === \OCP\Share::SHARE_TYPE_GROUP) { - $groupShare = $share; - } elseif ((int)$share['share_type'] === self::$shareTypeGroupUserUnique) { + if (\OC_Group::inGroup($uid, $share['share_with'])) { + $groupShare = $share; + } + } elseif ((int)$share['share_type'] === self::$shareTypeGroupUserUnique && + $share['share_with'] === $uid) { $uniqueGroupShare = $share; } } -- cgit v1.2.3 From 46f70aabfd399e1bc5517b2192ac2489189f9d94 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 5 Jun 2014 21:08:30 +0200 Subject: Dont connect hooks twice --- lib/private/hook.php | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'lib') diff --git a/lib/private/hook.php b/lib/private/hook.php index 30e22847c7f..c9ca58f779e 100644 --- a/lib/private/hook.php +++ b/lib/private/hook.php @@ -31,6 +31,12 @@ class OC_Hook{ self::$registered[$signalclass][$signalname] = array(); } + // dont connect hooks twice + foreach (self::$registered[$signalclass][$signalname] as $hook) { + if ($hook['class'] === $slotclass and $hook['name'] === $slotname) { + return false; + } + } // Connect the hook handler to the requested emitter self::$registered[$signalclass][$signalname][] = array( "class" => $slotclass, -- cgit v1.2.3