diff options
-rw-r--r-- | apps/files_sharing/ajax/shareinfo.php | 2 | ||||
-rw-r--r-- | apps/files_sharing/lib/readonlywrapper.php | 74 | ||||
-rw-r--r-- | apps/files_sharing/publicwebdav.php | 2 | ||||
-rw-r--r-- | apps/files_versions/appinfo/register_command.php | 28 | ||||
-rw-r--r-- | apps/files_versions/command/cleanup.php | 114 | ||||
-rw-r--r-- | apps/files_versions/tests/command/cleanuptest.php | 162 | ||||
-rw-r--r-- | lib/private/files/storage/polyfill/copydirectory.php | 89 | ||||
-rw-r--r-- | tests/lib/files/storage/copydirectory.php | 46 |
8 files changed, 441 insertions, 76 deletions
diff --git a/apps/files_sharing/ajax/shareinfo.php b/apps/files_sharing/ajax/shareinfo.php index 3ea0fae8522..db6194d3f05 100644 --- a/apps/files_sharing/ajax/shareinfo.php +++ b/apps/files_sharing/ajax/shareinfo.php @@ -55,7 +55,7 @@ $path = $data['realPath']; $isWritable = $linkItem['permissions'] & (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_CREATE); if (!$isWritable) { \OC\Files\Filesystem::addStorageWrapper('readonly', function ($mountPoint, $storage) { - return new \OCA\Files_Sharing\ReadOnlyWrapper(array('storage' => $storage)); + return new \OC\Files\Storage\Wrapper\PermissionsMask(array('storage' => $storage, 'mask' => \OCP\Constants::PERMISSION_READ + \OCP\Constants::PERMISSION_SHARE)); }); } diff --git a/apps/files_sharing/lib/readonlywrapper.php b/apps/files_sharing/lib/readonlywrapper.php deleted file mode 100644 index a5d84f7f5a2..00000000000 --- a/apps/files_sharing/lib/readonlywrapper.php +++ /dev/null @@ -1,74 +0,0 @@ -<?php -/** - * @author Joas Schilling <nickvergessen@owncloud.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <icewind@owncloud.com> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @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 OC\Files\Cache\Wrapper\CachePermissionsMask; -use OC\Files\Storage\Wrapper\Wrapper; -use OCP\Constants; - -class ReadOnlyWrapper extends Wrapper { - public function isUpdatable($path) { - return false; - } - - public function isCreatable($path) { - return false; - } - - public function isDeletable($path) { - return false; - } - - public function getPermissions($path) { - return $this->storage->getPermissions($path) & (\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE); - } - - public function rename($path1, $path2) { - return false; - } - - public function touch($path, $mtime = null) { - return false; - } - - public function mkdir($path) { - return false; - } - - public function rmdir($path) { - return false; - } - - public function unlink($path) { - return false; - } - - public function getCache($path = '', $storage = null) { - if (!$storage) { - $storage = $this; - } - $sourceCache = $this->storage->getCache($path, $storage); - return new CachePermissionsMask($sourceCache, Constants::PERMISSION_READ | Constants::PERMISSION_SHARE); - } -} diff --git a/apps/files_sharing/publicwebdav.php b/apps/files_sharing/publicwebdav.php index be7530897f6..c0a9dc328d1 100644 --- a/apps/files_sharing/publicwebdav.php +++ b/apps/files_sharing/publicwebdav.php @@ -64,7 +64,7 @@ $server->on('beforeMethod', function () use ($server, $objectTree, $authBackend) if (!$isWritable) { \OC\Files\Filesystem::addStorageWrapper('readonly', function ($mountPoint, $storage) { - return new \OCA\Files_Sharing\ReadOnlyWrapper(array('storage' => $storage)); + return new \OC\Files\Storage\Wrapper\PermissionsMask(array('storage' => $storage, 'mask' => \OCP\Constants::PERMISSION_READ + \OCP\Constants::PERMISSION_SHARE)); }); } diff --git a/apps/files_versions/appinfo/register_command.php b/apps/files_versions/appinfo/register_command.php new file mode 100644 index 00000000000..49efdbe6190 --- /dev/null +++ b/apps/files_versions/appinfo/register_command.php @@ -0,0 +1,28 @@ +<?php +/** + * @author Björn Schießle <schiessle@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @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_Versions\Command\CleanUp; + +$userManager = OC::$server->getUserManager(); +$rootFolder = \OC::$server->getRootFolder(); +/** @var Symfony\Component\Console\Application $application */ +$application->add(new CleanUp($rootFolder, $userManager)); diff --git a/apps/files_versions/command/cleanup.php b/apps/files_versions/command/cleanup.php new file mode 100644 index 00000000000..bed6dd01773 --- /dev/null +++ b/apps/files_versions/command/cleanup.php @@ -0,0 +1,114 @@ +<?php +/** + * @author Björn Schießle <schiessle@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @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_Versions\Command; + + +use OCP\Files\IRootFolder; +use OCP\IUserBackend; +use OCP\IUserManager; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class CleanUp extends Command { + + /** @var IUserManager */ + protected $userManager; + + /** @var IRootFolder */ + protected $rootFolder; + + /** + * @param IRootFolder $rootFolder + * @param IUserManager $userManager + */ + function __construct(IRootFolder $rootFolder, IUserManager $userManager) { + parent::__construct(); + $this->userManager = $userManager; + $this->rootFolder = $rootFolder; + } + + protected function configure() { + $this + ->setName('versions:cleanup') + ->setDescription('Delete versions') + ->addArgument( + 'user_id', + InputArgument::OPTIONAL | InputArgument::IS_ARRAY, + 'delete versions of the given user(s), if no user is given all versions will be deleted' + ); + } + + + protected function execute(InputInterface $input, OutputInterface $output) { + + $users = $input->getArgument('user_id'); + if (!empty($users)) { + foreach ($users as $user) { + if ($this->userManager->userExists($user)) { + $output->writeln("Delete versions of <info>$user</info>"); + $this->deleteVersions($user); + } else { + $output->writeln("<error>Unknown user $user</error>"); + } + } + } else { + $output->writeln('Delete all versions'); + foreach ($this->userManager->getBackends() as $backend) { + $name = get_class($backend); + + if ($backend instanceof IUserBackend) { + $name = $backend->getBackendName(); + } + + $output->writeln("Delete versions for users on backend <info>$name</info>"); + + $limit = 500; + $offset = 0; + do { + $users = $backend->getUsers('', $limit, $offset); + foreach ($users as $user) { + $output->writeln(" <info>$user</info>"); + $this->deleteVersions($user); + } + $offset += $limit; + } while (count($users) >= $limit); + } + } + } + + + /** + * delete versions for the given user + * + * @param string $user + */ + protected function deleteVersions($user) { + \OC_Util::tearDownFS(); + \OC_Util::setupFS($user); + if ($this->rootFolder->nodeExists('/' . $user . '/files_versions')) { + $this->rootFolder->get('/' . $user . '/files_versions')->delete(); + } + } + +} diff --git a/apps/files_versions/tests/command/cleanuptest.php b/apps/files_versions/tests/command/cleanuptest.php new file mode 100644 index 00000000000..bfde25d75ce --- /dev/null +++ b/apps/files_versions/tests/command/cleanuptest.php @@ -0,0 +1,162 @@ +<?php +/** + * @author Björn Schießle <schiessle@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @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_Versions\Tests\Command; + + +use OCA\Files_Versions\Command\CleanUp; +use Test\TestCase; +use OC\User\Manager; +use OCP\Files\IRootFolder; + +class CleanupTest extends TestCase { + + /** @var CleanUp */ + protected $cleanup; + + /** @var \PHPUnit_Framework_MockObject_MockObject | Manager */ + protected $userManager; + + /** @var \PHPUnit_Framework_MockObject_MockObject | IRootFolder */ + protected $rootFolder; + + public function setUp() { + parent::setUp(); + + $this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder') + ->disableOriginalConstructor()->getMock(); + $this->userManager = $this->getMockBuilder('OC\User\Manager') + ->disableOriginalConstructor()->getMock(); + + + $this->cleanup = new CleanUp($this->rootFolder, $this->userManager); + } + + /** + * @dataProvider dataTestDeleteVersions + * @param boolean $nodeExists + */ + public function testDeleteVersions($nodeExists) { + + $this->rootFolder->expects($this->once()) + ->method('nodeExists') + ->with('/testUser/files_versions') + ->willReturn($nodeExists); + + + if($nodeExists) { + $this->rootFolder->expects($this->once()) + ->method('get') + ->with('/testUser/files_versions') + ->willReturn($this->rootFolder); + $this->rootFolder->expects($this->once()) + ->method('delete'); + } else { + $this->rootFolder->expects($this->never()) + ->method('get'); + $this->rootFolder->expects($this->never()) + ->method('delete'); + } + + $this->invokePrivate($this->cleanup, 'deleteVersions', ['testUser']); + } + + public function dataTestDeleteVersions() { + return array( + array(true), + array(false) + ); + } + + + /** + * test delete versions from users given as parameter + */ + public function testExecuteDeleteListOfUsers() { + $userIds = ['user1', 'user2', 'user3']; + + $instance = $this->getMockBuilder('OCA\Files_Versions\Command\CleanUp') + ->setMethods(['deleteVersions']) + ->setConstructorArgs([$this->rootFolder, $this->userManager]) + ->getMock(); + $instance->expects($this->exactly(count($userIds))) + ->method('deleteVersions') + ->willReturnCallback(function ($user) use ($userIds) { + $this->assertTrue(in_array($user, $userIds)); + }); + + $this->userManager->expects($this->exactly(count($userIds))) + ->method('userExists')->willReturn(true); + + $inputInterface = $this->getMockBuilder('\Symfony\Component\Console\Input\InputInterface') + ->disableOriginalConstructor()->getMock(); + $inputInterface->expects($this->once())->method('getArgument') + ->with('user_id') + ->willReturn($userIds); + + $outputInterface = $this->getMockBuilder('\Symfony\Component\Console\Output\OutputInterface') + ->disableOriginalConstructor()->getMock(); + + $this->invokePrivate($instance, 'execute', [$inputInterface, $outputInterface]); + } + + /** + * test delete versions of all users + */ + public function testExecuteAllUsers() { + $userIds = []; + $backendUsers = ['user1', 'user2']; + + $instance = $this->getMockBuilder('OCA\Files_Versions\Command\CleanUp') + ->setMethods(['deleteVersions']) + ->setConstructorArgs([$this->rootFolder, $this->userManager]) + ->getMock(); + + $backend = $this->getMockBuilder('OC_User_Interface') + ->disableOriginalConstructor()->getMock(); + $backend->expects($this->once())->method('getUsers') + ->with('', 500, 0) + ->willReturn($backendUsers); + + $instance->expects($this->exactly(count($backendUsers))) + ->method('deleteVersions') + ->willReturnCallback(function ($user) use ($backendUsers) { + $this->assertTrue(in_array($user, $backendUsers)); + }); + + $inputInterface = $this->getMockBuilder('\Symfony\Component\Console\Input\InputInterface') + ->disableOriginalConstructor()->getMock(); + $inputInterface->expects($this->once())->method('getArgument') + ->with('user_id') + ->willReturn($userIds); + + $outputInterface = $this->getMockBuilder('\Symfony\Component\Console\Output\OutputInterface') + ->disableOriginalConstructor()->getMock(); + + $this->userManager->expects($this->once()) + ->method('getBackends') + ->willReturn([$backend]); + + $this->invokePrivate($instance, 'execute', [$inputInterface, $outputInterface]); + } + +} diff --git a/lib/private/files/storage/polyfill/copydirectory.php b/lib/private/files/storage/polyfill/copydirectory.php new file mode 100644 index 00000000000..1b4873a3a76 --- /dev/null +++ b/lib/private/files/storage/polyfill/copydirectory.php @@ -0,0 +1,89 @@ +<?php +/** + * Copyright (c) 2015 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Files\Storage\PolyFill; + +trait CopyDirectory { + /** + * Check if a path is a directory + * + * @param string $path + * @return bool + */ + abstract public function is_dir($path); + + /** + * Check if a file or folder exists + * + * @param string $path + * @return bool + */ + abstract public function file_exists($path); + + /** + * Delete a file or folder + * + * @param string $path + * @return bool + */ + abstract public function unlink($path); + + /** + * Open a directory handle for a folder + * + * @param string $path + * @return resource | bool + */ + abstract public function opendir($path); + + /** + * Create a new folder + * + * @param string $path + * @return bool + */ + abstract public function mkdir($path); + + public function copy($source, $target) { + if ($this->is_dir($source)) { + if ($this->file_exists($target)) { + $this->unlink($target); + } + $this->mkdir($target); + return $this->copyRecursive($source, $target); + } else { + return parent::copy($source, $target); + } + } + + /** + * For adapters that dont support copying folders natively + * + * @param $source + * @param $target + * @return bool + */ + protected function copyRecursive($source, $target) { + $dh = $this->opendir($source); + $result = true; + while ($file = readdir($dh)) { + if ($file !== '.' and $file !== '..') { + if ($this->is_dir($source . '/' . $file)) { + $this->mkdir($target . '/' . $file); + $result = $this->copyRecursive($source . '/' . $file, $target . '/' . $file); + } else { + $result = parent::copy($source . '/' . $file, $target . '/' . $file); + } + if (!$result) { + break; + } + } + } + return $result; + } +} diff --git a/tests/lib/files/storage/copydirectory.php b/tests/lib/files/storage/copydirectory.php new file mode 100644 index 00000000000..3338747f49b --- /dev/null +++ b/tests/lib/files/storage/copydirectory.php @@ -0,0 +1,46 @@ +<?php +/** + * @author Robin Appelman <icewind@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @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 Test\Files\Storage; + +use OC\Files\Storage\Temporary; + +class StorageNoRecursiveCopy extends Temporary { + public function copy($path1, $path2) { + if ($this->is_dir($path1)) { + return false; + } + return copy($this->getSourcePath($path1), $this->getSourcePath($path2)); + } +} + +class CopyDirectoryStorage extends StorageNoRecursiveCopy { + use \OC\Files\Storage\PolyFill\CopyDirectory; +} + +class CopyDirectory extends Storage { + + protected function setUp() { + parent::setUp(); + $this->instance = new CopyDirectoryStorage([]); + } +} + |