From 328b3c47d7fdf61b6de3c2d9129d392d3a9f5a87 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 13 May 2016 11:22:28 +0200 Subject: Move stuff from outside lib/ to PSR-4 --- apps/files_external/appinfo/app.php | 2 +- apps/files_external/appinfo/application.php | 135 ----- apps/files_external/appinfo/info.xml | 2 + apps/files_external/appinfo/register_command.php | 6 +- apps/files_external/appinfo/routes.php | 2 - apps/files_external/appinfo/update.php | 4 +- apps/files_external/command/applicable.php | 157 ----- apps/files_external/command/backends.php | 112 ---- apps/files_external/command/config.php | 120 ---- apps/files_external/command/create.php | 226 -------- apps/files_external/command/delete.php | 114 ---- apps/files_external/command/export.php | 56 -- apps/files_external/command/import.php | 227 -------- apps/files_external/command/listcommand.php | 248 -------- apps/files_external/command/option.php | 85 --- apps/files_external/command/verify.php | 145 ----- apps/files_external/controller/ajaxcontroller.php | 64 --- .../controller/globalstoragescontroller.php | 189 ------- .../controller/storagescontroller.php | 342 ----------- .../controller/userglobalstoragescontroller.php | 201 ------- .../controller/userstoragescontroller.php | 226 -------- apps/files_external/lib/AppInfo/Application.php | 135 +++++ apps/files_external/lib/Command/Applicable.php | 154 +++++ apps/files_external/lib/Command/Backends.php | 112 ++++ apps/files_external/lib/Command/Config.php | 120 ++++ apps/files_external/lib/Command/Create.php | 221 ++++++++ apps/files_external/lib/Command/Delete.php | 110 ++++ apps/files_external/lib/Command/Export.php | 56 ++ apps/files_external/lib/Command/Import.php | 223 ++++++++ apps/files_external/lib/Command/ListCommand.php | 247 ++++++++ apps/files_external/lib/Command/Option.php | 75 +++ apps/files_external/lib/Command/Verify.php | 142 +++++ .../lib/Controller/AjaxController.php | 64 +++ .../lib/Controller/GlobalStoragesController.php | 184 ++++++ .../lib/Controller/StoragesController.php | 338 +++++++++++ .../Controller/UserGlobalStoragesController.php | 201 +++++++ .../lib/Controller/UserStoragesController.php | 222 ++++++++ .../lib/Migration/DummyUserSession.php | 51 ++ .../lib/Migration/StorageMigrator.php | 143 +++++ apps/files_external/lib/Service/BackendService.php | 282 +++++++++ .../files_external/lib/Service/DBConfigService.php | 451 +++++++++++++++ .../lib/Service/GlobalLegacyStoragesService.php | 44 ++ .../lib/Service/GlobalStoragesService.php | 165 ++++++ .../lib/Service/ImportLegacyStoragesService.php | 46 ++ .../lib/Service/LegacyStoragesService.php | 209 +++++++ .../files_external/lib/Service/StoragesService.php | 527 +++++++++++++++++ .../lib/Service/UserGlobalStoragesService.php | 174 ++++++ .../lib/Service/UserLegacyStoragesService.php | 54 ++ .../lib/Service/UserStoragesService.php | 141 +++++ apps/files_external/lib/Service/UserTrait.php | 74 +++ apps/files_external/lib/config/configadapter.php | 4 +- apps/files_external/migration/dummyusersession.php | 51 -- apps/files_external/migration/storagemigrator.php | 147 ----- apps/files_external/personal.php | 2 +- apps/files_external/service/backendservice.php | 282 --------- apps/files_external/service/dbconfigservice.php | 451 --------------- .../service/globallegacystoragesservice.php | 44 -- .../service/globalstoragesservice.php | 165 ------ .../service/importlegacystoragesservice.php | 46 -- .../service/legacystoragesservice.php | 209 ------- apps/files_external/service/storagesservice.php | 527 ----------------- .../service/userglobalstoragesservice.php | 177 ------ .../service/userlegacystoragesservice.php | 54 -- .../files_external/service/userstoragesservice.php | 141 ----- apps/files_external/service/usertrait.php | 74 --- apps/files_external/settings.php | 2 +- .../tests/Command/ApplicableTest.php | 168 ++++++ apps/files_external/tests/Command/CommandTest.php | 104 ++++ .../tests/Command/ListCommandTest.php | 70 +++ .../Controller/GlobalStoragesControllerTest.php | 47 ++ .../tests/Controller/StoragesControllerTest.php | 394 +++++++++++++ .../Controller/UserStoragesControllerTest.php | 104 ++++ .../tests/Service/BackendServiceTest.php | 211 +++++++ .../tests/Service/DBConfigServiceTest.php | 285 ++++++++++ .../tests/Service/GlobalStoragesServiceTest.php | 627 ++++++++++++++++++++ .../tests/Service/StoragesServiceTest.php | 505 +++++++++++++++++ .../Service/UserGlobalStoragesServiceTest.php | 363 ++++++++++++ .../tests/Service/UserStoragesServiceTest.php | 207 +++++++ .../tests/command/applicabletest.php | 168 ------ apps/files_external/tests/command/commandtest.php | 104 ---- .../tests/command/listcommandtest.php | 70 --- .../controller/globalstoragescontrollertest.php | 49 -- .../tests/controller/storagescontrollertest.php | 394 ------------- .../controller/userstoragescontrollertest.php | 107 ---- .../tests/service/backendservicetest.php | 211 ------- .../tests/service/dbconfigservicetest.php | 285 ---------- .../tests/service/globalstoragesservicetest.php | 629 --------------------- .../tests/service/storagesservicetest.php | 505 ----------------- .../service/userglobalstoragesservicetest.php | 366 ------------ .../tests/service/userstoragesservicetest.php | 208 ------- 90 files changed, 8062 insertions(+), 8123 deletions(-) delete mode 100644 apps/files_external/appinfo/application.php delete mode 100644 apps/files_external/command/applicable.php delete mode 100644 apps/files_external/command/backends.php delete mode 100644 apps/files_external/command/config.php delete mode 100644 apps/files_external/command/create.php delete mode 100644 apps/files_external/command/delete.php delete mode 100644 apps/files_external/command/export.php delete mode 100644 apps/files_external/command/import.php delete mode 100644 apps/files_external/command/listcommand.php delete mode 100644 apps/files_external/command/option.php delete mode 100644 apps/files_external/command/verify.php delete mode 100644 apps/files_external/controller/ajaxcontroller.php delete mode 100644 apps/files_external/controller/globalstoragescontroller.php delete mode 100644 apps/files_external/controller/storagescontroller.php delete mode 100644 apps/files_external/controller/userglobalstoragescontroller.php delete mode 100644 apps/files_external/controller/userstoragescontroller.php create mode 100644 apps/files_external/lib/AppInfo/Application.php create mode 100644 apps/files_external/lib/Command/Applicable.php create mode 100644 apps/files_external/lib/Command/Backends.php create mode 100644 apps/files_external/lib/Command/Config.php create mode 100644 apps/files_external/lib/Command/Create.php create mode 100644 apps/files_external/lib/Command/Delete.php create mode 100644 apps/files_external/lib/Command/Export.php create mode 100644 apps/files_external/lib/Command/Import.php create mode 100644 apps/files_external/lib/Command/ListCommand.php create mode 100644 apps/files_external/lib/Command/Option.php create mode 100644 apps/files_external/lib/Command/Verify.php create mode 100644 apps/files_external/lib/Controller/AjaxController.php create mode 100644 apps/files_external/lib/Controller/GlobalStoragesController.php create mode 100644 apps/files_external/lib/Controller/StoragesController.php create mode 100644 apps/files_external/lib/Controller/UserGlobalStoragesController.php create mode 100644 apps/files_external/lib/Controller/UserStoragesController.php create mode 100644 apps/files_external/lib/Migration/DummyUserSession.php create mode 100644 apps/files_external/lib/Migration/StorageMigrator.php create mode 100644 apps/files_external/lib/Service/BackendService.php create mode 100644 apps/files_external/lib/Service/DBConfigService.php create mode 100644 apps/files_external/lib/Service/GlobalLegacyStoragesService.php create mode 100644 apps/files_external/lib/Service/GlobalStoragesService.php create mode 100644 apps/files_external/lib/Service/ImportLegacyStoragesService.php create mode 100644 apps/files_external/lib/Service/LegacyStoragesService.php create mode 100644 apps/files_external/lib/Service/StoragesService.php create mode 100644 apps/files_external/lib/Service/UserGlobalStoragesService.php create mode 100644 apps/files_external/lib/Service/UserLegacyStoragesService.php create mode 100644 apps/files_external/lib/Service/UserStoragesService.php create mode 100644 apps/files_external/lib/Service/UserTrait.php delete mode 100644 apps/files_external/migration/dummyusersession.php delete mode 100644 apps/files_external/migration/storagemigrator.php delete mode 100644 apps/files_external/service/backendservice.php delete mode 100644 apps/files_external/service/dbconfigservice.php delete mode 100644 apps/files_external/service/globallegacystoragesservice.php delete mode 100644 apps/files_external/service/globalstoragesservice.php delete mode 100644 apps/files_external/service/importlegacystoragesservice.php delete mode 100644 apps/files_external/service/legacystoragesservice.php delete mode 100644 apps/files_external/service/storagesservice.php delete mode 100644 apps/files_external/service/userglobalstoragesservice.php delete mode 100644 apps/files_external/service/userlegacystoragesservice.php delete mode 100644 apps/files_external/service/userstoragesservice.php delete mode 100644 apps/files_external/service/usertrait.php create mode 100644 apps/files_external/tests/Command/ApplicableTest.php create mode 100644 apps/files_external/tests/Command/CommandTest.php create mode 100644 apps/files_external/tests/Command/ListCommandTest.php create mode 100644 apps/files_external/tests/Controller/GlobalStoragesControllerTest.php create mode 100644 apps/files_external/tests/Controller/StoragesControllerTest.php create mode 100644 apps/files_external/tests/Controller/UserStoragesControllerTest.php create mode 100644 apps/files_external/tests/Service/BackendServiceTest.php create mode 100644 apps/files_external/tests/Service/DBConfigServiceTest.php create mode 100644 apps/files_external/tests/Service/GlobalStoragesServiceTest.php create mode 100644 apps/files_external/tests/Service/StoragesServiceTest.php create mode 100644 apps/files_external/tests/Service/UserGlobalStoragesServiceTest.php create mode 100644 apps/files_external/tests/Service/UserStoragesServiceTest.php delete mode 100644 apps/files_external/tests/command/applicabletest.php delete mode 100644 apps/files_external/tests/command/commandtest.php delete mode 100644 apps/files_external/tests/command/listcommandtest.php delete mode 100644 apps/files_external/tests/controller/globalstoragescontrollertest.php delete mode 100644 apps/files_external/tests/controller/storagescontrollertest.php delete mode 100644 apps/files_external/tests/controller/userstoragescontrollertest.php delete mode 100644 apps/files_external/tests/service/backendservicetest.php delete mode 100644 apps/files_external/tests/service/dbconfigservicetest.php delete mode 100644 apps/files_external/tests/service/globalstoragesservicetest.php delete mode 100644 apps/files_external/tests/service/storagesservicetest.php delete mode 100644 apps/files_external/tests/service/userglobalstoragesservicetest.php delete mode 100644 apps/files_external/tests/service/userstoragesservicetest.php (limited to 'apps') diff --git a/apps/files_external/appinfo/app.php b/apps/files_external/appinfo/app.php index 18f8b2551fa..26358886c0b 100644 --- a/apps/files_external/appinfo/app.php +++ b/apps/files_external/appinfo/app.php @@ -31,7 +31,7 @@ OC::$CLASSPATH['OC_Mount_Config'] = 'files_external/lib/config.php'; require_once __DIR__ . '/../3rdparty/autoload.php'; // register Application object singleton -\OC_Mount_Config::$app = new \OCA\Files_external\Appinfo\Application(); +\OC_Mount_Config::$app = new \OCA\Files_External\AppInfo\Application(); $appContainer = \OC_Mount_Config::$app->getContainer(); \OC_Mount_Config::$app->registerSettings(); diff --git a/apps/files_external/appinfo/application.php b/apps/files_external/appinfo/application.php deleted file mode 100644 index 5d490c6091f..00000000000 --- a/apps/files_external/appinfo/application.php +++ /dev/null @@ -1,135 +0,0 @@ - - * @author Robin Appelman - * @author Robin McCorkell - * @author Roeland Jago Douma - * @author Ross Nicoll - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_External\AppInfo; - -use \OCP\AppFramework\App; -use OCP\AppFramework\IAppContainer; -use \OCP\IContainer; -use \OCA\Files_External\Service\BackendService; -use \OCA\Files_External\Lib\Config\IBackendProvider; -use \OCA\Files_External\Lib\Config\IAuthMechanismProvider; - -/** - * @package OCA\Files_External\Appinfo - */ -class Application extends App implements IBackendProvider, IAuthMechanismProvider { - - public function __construct(array $urlParams = array()) { - parent::__construct('files_external', $urlParams); - - $container = $this->getContainer(); - - $container->registerService('OCP\Files\Config\IUserMountCache', function (IAppContainer $c) { - return $c->getServer()->query('UserMountCache'); - }); - - $backendService = $container->query('OCA\\Files_External\\Service\\BackendService'); - $backendService->registerBackendProvider($this); - $backendService->registerAuthMechanismProvider($this); - - // app developers: do NOT depend on this! it will disappear with oC 9.0! - \OC::$server->getEventDispatcher()->dispatch( - 'OCA\\Files_External::loadAdditionalBackends' - ); - } - - /** - * Register settings templates - */ - public function registerSettings() { - $container = $this->getContainer(); - $backendService = $container->query('OCA\\Files_External\\Service\\BackendService'); - - \OCP\App::registerAdmin('files_external', 'settings'); - \OCP\App::registerPersonal('files_external', 'personal'); - } - - /** - * @{inheritdoc} - */ - public function getBackends() { - $container = $this->getContainer(); - - $backends = [ - $container->query('OCA\Files_External\Lib\Backend\Local'), - $container->query('OCA\Files_External\Lib\Backend\FTP'), - $container->query('OCA\Files_External\Lib\Backend\DAV'), - $container->query('OCA\Files_External\Lib\Backend\OwnCloud'), - $container->query('OCA\Files_External\Lib\Backend\SFTP'), - $container->query('OCA\Files_External\Lib\Backend\AmazonS3'), - $container->query('OCA\Files_External\Lib\Backend\Dropbox'), - $container->query('OCA\Files_External\Lib\Backend\Google'), - $container->query('OCA\Files_External\Lib\Backend\Swift'), - $container->query('OCA\Files_External\Lib\Backend\SFTP_Key'), - ]; - - if (!\OC_Util::runningOnWindows()) { - $backends += [ - $container->query('OCA\Files_External\Lib\Backend\SMB'), - $container->query('OCA\Files_External\Lib\Backend\SMB_OC'), - ]; - } - - return $backends; - } - - /** - * @{inheritdoc} - */ - public function getAuthMechanisms() { - $container = $this->getContainer(); - - return [ - // AuthMechanism::SCHEME_NULL mechanism - $container->query('OCA\Files_External\Lib\Auth\NullMechanism'), - - // AuthMechanism::SCHEME_BUILTIN mechanism - $container->query('OCA\Files_External\Lib\Auth\Builtin'), - - // AuthMechanism::SCHEME_PASSWORD mechanisms - $container->query('OCA\Files_External\Lib\Auth\Password\Password'), - $container->query('OCA\Files_External\Lib\Auth\Password\SessionCredentials'), - - // AuthMechanism::SCHEME_OAUTH1 mechanisms - $container->query('OCA\Files_External\Lib\Auth\OAuth1\OAuth1'), - - // AuthMechanism::SCHEME_OAUTH2 mechanisms - $container->query('OCA\Files_External\Lib\Auth\OAuth2\OAuth2'), - - // AuthMechanism::SCHEME_PUBLICKEY mechanisms - $container->query('OCA\Files_External\Lib\Auth\PublicKey\RSA'), - - // AuthMechanism::SCHEME_OPENSTACK mechanisms - $container->query('OCA\Files_External\Lib\Auth\OpenStack\OpenStack'), - $container->query('OCA\Files_External\Lib\Auth\OpenStack\Rackspace'), - - // Specialized mechanisms - $container->query('OCA\Files_External\Lib\Auth\AmazonS3\AccessKey'), - ]; - } - -} diff --git a/apps/files_external/appinfo/info.xml b/apps/files_external/appinfo/info.xml index 2b192c94aa4..25df58c6368 100644 --- a/apps/files_external/appinfo/info.xml +++ b/apps/files_external/appinfo/info.xml @@ -19,6 +19,8 @@ 166048 + Files_External + diff --git a/apps/files_external/appinfo/register_command.php b/apps/files_external/appinfo/register_command.php index 927ce9869f9..65ac2286fa0 100644 --- a/apps/files_external/appinfo/register_command.php +++ b/apps/files_external/appinfo/register_command.php @@ -37,9 +37,9 @@ $groupManager = OC::$server->getGroupManager(); $app = \OC_Mount_Config::$app; -$globalStorageService = $app->getContainer()->query('\OCA\Files_external\Service\GlobalStoragesService'); -$userStorageService = $app->getContainer()->query('\OCA\Files_external\Service\UserStoragesService'); -$importLegacyStorageService = $app->getContainer()->query('\OCA\Files_external\Service\ImportLegacyStoragesService'); +$globalStorageService = $app->getContainer()->query('\OCA\Files_External\Service\GlobalStoragesService'); +$userStorageService = $app->getContainer()->query('\OCA\Files_External\Service\UserStoragesService'); +$importLegacyStorageService = $app->getContainer()->query('\OCA\Files_External\Service\ImportLegacyStoragesService'); $backendService = $app->getContainer()->query('OCA\Files_External\Service\BackendService'); /** @var Symfony\Component\Console\Application $application */ diff --git a/apps/files_external/appinfo/routes.php b/apps/files_external/appinfo/routes.php index e2f55e652a8..703b2cd265f 100644 --- a/apps/files_external/appinfo/routes.php +++ b/apps/files_external/appinfo/routes.php @@ -26,8 +26,6 @@ * */ -namespace OCA\Files_External\AppInfo; - /** * @var $this \OCP\Route\IRouter **/ diff --git a/apps/files_external/appinfo/update.php b/apps/files_external/appinfo/update.php index 8cc2422c7ff..62dfbadc936 100644 --- a/apps/files_external/appinfo/update.php +++ b/apps/files_external/appinfo/update.php @@ -21,10 +21,10 @@ $installedVersion = \OC::$server->getConfig()->getAppValue('files_external', 'installed_version'); -$app = new \OCA\Files_external\Appinfo\Application(); +$app = new \OCA\Files_External\AppInfo\Application(); // Migration to db config if (version_compare($installedVersion, '0.5.0', '<')) { - $migrator = $app->getContainer()->query('OCA\Files_external\Migration\StorageMigrator'); + $migrator = $app->getContainer()->query('OCA\Files_External\Migration\StorageMigrator'); $migrator->migrateGlobal(); } diff --git a/apps/files_external/command/applicable.php b/apps/files_external/command/applicable.php deleted file mode 100644 index 7e6c99d2915..00000000000 --- a/apps/files_external/command/applicable.php +++ /dev/null @@ -1,157 +0,0 @@ - - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_External\Command; - -use OC\Core\Command\Base; -use OCA\Files_external\Lib\StorageConfig; -use OCA\Files_external\NotFoundException; -use OCA\Files_external\Service\GlobalStoragesService; -use OCP\IGroupManager; -use OCP\IUserManager; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Helper\Table; -use Symfony\Component\Console\Helper\TableHelper; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Output\OutputInterface; - -class Applicable extends Base { - /** - * @var GlobalStoragesService - */ - protected $globalService; - - /** - * @var IUserManager - */ - private $userManager; - - /** - * @var IGroupManager - */ - private $groupManager; - - function __construct( - GlobalStoragesService $globalService, - IUserManager $userManager, - IGroupManager $groupManager - ) { - parent::__construct(); - $this->globalService = $globalService; - $this->userManager = $userManager; - $this->groupManager = $groupManager; - } - - protected function configure() { - $this - ->setName('files_external:applicable') - ->setDescription('Manage applicable users and groups for a mount') - ->addArgument( - 'mount_id', - InputArgument::REQUIRED, - 'The id of the mount to edit' - )->addOption( - 'add-user', - null, - InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, - 'user to add as applicable' - )->addOption( - 'remove-user', - null, - InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, - 'user to remove as applicable' - )->addOption( - 'add-group', - null, - InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, - 'group to add as applicable' - )->addOption( - 'remove-group', - null, - InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, - 'group to remove as applicable' - )->addOption( - 'remove-all', - null, - InputOption::VALUE_NONE, - 'Set the mount to be globally applicable' - ); - parent::configure(); - } - - protected function execute(InputInterface $input, OutputInterface $output) { - $mountId = $input->getArgument('mount_id'); - try { - $mount = $this->globalService->getStorage($mountId); - } catch (NotFoundException $e) { - $output->writeln('Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts'); - return 404; - } - - if ($mount->getType() === StorageConfig::MOUNT_TYPE_PERSONAl) { - $output->writeln('Can\'t change applicables on personal mounts'); - return 1; - } - - $addUsers = $input->getOption('add-user'); - $removeUsers = $input->getOption('remove-user'); - $addGroups = $input->getOption('add-group'); - $removeGroups = $input->getOption('remove-group'); - - $applicableUsers = $mount->getApplicableUsers(); - $applicableGroups = $mount->getApplicableGroups(); - - if ((count($addUsers) + count($removeUsers) + count($addGroups) + count($removeGroups) > 0) || $input->getOption('remove-all')) { - foreach ($addUsers as $addUser) { - if (!$this->userManager->userExists($addUser)) { - $output->writeln('User "' . $addUser . '" not found'); - return 404; - } - } - foreach ($addGroups as $addGroup) { - if (!$this->groupManager->groupExists($addGroup)) { - $output->writeln('Group "' . $addGroup . '" not found'); - return 404; - } - } - - if ($input->getOption('remove-all')) { - $applicableUsers = []; - $applicableGroups = []; - } else { - $applicableUsers = array_unique(array_merge($applicableUsers, $addUsers)); - $applicableUsers = array_values(array_diff($applicableUsers, $removeUsers)); - $applicableGroups = array_unique(array_merge($applicableGroups, $addGroups)); - $applicableGroups = array_values(array_diff($applicableGroups, $removeGroups)); - } - $mount->setApplicableUsers($applicableUsers); - $mount->setApplicableGroups($applicableGroups); - $this->globalService->updateStorage($mount); - } - - $this->writeArrayInOutputFormat($input, $output, [ - 'users' => $applicableUsers, - 'groups' => $applicableGroups - ]); - } -} diff --git a/apps/files_external/command/backends.php b/apps/files_external/command/backends.php deleted file mode 100644 index 260ea210397..00000000000 --- a/apps/files_external/command/backends.php +++ /dev/null @@ -1,112 +0,0 @@ - - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_External\Command; - -use OC\Core\Command\Base; -use OCA\Files_External\Lib\Auth\AuthMechanism; -use OCA\Files_External\Lib\Backend\Backend; -use OCA\Files_External\Lib\DefinitionParameter; -use OCA\Files_External\Service\BackendService; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Helper\Table; -use Symfony\Component\Console\Helper\TableHelper; -use Symfony\Component\Console\Input\ArrayInput; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Input\Input; -use Symfony\Component\Console\Output\OutputInterface; - -class Backends extends Base { - /** @var BackendService */ - private $backendService; - - function __construct(BackendService $backendService - ) { - parent::__construct(); - - $this->backendService = $backendService; - } - - protected function configure() { - $this - ->setName('files_external:backends') - ->setDescription('Show available authentication and storage backends') - ->addArgument( - 'type', - InputArgument::OPTIONAL, - 'only show backends of a certain type. Possible values are "authentication" or "storage"' - )->addArgument( - 'backend', - InputArgument::OPTIONAL, - 'only show information of a specific backend' - ); - parent::configure(); - } - - protected function execute(InputInterface $input, OutputInterface $output) { - $authBackends = $this->backendService->getAuthMechanisms(); - $storageBackends = $this->backendService->getBackends(); - - $data = [ - 'authentication' => array_map([$this, 'serializeAuthBackend'], $authBackends), - 'storage' => array_map([$this, 'serializeAuthBackend'], $storageBackends) - ]; - - $type = $input->getArgument('type'); - $backend = $input->getArgument('backend'); - if ($type) { - if (!isset($data[$type])) { - $output->writeln('Invalid type "' . $type . '". Possible values are "authentication" or "storage"'); - return 1; - } - $data = $data[$type]; - - if ($backend) { - if (!isset($data[$backend])) { - $output->writeln('Unknown backend "' . $backend . '" of type "' . $type . '"'); - return 1; - } - $data = $data[$backend]; - } - } - - $this->writeArrayInOutputFormat($input, $output, $data); - } - - private function serializeAuthBackend(\JsonSerializable $backend) { - $data = $backend->jsonSerialize(); - $result = [ - 'name' => $data['name'], - 'identifier' => $data['identifier'], - 'configuration' => array_map(function (DefinitionParameter $parameter) { - return $parameter->getTypeName(); - }, $data['configuration']) - ]; - if ($backend instanceof Backend) { - $result['storage_class'] = $backend->getStorageClass(); - $authBackends = $this->backendService->getAuthMechanismsByScheme(array_keys($backend->getAuthSchemes())); - $result['supported_authentication_backends'] = array_keys($authBackends); - } - return $result; - } -} diff --git a/apps/files_external/command/config.php b/apps/files_external/command/config.php deleted file mode 100644 index 1403c24b345..00000000000 --- a/apps/files_external/command/config.php +++ /dev/null @@ -1,120 +0,0 @@ - - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_External\Command; - -use OC\Core\Command\Base; -use OCA\Files_external\Lib\StorageConfig; -use OCA\Files_external\NotFoundException; -use OCA\Files_external\Service\GlobalStoragesService; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Helper\Table; -use Symfony\Component\Console\Helper\TableHelper; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Output\OutputInterface; - -class Config extends Base { - /** - * @var GlobalStoragesService - */ - protected $globalService; - - function __construct(GlobalStoragesService $globalService) { - parent::__construct(); - $this->globalService = $globalService; - } - - protected function configure() { - $this - ->setName('files_external:config') - ->setDescription('Manage backend configuration for a mount') - ->addArgument( - 'mount_id', - InputArgument::REQUIRED, - 'The id of the mount to edit' - )->addArgument( - 'key', - InputArgument::REQUIRED, - 'key of the config option to set/get' - )->addArgument( - 'value', - InputArgument::OPTIONAL, - 'value to set the config option to, when no value is provided the existing value will be printed' - ); - parent::configure(); - } - - protected function execute(InputInterface $input, OutputInterface $output) { - $mountId = $input->getArgument('mount_id'); - $key = $input->getArgument('key'); - try { - $mount = $this->globalService->getStorage($mountId); - } catch (NotFoundException $e) { - $output->writeln('Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"'); - return 404; - } - - $value = $input->getArgument('value'); - if ($value) { - $this->setOption($mount, $key, $value, $output); - } else { - $this->getOption($mount, $key, $output); - } - } - - /** - * @param StorageConfig $mount - * @param string $key - * @param OutputInterface $output - */ - protected function getOption(StorageConfig $mount, $key, OutputInterface $output) { - if ($key === 'mountpoint' || $key === 'mount_point') { - $value = $mount->getMountPoint(); - } else { - $value = $mount->getBackendOption($key); - } - if (!is_string($value)) { // show bools and objects correctly - $value = json_encode($value); - } - $output->writeln($value); - } - - /** - * @param StorageConfig $mount - * @param string $key - * @param string $value - * @param OutputInterface $output - */ - protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output) { - $decoded = json_decode($value, true); - if (!is_null($decoded)) { - $value = $decoded; - } - if ($key === 'mountpoint' || $key === 'mount_point') { - $mount->setMountPoint($value); - } else { - $mount->setBackendOption($key, $value); - } - $this->globalService->updateStorage($mount); - } -} diff --git a/apps/files_external/command/create.php b/apps/files_external/command/create.php deleted file mode 100644 index 315b23f9d3c..00000000000 --- a/apps/files_external/command/create.php +++ /dev/null @@ -1,226 +0,0 @@ - - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_External\Command; - -use OC\Core\Command\Base; -use OC\Files\Filesystem; -use OC\User\NoUserException; -use OCA\Files_External\Lib\Auth\AuthMechanism; -use OCA\Files_External\Lib\Backend\Backend; -use OCA\Files_External\Lib\DefinitionParameter; -use OCA\Files_external\Lib\StorageConfig; -use OCA\Files_External\Service\BackendService; -use OCA\Files_external\Service\GlobalStoragesService; -use OCA\Files_external\Service\ImportLegacyStoragesService; -use OCA\Files_external\Service\UserStoragesService; -use OCP\IUserManager; -use OCP\IUserSession; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Helper\Table; -use Symfony\Component\Console\Helper\TableHelper; -use Symfony\Component\Console\Input\ArrayInput; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Input\Input; -use Symfony\Component\Console\Output\OutputInterface; - -class Create extends Base { - /** - * @var GlobalStoragesService - */ - private $globalService; - - /** - * @var UserStoragesService - */ - private $userService; - - /** - * @var IUserManager - */ - private $userManager; - - /** @var BackendService */ - private $backendService; - - /** @var IUserSession */ - private $userSession; - - function __construct(GlobalStoragesService $globalService, - UserStoragesService $userService, - IUserManager $userManager, - IUserSession $userSession, - BackendService $backendService - ) { - parent::__construct(); - $this->globalService = $globalService; - $this->userService = $userService; - $this->userManager = $userManager; - $this->userSession = $userSession; - $this->backendService = $backendService; - } - - protected function configure() { - $this - ->setName('files_external:create') - ->setDescription('Create a new mount configuration') - ->addOption( - 'user', - null, - InputOption::VALUE_OPTIONAL, - 'user to add the mount configuration for, if not set the mount will be added as system mount' - ) - ->addArgument( - 'mount_point', - InputArgument::REQUIRED, - 'mount point for the new mount' - ) - ->addArgument( - 'storage_backend', - InputArgument::REQUIRED, - 'storage backend identifier for the new mount, see `occ files_external:backends` for possible values' - ) - ->addArgument( - 'authentication_backend', - InputArgument::REQUIRED, - 'authentication backend identifier for the new mount, see `occ files_external:backends` for possible values' - ) - ->addOption( - 'config', - 'c', - InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, - 'Mount configuration option in key=value format' - ) - ->addOption( - 'dry', - null, - InputOption::VALUE_NONE, - 'Don\'t save the created mount, only list the new mount' - ); - parent::configure(); - } - - protected function execute(InputInterface $input, OutputInterface $output) { - $user = $input->getOption('user'); - $mountPoint = $input->getArgument('mount_point'); - $storageIdentifier = $input->getArgument('storage_backend'); - $authIdentifier = $input->getArgument('authentication_backend'); - $configInput = $input->getOption('config'); - - $storageBackend = $this->backendService->getBackend($storageIdentifier); - $authBackend = $this->backendService->getAuthMechanism($authIdentifier); - - if (!Filesystem::isValidPath($mountPoint)) { - $output->writeln('Invalid mountpoint "' . $mountPoint . '"'); - return 1; - } - if (is_null($storageBackend)) { - $output->writeln('Storage backend with identifier "' . $storageIdentifier . '" not found (see `occ files_external:backends` for possible values)'); - return 404; - } - if (is_null($authBackend)) { - $output->writeln('Authentication backend with identifier "' . $authIdentifier . '" not found (see `occ files_external:backends` for possible values)'); - return 404; - } - $supportedSchemes = array_keys($storageBackend->getAuthSchemes()); - if (!in_array($authBackend->getScheme(), $supportedSchemes)) { - $output->writeln('Authentication backend "' . $authIdentifier . '" not valid for storage backend "' . $storageIdentifier . '" (see `occ files_external:backends storage ' . $storageIdentifier . '` for possible values)'); - return 1; - } - - $config = []; - foreach ($configInput as $configOption) { - if (!strpos($configOption, '=')) { - $output->writeln('Invalid mount configuration option "' . $configOption . '"'); - return 1; - } - list($key, $value) = explode('=', $configOption, 2); - if (!$this->validateParam($key, $value, $storageBackend, $authBackend)) { - $output->writeln('Unknown configuration for backends "' . $key . '"'); - return 1; - } - $config[$key] = $value; - } - - $mount = new StorageConfig(); - $mount->setMountPoint($mountPoint); - $mount->setBackend($storageBackend); - $mount->setAuthMechanism($authBackend); - $mount->setBackendOptions($config); - - if ($user) { - if (!$this->userManager->userExists($user)) { - $output->writeln('User "' . $user . '" not found'); - return 1; - } - $mount->setApplicableUsers([$user]); - } - - if ($input->getOption('dry')) { - $this->showMount($user, $mount, $input, $output); - } else { - $this->getStorageService($user)->addStorage($mount); - if ($input->getOption('output') === self::OUTPUT_FORMAT_PLAIN) { - $output->writeln('Storage created with id ' . $mount->getId() . ''); - } else { - $output->writeln($mount->getId()); - } - } - return 0; - } - - private function validateParam($key, &$value, Backend $storageBackend, AuthMechanism $authBackend) { - $params = array_merge($storageBackend->getParameters(), $authBackend->getParameters()); - foreach ($params as $param) { - /** @var DefinitionParameter $param */ - if ($param->getName() === $key) { - if ($param->getType() === DefinitionParameter::VALUE_BOOLEAN) { - $value = ($value === 'true'); - } - return true; - } - } - return false; - } - - private function showMount($user, StorageConfig $mount, InputInterface $input, OutputInterface $output) { - $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); - $listInput = new ArrayInput([], $listCommand->getDefinition()); - $listInput->setOption('output', $input->getOption('output')); - $listInput->setOption('show-password', true); - $listCommand->listMounts($user, [$mount], $listInput, $output); - } - - protected function getStorageService($userId) { - if (!empty($userId)) { - $user = $this->userManager->get($userId); - if (is_null($user)) { - throw new NoUserException("user $userId not found"); - } - $this->userSession->setUser($user); - return $this->userService; - } else { - return $this->globalService; - } - } -} diff --git a/apps/files_external/command/delete.php b/apps/files_external/command/delete.php deleted file mode 100644 index bdbfcf8bb55..00000000000 --- a/apps/files_external/command/delete.php +++ /dev/null @@ -1,114 +0,0 @@ - - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_External\Command; - -use OC\Core\Command\Base; -use OCA\Files_external\Lib\StorageConfig; -use OCA\Files_external\NotFoundException; -use OCA\Files_external\Service\GlobalStoragesService; -use OCA\Files_external\Service\UserStoragesService; -use OCP\IUserManager; -use OCP\IUserSession; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Helper\Table; -use Symfony\Component\Console\Helper\TableHelper; -use Symfony\Component\Console\Input\ArrayInput; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Question\ConfirmationQuestion; - -class Delete extends Base { - /** - * @var GlobalStoragesService - */ - protected $globalService; - - /** - * @var UserStoragesService - */ - protected $userService; - - /** - * @var IUserSession - */ - protected $userSession; - - /** - * @var IUserManager - */ - protected $userManager; - - function __construct(GlobalStoragesService $globalService, UserStoragesService $userService, IUserSession $userSession, IUserManager $userManager) { - parent::__construct(); - $this->globalService = $globalService; - $this->userService = $userService; - $this->userSession = $userSession; - $this->userManager = $userManager; - } - - protected function configure() { - $this - ->setName('files_external:delete') - ->setDescription('Delete an external mount') - ->addArgument( - 'mount_id', - InputArgument::REQUIRED, - 'The id of the mount to edit' - )->addOption( - 'yes', - 'y', - InputOption::VALUE_NONE, - 'Skip confirmation' - ); - parent::configure(); - } - - protected function execute(InputInterface $input, OutputInterface $output) { - $mountId = $input->getArgument('mount_id'); - try { - $mount = $this->globalService->getStorage($mountId); - } catch (NotFoundException $e) { - $output->writeln('Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"'); - return 404; - } - - $noConfirm = $input->getOption('yes'); - - if (!$noConfirm) { - $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); - $listInput = new ArrayInput([], $listCommand->getDefinition()); - $listInput->setOption('output', $input->getOption('output')); - $listCommand->listMounts(null, [$mount], $listInput, $output); - - $questionHelper = $this->getHelper('question'); - $question = new ConfirmationQuestion('Delete this mount? [y/N] ', false); - - if (!$questionHelper->ask($input, $output, $question)) { - return; - } - } - - $this->globalService->removeStorage($mountId); - } -} diff --git a/apps/files_external/command/export.php b/apps/files_external/command/export.php deleted file mode 100644 index 09c5ea8a9df..00000000000 --- a/apps/files_external/command/export.php +++ /dev/null @@ -1,56 +0,0 @@ - - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_External\Command; - -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Helper\Table; -use Symfony\Component\Console\Helper\TableHelper; -use Symfony\Component\Console\Input\ArrayInput; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Input\Input; -use Symfony\Component\Console\Output\OutputInterface; - -class Export extends ListCommand { - - protected function configure() { - $this - ->setName('files_external:export') - ->setDescription('Export mount configurations') - ->addArgument( - 'user_id', - InputArgument::OPTIONAL, - 'user id to export the personal mounts for, if no user is provided admin mounts will be exported' - ); - } - - protected function execute(InputInterface $input, OutputInterface $output) { - $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); - $listInput = new ArrayInput([], $listCommand->getDefinition()); - $listInput->setArgument('user_id', $input->getArgument('user_id')); - $listInput->setOption('output', 'json_pretty'); - $listInput->setOption('show-password', true); - $listInput->setOption('full', true); - $listCommand->execute($listInput, $output); - } -} diff --git a/apps/files_external/command/import.php b/apps/files_external/command/import.php deleted file mode 100644 index 97bb9a937f0..00000000000 --- a/apps/files_external/command/import.php +++ /dev/null @@ -1,227 +0,0 @@ - - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_External\Command; - -use OC\Core\Command\Base; -use OC\User\NoUserException; -use OCA\Files_external\Lib\StorageConfig; -use OCA\Files_External\Service\BackendService; -use OCA\Files_external\Service\GlobalStoragesService; -use OCA\Files_external\Service\ImportLegacyStoragesService; -use OCA\Files_external\Service\UserStoragesService; -use OCP\IUserManager; -use OCP\IUserSession; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Helper\Table; -use Symfony\Component\Console\Helper\TableHelper; -use Symfony\Component\Console\Input\ArrayInput; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Input\Input; -use Symfony\Component\Console\Output\OutputInterface; - -class Import extends Base { - /** - * @var GlobalStoragesService - */ - private $globalService; - - /** - * @var UserStoragesService - */ - private $userService; - - /** - * @var IUserSession - */ - private $userSession; - - /** - * @var IUserManager - */ - private $userManager; - - /** @var ImportLegacyStoragesService */ - private $importLegacyStorageService; - - /** @var BackendService */ - private $backendService; - - function __construct(GlobalStoragesService $globalService, - UserStoragesService $userService, - IUserSession $userSession, - IUserManager $userManager, - ImportLegacyStoragesService $importLegacyStorageService, - BackendService $backendService - ) { - parent::__construct(); - $this->globalService = $globalService; - $this->userService = $userService; - $this->userSession = $userSession; - $this->userManager = $userManager; - $this->importLegacyStorageService = $importLegacyStorageService; - $this->backendService = $backendService; - } - - protected function configure() { - $this - ->setName('files_external:import') - ->setDescription('Import mount configurations') - ->addOption( - 'user', - null, - InputOption::VALUE_OPTIONAL, - 'user to add the mount configurations for, if not set the mount will be added as system mount' - ) - ->addArgument( - 'path', - InputArgument::REQUIRED, - 'path to a json file containing the mounts to import, use "-" to read from stdin' - ) - ->addOption( - 'dry', - null, - InputOption::VALUE_NONE, - 'Don\'t save the imported mounts, only list the new mounts' - ); - parent::configure(); - } - - protected function execute(InputInterface $input, OutputInterface $output) { - $user = $input->getOption('user'); - $path = $input->getArgument('path'); - if ($path === '-') { - $json = file_get_contents('php://stdin'); - } else { - if (!file_exists($path)) { - $output->writeln('File not found: ' . $path . ''); - return 1; - } - $json = file_get_contents($path); - } - if (!is_string($json) || strlen($json) < 2) { - $output->writeln('Error while reading json'); - return 1; - } - $data = json_decode($json, true); - if (!is_array($data)) { - $output->writeln('Error while parsing json'); - return 1; - } - - $isLegacy = isset($data['user']) || isset($data['group']); - if ($isLegacy) { - $this->importLegacyStorageService->setData($data); - $mounts = $this->importLegacyStorageService->getAllStorages(); - foreach ($mounts as $mount) { - if ($mount->getBackendOption('password') === false) { - $output->writeln('Failed to decrypt password'); - return 1; - } - } - } else { - if (!isset($data[0])) { //normalize to an array of mounts - $data = [$data]; - } - $mounts = array_map([$this, 'parseData'], $data); - } - - if ($user) { - // ensure applicables are correct for personal mounts - foreach ($mounts as $mount) { - $mount->setApplicableGroups([]); - $mount->setApplicableUsers([$user]); - } - } - - $storageService = $this->getStorageService($user); - - $existingMounts = $storageService->getAllStorages(); - - foreach ($mounts as $mount) { - foreach ($existingMounts as $existingMount) { - if ( - $existingMount->getMountPoint() === $mount->getMountPoint() && - $existingMount->getApplicableGroups() === $mount->getApplicableGroups() && - $existingMount->getApplicableUsers() == $mount->getApplicableUsers() && - $existingMount->getBackendOptions() == $mount->getBackendOptions() - ) { - $output->writeln("Duplicate mount (" . $mount->getMountPoint() . ")"); - return 1; - } - } - } - - if ($input->getOption('dry')) { - if (count($mounts) === 0) { - $output->writeln('No mounts to be imported'); - return 1; - } - $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); - $listInput = new ArrayInput([], $listCommand->getDefinition()); - $listInput->setOption('output', $input->getOption('output')); - $listInput->setOption('show-password', true); - $listCommand->listMounts($user, $mounts, $listInput, $output); - } else { - foreach ($mounts as $mount) { - $storageService->addStorage($mount); - } - } - return 0; - } - - private function parseData(array $data) { - $mount = new StorageConfig($data['mount_id']); - $mount->setMountPoint($data['mount_point']); - $mount->setBackend($this->getBackendByClass($data['storage'])); - $authBackend = $this->backendService->getAuthMechanism($data['authentication_type']); - $mount->setAuthMechanism($authBackend); - $mount->setBackendOptions($data['configuration']); - $mount->setMountOptions($data['options']); - $mount->setApplicableUsers(isset($data['applicable_users']) ? $data['applicable_users'] : []); - $mount->setApplicableGroups(isset($data['applicable_groups']) ? $data['applicable_groups'] : []); - return $mount; - } - - private function getBackendByClass($className) { - $backends = $this->backendService->getBackends(); - foreach ($backends as $backend) { - if ($backend->getStorageClass() === $className) { - return $backend; - } - } - } - - protected function getStorageService($userId) { - if (!empty($userId)) { - $user = $this->userManager->get($userId); - if (is_null($user)) { - throw new NoUserException("user $userId not found"); - } - $this->userSession->setUser($user); - return $this->userService; - } else { - return $this->globalService; - } - } -} diff --git a/apps/files_external/command/listcommand.php b/apps/files_external/command/listcommand.php deleted file mode 100644 index 5a0794be4c5..00000000000 --- a/apps/files_external/command/listcommand.php +++ /dev/null @@ -1,248 +0,0 @@ - - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_External\Command; - -use OC\Core\Command\Base; -use OC\User\NoUserException; -use OCA\Files_external\Lib\StorageConfig; -use OCA\Files_external\Service\GlobalStoragesService; -use OCA\Files_external\Service\UserStoragesService; -use OCP\IUserManager; -use OCP\IUserSession; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Helper\Table; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Output\OutputInterface; - -class ListCommand extends Base { - /** - * @var GlobalStoragesService - */ - protected $globalService; - - /** - * @var UserStoragesService - */ - protected $userService; - - /** - * @var IUserSession - */ - protected $userSession; - - /** - * @var IUserManager - */ - protected $userManager; - - function __construct(GlobalStoragesService $globalService, UserStoragesService $userService, IUserSession $userSession, IUserManager $userManager) { - parent::__construct(); - $this->globalService = $globalService; - $this->userService = $userService; - $this->userSession = $userSession; - $this->userManager = $userManager; - } - - protected function configure() { - $this - ->setName('files_external:list') - ->setDescription('List configured mounts') - ->addArgument( - 'user_id', - InputArgument::OPTIONAL, - 'user id to list the personal mounts for, if no user is provided admin mounts will be listed' - )->addOption( - 'show-password', - null, - InputOption::VALUE_NONE, - 'show passwords and secrets' - )->addOption( - 'full', - null, - InputOption::VALUE_NONE, - 'don\'t truncate long values in table output' - ); - parent::configure(); - } - - protected function execute(InputInterface $input, OutputInterface $output) { - $userId = $input->getArgument('user_id'); - $storageService = $this->getStorageService($userId); - - /** @var $mounts StorageConfig[] */ - $mounts = $storageService->getAllStorages(); - - $this->listMounts($userId, $mounts, $input, $output); - } - - /** - * @param $userId $userId - * @param StorageConfig[] $mounts - * @param InputInterface $input - * @param OutputInterface $output - */ - public function listMounts($userId, array $mounts, InputInterface $input, OutputInterface $output){ - $outputType = $input->getOption('output'); - if (count($mounts) === 0) { - if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) { - $output->writeln('[]'); - } else { - if ($userId) { - $output->writeln("No mounts configured by $userId"); - } else { - $output->writeln("No admin mounts configured"); - } - } - return; - } - - $headers = ['Mount ID', 'Mount Point', 'Storage', 'Authentication Type', 'Configuration', 'Options']; - - if (!$userId) { - $headers[] = 'Applicable Users'; - $headers[] = 'Applicable Groups'; - } - - if (!$input->getOption('show-password')) { - $hideKeys = ['password', 'refresh_token', 'token', 'client_secret', 'public_key', 'private_key']; - foreach ($mounts as $mount) { - $config = $mount->getBackendOptions(); - foreach ($config as $key => $value) { - if (in_array($key, $hideKeys)) { - $mount->setBackendOption($key, '***'); - } - } - } - } - - if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) { - $keys = array_map(function ($header) { - return strtolower(str_replace(' ', '_', $header)); - }, $headers); - - $pairs = array_map(function (StorageConfig $config) use ($keys, $userId) { - $values = [ - $config->getId(), - $config->getMountPoint(), - $config->getBackend()->getStorageClass(), - $config->getAuthMechanism()->getIdentifier(), - $config->getBackendOptions(), - $config->getMountOptions() - ]; - if (!$userId) { - $values[] = $config->getApplicableUsers(); - $values[] = $config->getApplicableGroups(); - } - - return array_combine($keys, $values); - }, $mounts); - if ($outputType === self::OUTPUT_FORMAT_JSON) { - $output->writeln(json_encode(array_values($pairs))); - } else { - $output->writeln(json_encode(array_values($pairs), JSON_PRETTY_PRINT)); - } - } else { - $full = $input->getOption('full'); - $defaultMountOptions = [ - 'encrypt' => true, - 'previews' => true, - 'filesystem_check_changes' => 1 - ]; - $rows = array_map(function (StorageConfig $config) use ($userId, $defaultMountOptions, $full) { - $storageConfig = $config->getBackendOptions(); - $keys = array_keys($storageConfig); - $values = array_values($storageConfig); - - if (!$full) { - $values = array_map(function ($value) { - if (is_string($value) && strlen($value) > 32) { - return substr($value, 0, 6) . '...' . substr($value, -6, 6); - } else { - return $value; - } - }, $values); - } - - $configStrings = array_map(function ($key, $value) { - return $key . ': ' . json_encode($value); - }, $keys, $values); - $configString = implode(', ', $configStrings); - - $mountOptions = $config->getMountOptions(); - // hide defaults - foreach ($mountOptions as $key => $value) { - if ($value === $defaultMountOptions[$key]) { - unset($mountOptions[$key]); - } - } - $keys = array_keys($mountOptions); - $values = array_values($mountOptions); - - $optionsStrings = array_map(function ($key, $value) { - return $key . ': ' . json_encode($value); - }, $keys, $values); - $optionsString = implode(', ', $optionsStrings); - - $values = [ - $config->getId(), - $config->getMountPoint(), - $config->getBackend()->getText(), - $config->getAuthMechanism()->getText(), - $configString, - $optionsString - ]; - - if (!$userId) { - $applicableUsers = implode(', ', $config->getApplicableUsers()); - $applicableGroups = implode(', ', $config->getApplicableGroups()); - if ($applicableUsers === '' && $applicableGroups === '') { - $applicableUsers = 'All'; - } - $values[] = $applicableUsers; - $values[] = $applicableGroups; - } - - return $values; - }, $mounts); - - $table = new Table($output); - $table->setHeaders($headers); - $table->setRows($rows); - $table->render(); - } - } - - protected function getStorageService($userId) { - if (!empty($userId)) { - $user = $this->userManager->get($userId); - if (is_null($user)) { - throw new NoUserException("user $userId not found"); - } - $this->userSession->setUser($user); - return $this->userService; - } else { - return $this->globalService; - } - } -} diff --git a/apps/files_external/command/option.php b/apps/files_external/command/option.php deleted file mode 100644 index 292005134cb..00000000000 --- a/apps/files_external/command/option.php +++ /dev/null @@ -1,85 +0,0 @@ - - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_External\Command; - -use OC\Core\Command\Base; -use OCA\Files_external\Lib\StorageConfig; -use OCA\Files_external\Service\GlobalStoragesService; -use OCA\Files_external\Service\UserStoragesService; -use OCP\IUserManager; -use OCP\IUserSession; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Helper\Table; -use Symfony\Component\Console\Helper\TableHelper; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Output\OutputInterface; - -class Option extends Config { - protected function configure() { - $this - ->setName('files_external:option') - ->setDescription('Manage mount options for a mount') - ->addArgument( - 'mount_id', - InputArgument::REQUIRED, - 'The id of the mount to edit' - )->addArgument( - 'key', - InputArgument::REQUIRED, - 'key of the mount option to set/get' - )->addArgument( - 'value', - InputArgument::OPTIONAL, - 'value to set the mount option to, when no value is provided the existing value will be printed' - ); - } - - /** - * @param StorageConfig $mount - * @param string $key - * @param OutputInterface $output - */ - protected function getOption(StorageConfig $mount, $key, OutputInterface $output) { - $value = $mount->getMountOption($key); - if (!is_string($value)) { // show bools and objects correctly - $value = json_encode($value); - } - $output->writeln($value); - } - - /** - * @param StorageConfig $mount - * @param string $key - * @param string $value - * @param OutputInterface $output - */ - protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output) { - $decoded = json_decode($value, true); - if (!is_null($decoded)) { - $value = $decoded; - } - $mount->setMountOption($key, $value); - $this->globalService->updateStorage($mount); - } -} diff --git a/apps/files_external/command/verify.php b/apps/files_external/command/verify.php deleted file mode 100644 index f985cb401af..00000000000 --- a/apps/files_external/command/verify.php +++ /dev/null @@ -1,145 +0,0 @@ - - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_External\Command; - -use OC\Core\Command\Base; -use OCA\Files_External\Lib\Auth\AuthMechanism; -use OCA\Files_External\Lib\Backend\Backend; -use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException; -use OCA\Files_external\Lib\StorageConfig; -use OCA\Files_external\NotFoundException; -use OCA\Files_external\Service\GlobalStoragesService; -use OCP\Files\StorageNotAvailableException; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Helper\Table; -use Symfony\Component\Console\Helper\TableHelper; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Output\OutputInterface; - -class Verify extends Base { - /** - * @var GlobalStoragesService - */ - protected $globalService; - - function __construct(GlobalStoragesService $globalService) { - parent::__construct(); - $this->globalService = $globalService; - } - - protected function configure() { - $this - ->setName('files_external:verify') - ->setDescription('Verify mount configuration') - ->addArgument( - 'mount_id', - InputArgument::REQUIRED, - 'The id of the mount to check' - )->addOption( - 'config', - 'c', - InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, - 'Additional config option to set before checking in key=value pairs, required for certain auth backends such as login credentails' - ); - parent::configure(); - } - - protected function execute(InputInterface $input, OutputInterface $output) { - $mountId = $input->getArgument('mount_id'); - $configInput = $input->getOption('config'); - - try { - $mount = $this->globalService->getStorage($mountId); - } catch (NotFoundException $e) { - $output->writeln('Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"'); - return 404; - } - - $this->updateStorageStatus($mount, $configInput, $output); - - $this->writeArrayInOutputFormat($input, $output, [ - 'status' => StorageNotAvailableException::getStateCodeName($mount->getStatus()), - 'code' => $mount->getStatus(), - 'message' => $mount->getStatusMessage() - ]); - } - - private function manipulateStorageConfig(StorageConfig $storage) { - /** @var AuthMechanism */ - $authMechanism = $storage->getAuthMechanism(); - $authMechanism->manipulateStorageConfig($storage); - /** @var Backend */ - $backend = $storage->getBackend(); - $backend->manipulateStorageConfig($storage); - } - - private function updateStorageStatus(StorageConfig &$storage, $configInput, OutputInterface $output) { - try { - try { - $this->manipulateStorageConfig($storage); - } catch (InsufficientDataForMeaningfulAnswerException $e) { - if (count($configInput) === 0) { // extra config options might solve the error - throw $e; - } - } - - foreach ($configInput as $configOption) { - if (!strpos($configOption, '=')) { - $output->writeln('Invalid mount configuration option "' . $configOption . '"'); - return; - } - list($key, $value) = explode('=', $configOption, 2); - $storage->setBackendOption($key, $value); - } - - /** @var Backend */ - $backend = $storage->getBackend(); - // update status (can be time-consuming) - $storage->setStatus( - \OC_Mount_Config::getBackendStatus( - $backend->getStorageClass(), - $storage->getBackendOptions(), - false - ) - ); - } catch (InsufficientDataForMeaningfulAnswerException $e) { - $status = $e->getCode() ? $e->getCode() : StorageNotAvailableException::STATUS_INDETERMINATE; - $storage->setStatus( - $status, - $e->getMessage() - ); - } catch (StorageNotAvailableException $e) { - $storage->setStatus( - $e->getCode(), - $e->getMessage() - ); - } catch (\Exception $e) { - // FIXME: convert storage exceptions to StorageNotAvailableException - $storage->setStatus( - StorageNotAvailableException::STATUS_ERROR, - get_class($e) . ': ' . $e->getMessage() - ); - } - } -} diff --git a/apps/files_external/controller/ajaxcontroller.php b/apps/files_external/controller/ajaxcontroller.php deleted file mode 100644 index c3df3fa8522..00000000000 --- a/apps/files_external/controller/ajaxcontroller.php +++ /dev/null @@ -1,64 +0,0 @@ - - * @author Robin Appelman - * @author Robin McCorkell - * @author Ross Nicoll - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_External\Controller; - -use OCP\AppFramework\Controller; -use OCP\IRequest; -use OCP\AppFramework\Http\JSONResponse; -use OCA\Files_External\Lib\Auth\PublicKey\RSA; - -class AjaxController extends Controller { - /** @var RSA */ - private $rsaMechanism; - - public function __construct($appName, IRequest $request, RSA $rsaMechanism) { - parent::__construct($appName, $request); - $this->rsaMechanism = $rsaMechanism; - } - - private function generateSshKeys() { - $key = $this->rsaMechanism->createKey(); - // Replace the placeholder label with a more meaningful one - $key['publicKey'] = str_replace('phpseclib-generated-key', gethostname(), $key['publickey']); - - return $key; - } - - /** - * Generates an SSH public/private key pair. - * - * @NoAdminRequired - */ - public function getSshKeys() { - $key = $this->generateSshKeys(); - return new JSONResponse( - array('data' => array( - 'private_key' => $key['privatekey'], - 'public_key' => $key['publickey'] - ), - 'status' => 'success' - )); - } -} diff --git a/apps/files_external/controller/globalstoragescontroller.php b/apps/files_external/controller/globalstoragescontroller.php deleted file mode 100644 index b443cf4ea8f..00000000000 --- a/apps/files_external/controller/globalstoragescontroller.php +++ /dev/null @@ -1,189 +0,0 @@ - - * @author Robin McCorkell - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_External\Controller; - - -use \OCP\IConfig; -use OCP\ILogger; -use \OCP\IUserSession; -use \OCP\IRequest; -use \OCP\IL10N; -use \OCP\AppFramework\Http\DataResponse; -use \OCP\AppFramework\Controller; -use \OCP\AppFramework\Http; -use \OCA\Files_external\Service\GlobalStoragesService; -use \OCA\Files_external\NotFoundException; -use \OCA\Files_external\Lib\StorageConfig; -use \OCA\Files_External\Service\BackendService; - -/** - * Global storages controller - */ -class GlobalStoragesController extends StoragesController { - /** - * Creates a new global storages controller. - * - * @param string $AppName application name - * @param IRequest $request request object - * @param IL10N $l10n l10n service - * @param GlobalStoragesService $globalStoragesService storage service - * @param ILogger $logger - */ - public function __construct( - $AppName, - IRequest $request, - IL10N $l10n, - GlobalStoragesService $globalStoragesService, - ILogger $logger - ) { - parent::__construct( - $AppName, - $request, - $l10n, - $globalStoragesService, - $logger - ); - } - - /** - * Create an external storage entry. - * - * @param string $mountPoint storage mount point - * @param string $backend backend identifier - * @param string $authMechanism authentication mechanism identifier - * @param array $backendOptions backend-specific options - * @param array $mountOptions mount-specific options - * @param array $applicableUsers users for which to mount the storage - * @param array $applicableGroups groups for which to mount the storage - * @param int $priority priority - * - * @return DataResponse - */ - public function create( - $mountPoint, - $backend, - $authMechanism, - $backendOptions, - $mountOptions, - $applicableUsers, - $applicableGroups, - $priority - ) { - $newStorage = $this->createStorage( - $mountPoint, - $backend, - $authMechanism, - $backendOptions, - $mountOptions, - $applicableUsers, - $applicableGroups, - $priority - ); - if ($newStorage instanceof DataResponse) { - return $newStorage; - } - - $response = $this->validate($newStorage); - if (!empty($response)) { - return $response; - } - - $newStorage = $this->service->addStorage($newStorage); - - $this->updateStorageStatus($newStorage); - - return new DataResponse( - $newStorage, - Http::STATUS_CREATED - ); - } - - /** - * Update an external storage entry. - * - * @param int $id storage id - * @param string $mountPoint storage mount point - * @param string $backend backend identifier - * @param string $authMechanism authentication mechansim identifier - * @param array $backendOptions backend-specific options - * @param array $mountOptions mount-specific options - * @param array $applicableUsers users for which to mount the storage - * @param array $applicableGroups groups for which to mount the storage - * @param int $priority priority - * - * @return DataResponse - */ - public function update( - $id, - $mountPoint, - $backend, - $authMechanism, - $backendOptions, - $mountOptions, - $applicableUsers, - $applicableGroups, - $priority - ) { - $storage = $this->createStorage( - $mountPoint, - $backend, - $authMechanism, - $backendOptions, - $mountOptions, - $applicableUsers, - $applicableGroups, - $priority - ); - if ($storage instanceof DataResponse) { - return $storage; - } - $storage->setId($id); - - $response = $this->validate($storage); - if (!empty($response)) { - return $response; - } - - try { - $storage = $this->service->updateStorage($storage); - } catch (NotFoundException $e) { - return new DataResponse( - [ - 'message' => (string)$this->l10n->t('Storage with id "%i" not found', array($id)) - ], - Http::STATUS_NOT_FOUND - ); - } - - $this->updateStorageStatus($storage); - - return new DataResponse( - $storage, - Http::STATUS_OK - ); - - } - - -} diff --git a/apps/files_external/controller/storagescontroller.php b/apps/files_external/controller/storagescontroller.php deleted file mode 100644 index 09b83104700..00000000000 --- a/apps/files_external/controller/storagescontroller.php +++ /dev/null @@ -1,342 +0,0 @@ - - * @author Robin Appelman - * @author Robin McCorkell - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_External\Controller; - - -use \OCP\IConfig; -use OCP\ILogger; -use OCP\IUser; -use \OCP\IUserSession; -use \OCP\IRequest; -use \OCP\IL10N; -use \OCP\AppFramework\Http\DataResponse; -use \OCP\AppFramework\Controller; -use \OCP\AppFramework\Http; -use \OCA\Files_external\Service\StoragesService; -use \OCA\Files_external\NotFoundException; -use \OCA\Files_external\Lib\StorageConfig; -use \OCA\Files_External\Lib\Backend\Backend; -use \OCA\Files_External\Lib\Auth\AuthMechanism; -use \OCP\Files\StorageNotAvailableException; -use \OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException; -use \OCA\Files_External\Service\BackendService; - -/** - * Base class for storages controllers - */ -abstract class StoragesController extends Controller { - - /** - * L10N service - * - * @var IL10N - */ - protected $l10n; - - /** - * Storages service - * - * @var StoragesService - */ - protected $service; - - /** - * @var ILogger - */ - protected $logger; - - /** - * Creates a new storages controller. - * - * @param string $AppName application name - * @param IRequest $request request object - * @param IL10N $l10n l10n service - * @param StoragesService $storagesService storage service - * @param ILogger $logger - */ - public function __construct( - $AppName, - IRequest $request, - IL10N $l10n, - StoragesService $storagesService, - ILogger $logger - ) { - parent::__construct($AppName, $request); - $this->l10n = $l10n; - $this->service = $storagesService; - $this->logger = $logger; - } - - /** - * Create a storage from its parameters - * - * @param string $mountPoint storage mount point - * @param string $backend backend identifier - * @param string $authMechanism authentication mechanism identifier - * @param array $backendOptions backend-specific options - * @param array|null $mountOptions mount-specific options - * @param array|null $applicableUsers users for which to mount the storage - * @param array|null $applicableGroups groups for which to mount the storage - * @param int|null $priority priority - * - * @return StorageConfig|DataResponse - */ - protected function createStorage( - $mountPoint, - $backend, - $authMechanism, - $backendOptions, - $mountOptions = null, - $applicableUsers = null, - $applicableGroups = null, - $priority = null - ) { - try { - return $this->service->createStorage( - $mountPoint, - $backend, - $authMechanism, - $backendOptions, - $mountOptions, - $applicableUsers, - $applicableGroups, - $priority - ); - } catch (\InvalidArgumentException $e) { - $this->logger->logException($e); - return new DataResponse( - [ - 'message' => (string)$this->l10n->t('Invalid backend or authentication mechanism class') - ], - Http::STATUS_UNPROCESSABLE_ENTITY - ); - } - } - - /** - * Validate storage config - * - * @param StorageConfig $storage storage config - *1 - * @return DataResponse|null returns response in case of validation error - */ - protected function validate(StorageConfig $storage) { - $mountPoint = $storage->getMountPoint(); - if ($mountPoint === '' || $mountPoint === '/') { - return new DataResponse( - array( - 'message' => (string)$this->l10n->t('Invalid mount point') - ), - Http::STATUS_UNPROCESSABLE_ENTITY - ); - } - - if ($storage->getBackendOption('objectstore')) { - // objectstore must not be sent from client side - return new DataResponse( - array( - 'message' => (string)$this->l10n->t('Objectstore forbidden') - ), - Http::STATUS_UNPROCESSABLE_ENTITY - ); - } - - /** @var Backend */ - $backend = $storage->getBackend(); - /** @var AuthMechanism */ - $authMechanism = $storage->getAuthMechanism(); - if ($backend->checkDependencies()) { - // invalid backend - return new DataResponse( - array( - 'message' => (string)$this->l10n->t('Invalid storage backend "%s"', [ - $backend->getIdentifier() - ]) - ), - Http::STATUS_UNPROCESSABLE_ENTITY - ); - } - - if (!$backend->isVisibleFor($this->service->getVisibilityType())) { - // not permitted to use backend - return new DataResponse( - array( - 'message' => (string)$this->l10n->t('Not permitted to use backend "%s"', [ - $backend->getIdentifier() - ]) - ), - Http::STATUS_UNPROCESSABLE_ENTITY - ); - } - if (!$authMechanism->isVisibleFor($this->service->getVisibilityType())) { - // not permitted to use auth mechanism - return new DataResponse( - array( - 'message' => (string)$this->l10n->t('Not permitted to use authentication mechanism "%s"', [ - $authMechanism->getIdentifier() - ]) - ), - Http::STATUS_UNPROCESSABLE_ENTITY - ); - } - - if (!$backend->validateStorage($storage)) { - // unsatisfied parameters - return new DataResponse( - array( - 'message' => (string)$this->l10n->t('Unsatisfied backend parameters') - ), - Http::STATUS_UNPROCESSABLE_ENTITY - ); - } - if (!$authMechanism->validateStorage($storage)) { - // unsatisfied parameters - return new DataResponse( - [ - 'message' => (string)$this->l10n->t('Unsatisfied authentication mechanism parameters') - ], - Http::STATUS_UNPROCESSABLE_ENTITY - ); - } - - return null; - } - - protected function manipulateStorageConfig(StorageConfig $storage) { - /** @var AuthMechanism */ - $authMechanism = $storage->getAuthMechanism(); - $authMechanism->manipulateStorageConfig($storage); - /** @var Backend */ - $backend = $storage->getBackend(); - $backend->manipulateStorageConfig($storage); - } - - /** - * Check whether the given storage is available / valid. - * - * Note that this operation can be time consuming depending - * on whether the remote storage is available or not. - * - * @param StorageConfig $storage storage configuration - */ - protected function updateStorageStatus(StorageConfig &$storage) { - try { - $this->manipulateStorageConfig($storage); - - /** @var Backend */ - $backend = $storage->getBackend(); - // update status (can be time-consuming) - $storage->setStatus( - \OC_Mount_Config::getBackendStatus( - $backend->getStorageClass(), - $storage->getBackendOptions(), - false - ) - ); - } catch (InsufficientDataForMeaningfulAnswerException $e) { - $status = $e->getCode() ? $e->getCode() : StorageNotAvailableException::STATUS_INDETERMINATE; - $storage->setStatus( - $status, - $this->l10n->t('Insufficient data: %s', [$e->getMessage()]) - ); - } catch (StorageNotAvailableException $e) { - $storage->setStatus( - $e->getCode(), - $this->l10n->t('%s', [$e->getMessage()]) - ); - } catch (\Exception $e) { - // FIXME: convert storage exceptions to StorageNotAvailableException - $storage->setStatus( - StorageNotAvailableException::STATUS_ERROR, - get_class($e).': '.$e->getMessage() - ); - } - } - - /** - * Get all storage entries - * - * @return DataResponse - */ - public function index() { - $storages = $this->service->getStorages(); - - return new DataResponse( - $storages, - Http::STATUS_OK - ); - } - - /** - * Get an external storage entry. - * - * @param int $id storage id - * - * @return DataResponse - */ - public function show($id) { - try { - $storage = $this->service->getStorage($id); - - $this->updateStorageStatus($storage); - } catch (NotFoundException $e) { - return new DataResponse( - [ - 'message' => (string)$this->l10n->t('Storage with id "%i" not found', array($id)) - ], - Http::STATUS_NOT_FOUND - ); - } - - return new DataResponse( - $storage, - Http::STATUS_OK - ); - } - - /** - * Deletes the storage with the given id. - * - * @param int $id storage id - * - * @return DataResponse - */ - public function destroy($id) { - try { - $this->service->removeStorage($id); - } catch (NotFoundException $e) { - return new DataResponse( - [ - 'message' => (string)$this->l10n->t('Storage with id "%i" not found', array($id)) - ], - Http::STATUS_NOT_FOUND - ); - } - - return new DataResponse([], Http::STATUS_NO_CONTENT); - } - -} - diff --git a/apps/files_external/controller/userglobalstoragescontroller.php b/apps/files_external/controller/userglobalstoragescontroller.php deleted file mode 100644 index 36c3740eed3..00000000000 --- a/apps/files_external/controller/userglobalstoragescontroller.php +++ /dev/null @@ -1,201 +0,0 @@ - - * @author Robin McCorkell - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_External\Controller; - -use OCA\Files_External\Lib\Auth\AuthMechanism; -use OCA\Files_External\Lib\Auth\IUserProvided; -use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException; -use OCP\ILogger; -use \OCP\IRequest; -use \OCP\IL10N; -use \OCP\AppFramework\Http\DataResponse; -use \OCP\AppFramework\Http; -use \OCA\Files_external\Service\UserGlobalStoragesService; -use \OCA\Files_external\NotFoundException; -use \OCA\Files_external\Lib\StorageConfig; -use \OCA\Files_External\Lib\Backend\Backend; -use OCP\IUserSession; - -/** - * User global storages controller - */ -class UserGlobalStoragesController extends StoragesController { - /** - * @var IUserSession - */ - private $userSession; - - /** - * Creates a new user global storages controller. - * - * @param string $AppName application name - * @param IRequest $request request object - * @param IL10N $l10n l10n service - * @param UserGlobalStoragesService $userGlobalStoragesService storage service - * @param IUserSession $userSession - */ - public function __construct( - $AppName, - IRequest $request, - IL10N $l10n, - UserGlobalStoragesService $userGlobalStoragesService, - IUserSession $userSession, - ILogger $logger - ) { - parent::__construct( - $AppName, - $request, - $l10n, - $userGlobalStoragesService, - $logger - ); - $this->userSession = $userSession; - } - - /** - * Get all storage entries - * - * @return DataResponse - * - * @NoAdminRequired - */ - public function index() { - $storages = $this->service->getUniqueStorages(); - - // remove configuration data, this must be kept private - foreach ($storages as $storage) { - $this->sanitizeStorage($storage); - } - - return new DataResponse( - $storages, - Http::STATUS_OK - ); - } - - protected function manipulateStorageConfig(StorageConfig $storage) { - /** @var AuthMechanism */ - $authMechanism = $storage->getAuthMechanism(); - $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser()); - /** @var Backend */ - $backend = $storage->getBackend(); - $backend->manipulateStorageConfig($storage, $this->userSession->getUser()); - } - - /** - * Get an external storage entry. - * - * @param int $id storage id - * @return DataResponse - * - * @NoAdminRequired - */ - public function show($id) { - try { - $storage = $this->service->getStorage($id); - - $this->updateStorageStatus($storage); - } catch (NotFoundException $e) { - return new DataResponse( - [ - 'message' => (string)$this->l10n->t('Storage with id "%i" not found', array($id)) - ], - Http::STATUS_NOT_FOUND - ); - } - - $this->sanitizeStorage($storage); - - return new DataResponse( - $storage, - Http::STATUS_OK - ); - } - - /** - * Update an external storage entry. - * Only allows setting user provided backend fields - * - * @param int $id storage id - * @param array $backendOptions backend-specific options - * - * @return DataResponse - * - * @NoAdminRequired - */ - public function update( - $id, - $backendOptions - ) { - try { - $storage = $this->service->getStorage($id); - $authMechanism = $storage->getAuthMechanism(); - if ($authMechanism instanceof IUserProvided) { - $authMechanism->saveBackendOptions($this->userSession->getUser(), $id, $backendOptions); - $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser()); - } else { - return new DataResponse( - [ - 'message' => (string)$this->l10n->t('Storage with id "%i" is not user editable', array($id)) - ], - Http::STATUS_FORBIDDEN - ); - } - } catch (NotFoundException $e) { - return new DataResponse( - [ - 'message' => (string)$this->l10n->t('Storage with id "%i" not found', array($id)) - ], - Http::STATUS_NOT_FOUND - ); - } - - $this->updateStorageStatus($storage); - $this->sanitizeStorage($storage); - - return new DataResponse( - $storage, - Http::STATUS_OK - ); - - } - - /** - * Remove sensitive data from a StorageConfig before returning it to the user - * - * @param StorageConfig $storage - */ - protected function sanitizeStorage(StorageConfig $storage) { - $storage->setBackendOptions([]); - $storage->setMountOptions([]); - - if ($storage->getAuthMechanism() instanceof IUserProvided) { - try { - $storage->getAuthMechanism()->manipulateStorageConfig($storage, $this->userSession->getUser()); - } catch (InsufficientDataForMeaningfulAnswerException $e) { - // not configured yet - } - } - } - -} diff --git a/apps/files_external/controller/userstoragescontroller.php b/apps/files_external/controller/userstoragescontroller.php deleted file mode 100644 index e53ea21f005..00000000000 --- a/apps/files_external/controller/userstoragescontroller.php +++ /dev/null @@ -1,226 +0,0 @@ - - * @author Robin McCorkell - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_External\Controller; - - -use OCA\Files_External\Lib\Auth\AuthMechanism; -use \OCP\IConfig; -use OCP\ILogger; -use OCP\IUser; -use \OCP\IUserSession; -use \OCP\IRequest; -use \OCP\IL10N; -use \OCP\AppFramework\Http\DataResponse; -use \OCP\AppFramework\Controller; -use \OCP\AppFramework\Http; -use \OCA\Files_external\Service\UserStoragesService; -use \OCA\Files_External\Service\BackendService; -use \OCA\Files_external\NotFoundException; -use \OCA\Files_external\Lib\StorageConfig; -use \OCA\Files_External\Lib\Backend\Backend; - -/** - * User storages controller - */ -class UserStoragesController extends StoragesController { - /** - * @var IUserSession - */ - private $userSession; - - /** - * Creates a new user storages controller. - * - * @param string $AppName application name - * @param IRequest $request request object - * @param IL10N $l10n l10n service - * @param UserStoragesService $userStoragesService storage service - * @param IUserSession $userSession - * @param ILogger $logger - */ - public function __construct( - $AppName, - IRequest $request, - IL10N $l10n, - UserStoragesService $userStoragesService, - IUserSession $userSession, - ILogger $logger - ) { - parent::__construct( - $AppName, - $request, - $l10n, - $userStoragesService, - $logger - ); - $this->userSession = $userSession; - } - - protected function manipulateStorageConfig(StorageConfig $storage) { - /** @var AuthMechanism */ - $authMechanism = $storage->getAuthMechanism(); - $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser()); - /** @var Backend */ - $backend = $storage->getBackend(); - $backend->manipulateStorageConfig($storage, $this->userSession->getUser()); - } - - /** - * Get all storage entries - * - * @NoAdminRequired - * - * @return DataResponse - */ - public function index() { - return parent::index(); - } - - /** - * Return storage - * - * @NoAdminRequired - * - * {@inheritdoc} - */ - public function show($id) { - return parent::show($id); - } - - /** - * Create an external storage entry. - * - * @param string $mountPoint storage mount point - * @param string $backend backend identifier - * @param string $authMechanism authentication mechanism identifier - * @param array $backendOptions backend-specific options - * @param array $mountOptions backend-specific mount options - * - * @return DataResponse - * - * @NoAdminRequired - */ - public function create( - $mountPoint, - $backend, - $authMechanism, - $backendOptions, - $mountOptions - ) { - $newStorage = $this->createStorage( - $mountPoint, - $backend, - $authMechanism, - $backendOptions, - $mountOptions - ); - if ($newStorage instanceOf DataResponse) { - return $newStorage; - } - - $response = $this->validate($newStorage); - if (!empty($response)) { - return $response; - } - - $newStorage = $this->service->addStorage($newStorage); - $this->updateStorageStatus($newStorage); - - return new DataResponse( - $newStorage, - Http::STATUS_CREATED - ); - } - - /** - * Update an external storage entry. - * - * @param int $id storage id - * @param string $mountPoint storage mount point - * @param string $backend backend identifier - * @param string $authMechanism authentication mechanism identifier - * @param array $backendOptions backend-specific options - * @param array $mountOptions backend-specific mount options - * - * @return DataResponse - * - * @NoAdminRequired - */ - public function update( - $id, - $mountPoint, - $backend, - $authMechanism, - $backendOptions, - $mountOptions - ) { - $storage = $this->createStorage( - $mountPoint, - $backend, - $authMechanism, - $backendOptions, - $mountOptions - ); - if ($storage instanceOf DataResponse) { - return $storage; - } - $storage->setId($id); - - $response = $this->validate($storage); - if (!empty($response)) { - return $response; - } - - try { - $storage = $this->service->updateStorage($storage); - } catch (NotFoundException $e) { - return new DataResponse( - [ - 'message' => (string)$this->l10n->t('Storage with id "%i" not found', array($id)) - ], - Http::STATUS_NOT_FOUND - ); - } - - $this->updateStorageStatus($storage); - - return new DataResponse( - $storage, - Http::STATUS_OK - ); - - } - - /** - * Delete storage - * - * @NoAdminRequired - * - * {@inheritdoc} - */ - public function destroy($id) { - return parent::destroy($id); - } - -} diff --git a/apps/files_external/lib/AppInfo/Application.php b/apps/files_external/lib/AppInfo/Application.php new file mode 100644 index 00000000000..d7c650526b3 --- /dev/null +++ b/apps/files_external/lib/AppInfo/Application.php @@ -0,0 +1,135 @@ + + * @author Robin Appelman + * @author Robin McCorkell + * @author Roeland Jago Douma + * @author Ross Nicoll + * @author Vincent Petry + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\AppInfo; + +use \OCP\AppFramework\App; +use OCP\AppFramework\IAppContainer; +use \OCP\IContainer; +use \OCA\Files_External\Service\BackendService; +use \OCA\Files_External\Lib\Config\IBackendProvider; +use \OCA\Files_External\Lib\Config\IAuthMechanismProvider; + +/** + * @package OCA\Files_External\AppInfo + */ +class Application extends App implements IBackendProvider, IAuthMechanismProvider { + + public function __construct(array $urlParams = array()) { + parent::__construct('files_external', $urlParams); + + $container = $this->getContainer(); + + $container->registerService('OCP\Files\Config\IUserMountCache', function (IAppContainer $c) { + return $c->getServer()->query('UserMountCache'); + }); + + $backendService = $container->query('OCA\\Files_External\\Service\\BackendService'); + $backendService->registerBackendProvider($this); + $backendService->registerAuthMechanismProvider($this); + + // app developers: do NOT depend on this! it will disappear with oC 9.0! + \OC::$server->getEventDispatcher()->dispatch( + 'OCA\\Files_External::loadAdditionalBackends' + ); + } + + /** + * Register settings templates + */ + public function registerSettings() { + $container = $this->getContainer(); + $backendService = $container->query('OCA\\Files_External\\Service\\BackendService'); + + \OCP\App::registerAdmin('files_external', 'settings'); + \OCP\App::registerPersonal('files_external', 'personal'); + } + + /** + * @{inheritdoc} + */ + public function getBackends() { + $container = $this->getContainer(); + + $backends = [ + $container->query('OCA\Files_External\Lib\Backend\Local'), + $container->query('OCA\Files_External\Lib\Backend\FTP'), + $container->query('OCA\Files_External\Lib\Backend\DAV'), + $container->query('OCA\Files_External\Lib\Backend\OwnCloud'), + $container->query('OCA\Files_External\Lib\Backend\SFTP'), + $container->query('OCA\Files_External\Lib\Backend\AmazonS3'), + $container->query('OCA\Files_External\Lib\Backend\Dropbox'), + $container->query('OCA\Files_External\Lib\Backend\Google'), + $container->query('OCA\Files_External\Lib\Backend\Swift'), + $container->query('OCA\Files_External\Lib\Backend\SFTP_Key'), + ]; + + if (!\OC_Util::runningOnWindows()) { + $backends += [ + $container->query('OCA\Files_External\Lib\Backend\SMB'), + $container->query('OCA\Files_External\Lib\Backend\SMB_OC'), + ]; + } + + return $backends; + } + + /** + * @{inheritdoc} + */ + public function getAuthMechanisms() { + $container = $this->getContainer(); + + return [ + // AuthMechanism::SCHEME_NULL mechanism + $container->query('OCA\Files_External\Lib\Auth\NullMechanism'), + + // AuthMechanism::SCHEME_BUILTIN mechanism + $container->query('OCA\Files_External\Lib\Auth\Builtin'), + + // AuthMechanism::SCHEME_PASSWORD mechanisms + $container->query('OCA\Files_External\Lib\Auth\Password\Password'), + $container->query('OCA\Files_External\Lib\Auth\Password\SessionCredentials'), + + // AuthMechanism::SCHEME_OAUTH1 mechanisms + $container->query('OCA\Files_External\Lib\Auth\OAuth1\OAuth1'), + + // AuthMechanism::SCHEME_OAUTH2 mechanisms + $container->query('OCA\Files_External\Lib\Auth\OAuth2\OAuth2'), + + // AuthMechanism::SCHEME_PUBLICKEY mechanisms + $container->query('OCA\Files_External\Lib\Auth\PublicKey\RSA'), + + // AuthMechanism::SCHEME_OPENSTACK mechanisms + $container->query('OCA\Files_External\Lib\Auth\OpenStack\OpenStack'), + $container->query('OCA\Files_External\Lib\Auth\OpenStack\Rackspace'), + + // Specialized mechanisms + $container->query('OCA\Files_External\Lib\Auth\AmazonS3\AccessKey'), + ]; + } + +} diff --git a/apps/files_external/lib/Command/Applicable.php b/apps/files_external/lib/Command/Applicable.php new file mode 100644 index 00000000000..902e76cb4cb --- /dev/null +++ b/apps/files_external/lib/Command/Applicable.php @@ -0,0 +1,154 @@ + + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Command; + +use OC\Core\Command\Base; +use OCA\Files_external\Lib\StorageConfig; +use OCA\Files_external\NotFoundException; +use OCA\Files_External\Service\GlobalStoragesService; +use OCP\IGroupManager; +use OCP\IUserManager; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class Applicable extends Base { + /** + * @var GlobalStoragesService + */ + protected $globalService; + + /** + * @var IUserManager + */ + private $userManager; + + /** + * @var IGroupManager + */ + private $groupManager; + + function __construct( + GlobalStoragesService $globalService, + IUserManager $userManager, + IGroupManager $groupManager + ) { + parent::__construct(); + $this->globalService = $globalService; + $this->userManager = $userManager; + $this->groupManager = $groupManager; + } + + protected function configure() { + $this + ->setName('files_external:applicable') + ->setDescription('Manage applicable users and groups for a mount') + ->addArgument( + 'mount_id', + InputArgument::REQUIRED, + 'The id of the mount to edit' + )->addOption( + 'add-user', + null, + InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, + 'user to add as applicable' + )->addOption( + 'remove-user', + null, + InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, + 'user to remove as applicable' + )->addOption( + 'add-group', + null, + InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, + 'group to add as applicable' + )->addOption( + 'remove-group', + null, + InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, + 'group to remove as applicable' + )->addOption( + 'remove-all', + null, + InputOption::VALUE_NONE, + 'Set the mount to be globally applicable' + ); + parent::configure(); + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $mountId = $input->getArgument('mount_id'); + try { + $mount = $this->globalService->getStorage($mountId); + } catch (NotFoundException $e) { + $output->writeln('Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts'); + return 404; + } + + if ($mount->getType() === StorageConfig::MOUNT_TYPE_PERSONAl) { + $output->writeln('Can\'t change applicables on personal mounts'); + return 1; + } + + $addUsers = $input->getOption('add-user'); + $removeUsers = $input->getOption('remove-user'); + $addGroups = $input->getOption('add-group'); + $removeGroups = $input->getOption('remove-group'); + + $applicableUsers = $mount->getApplicableUsers(); + $applicableGroups = $mount->getApplicableGroups(); + + if ((count($addUsers) + count($removeUsers) + count($addGroups) + count($removeGroups) > 0) || $input->getOption('remove-all')) { + foreach ($addUsers as $addUser) { + if (!$this->userManager->userExists($addUser)) { + $output->writeln('User "' . $addUser . '" not found'); + return 404; + } + } + foreach ($addGroups as $addGroup) { + if (!$this->groupManager->groupExists($addGroup)) { + $output->writeln('Group "' . $addGroup . '" not found'); + return 404; + } + } + + if ($input->getOption('remove-all')) { + $applicableUsers = []; + $applicableGroups = []; + } else { + $applicableUsers = array_unique(array_merge($applicableUsers, $addUsers)); + $applicableUsers = array_values(array_diff($applicableUsers, $removeUsers)); + $applicableGroups = array_unique(array_merge($applicableGroups, $addGroups)); + $applicableGroups = array_values(array_diff($applicableGroups, $removeGroups)); + } + $mount->setApplicableUsers($applicableUsers); + $mount->setApplicableGroups($applicableGroups); + $this->globalService->updateStorage($mount); + } + + $this->writeArrayInOutputFormat($input, $output, [ + 'users' => $applicableUsers, + 'groups' => $applicableGroups + ]); + } +} diff --git a/apps/files_external/lib/Command/Backends.php b/apps/files_external/lib/Command/Backends.php new file mode 100644 index 00000000000..260ea210397 --- /dev/null +++ b/apps/files_external/lib/Command/Backends.php @@ -0,0 +1,112 @@ + + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Command; + +use OC\Core\Command\Base; +use OCA\Files_External\Lib\Auth\AuthMechanism; +use OCA\Files_External\Lib\Backend\Backend; +use OCA\Files_External\Lib\DefinitionParameter; +use OCA\Files_External\Service\BackendService; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Helper\Table; +use Symfony\Component\Console\Helper\TableHelper; +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Input\Input; +use Symfony\Component\Console\Output\OutputInterface; + +class Backends extends Base { + /** @var BackendService */ + private $backendService; + + function __construct(BackendService $backendService + ) { + parent::__construct(); + + $this->backendService = $backendService; + } + + protected function configure() { + $this + ->setName('files_external:backends') + ->setDescription('Show available authentication and storage backends') + ->addArgument( + 'type', + InputArgument::OPTIONAL, + 'only show backends of a certain type. Possible values are "authentication" or "storage"' + )->addArgument( + 'backend', + InputArgument::OPTIONAL, + 'only show information of a specific backend' + ); + parent::configure(); + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $authBackends = $this->backendService->getAuthMechanisms(); + $storageBackends = $this->backendService->getBackends(); + + $data = [ + 'authentication' => array_map([$this, 'serializeAuthBackend'], $authBackends), + 'storage' => array_map([$this, 'serializeAuthBackend'], $storageBackends) + ]; + + $type = $input->getArgument('type'); + $backend = $input->getArgument('backend'); + if ($type) { + if (!isset($data[$type])) { + $output->writeln('Invalid type "' . $type . '". Possible values are "authentication" or "storage"'); + return 1; + } + $data = $data[$type]; + + if ($backend) { + if (!isset($data[$backend])) { + $output->writeln('Unknown backend "' . $backend . '" of type "' . $type . '"'); + return 1; + } + $data = $data[$backend]; + } + } + + $this->writeArrayInOutputFormat($input, $output, $data); + } + + private function serializeAuthBackend(\JsonSerializable $backend) { + $data = $backend->jsonSerialize(); + $result = [ + 'name' => $data['name'], + 'identifier' => $data['identifier'], + 'configuration' => array_map(function (DefinitionParameter $parameter) { + return $parameter->getTypeName(); + }, $data['configuration']) + ]; + if ($backend instanceof Backend) { + $result['storage_class'] = $backend->getStorageClass(); + $authBackends = $this->backendService->getAuthMechanismsByScheme(array_keys($backend->getAuthSchemes())); + $result['supported_authentication_backends'] = array_keys($authBackends); + } + return $result; + } +} diff --git a/apps/files_external/lib/Command/Config.php b/apps/files_external/lib/Command/Config.php new file mode 100644 index 00000000000..873fece86d7 --- /dev/null +++ b/apps/files_external/lib/Command/Config.php @@ -0,0 +1,120 @@ + + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Command; + +use OC\Core\Command\Base; +use OCA\Files_external\Lib\StorageConfig; +use OCA\Files_external\NotFoundException; +use OCA\Files_External\Service\GlobalStoragesService; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Helper\Table; +use Symfony\Component\Console\Helper\TableHelper; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class Config extends Base { + /** + * @var GlobalStoragesService + */ + protected $globalService; + + function __construct(GlobalStoragesService $globalService) { + parent::__construct(); + $this->globalService = $globalService; + } + + protected function configure() { + $this + ->setName('files_external:config') + ->setDescription('Manage backend configuration for a mount') + ->addArgument( + 'mount_id', + InputArgument::REQUIRED, + 'The id of the mount to edit' + )->addArgument( + 'key', + InputArgument::REQUIRED, + 'key of the config option to set/get' + )->addArgument( + 'value', + InputArgument::OPTIONAL, + 'value to set the config option to, when no value is provided the existing value will be printed' + ); + parent::configure(); + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $mountId = $input->getArgument('mount_id'); + $key = $input->getArgument('key'); + try { + $mount = $this->globalService->getStorage($mountId); + } catch (NotFoundException $e) { + $output->writeln('Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"'); + return 404; + } + + $value = $input->getArgument('value'); + if ($value) { + $this->setOption($mount, $key, $value, $output); + } else { + $this->getOption($mount, $key, $output); + } + } + + /** + * @param StorageConfig $mount + * @param string $key + * @param OutputInterface $output + */ + protected function getOption(StorageConfig $mount, $key, OutputInterface $output) { + if ($key === 'mountpoint' || $key === 'mount_point') { + $value = $mount->getMountPoint(); + } else { + $value = $mount->getBackendOption($key); + } + if (!is_string($value)) { // show bools and objects correctly + $value = json_encode($value); + } + $output->writeln($value); + } + + /** + * @param StorageConfig $mount + * @param string $key + * @param string $value + * @param OutputInterface $output + */ + protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output) { + $decoded = json_decode($value, true); + if (!is_null($decoded)) { + $value = $decoded; + } + if ($key === 'mountpoint' || $key === 'mount_point') { + $mount->setMountPoint($value); + } else { + $mount->setBackendOption($key, $value); + } + $this->globalService->updateStorage($mount); + } +} diff --git a/apps/files_external/lib/Command/Create.php b/apps/files_external/lib/Command/Create.php new file mode 100644 index 00000000000..17ad7ef1a16 --- /dev/null +++ b/apps/files_external/lib/Command/Create.php @@ -0,0 +1,221 @@ + + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Command; + +use OC\Core\Command\Base; +use OC\Files\Filesystem; +use OC\User\NoUserException; +use OCA\Files_External\Lib\Auth\AuthMechanism; +use OCA\Files_External\Lib\Backend\Backend; +use OCA\Files_External\Lib\DefinitionParameter; +use OCA\Files_external\Lib\StorageConfig; +use OCA\Files_External\Service\BackendService; +use OCA\Files_External\Service\GlobalStoragesService; +use OCA\Files_External\Service\UserStoragesService; +use OCP\IUserManager; +use OCP\IUserSession; +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class Create extends Base { + /** + * @var GlobalStoragesService + */ + private $globalService; + + /** + * @var UserStoragesService + */ + private $userService; + + /** + * @var IUserManager + */ + private $userManager; + + /** @var BackendService */ + private $backendService; + + /** @var IUserSession */ + private $userSession; + + function __construct(GlobalStoragesService $globalService, + UserStoragesService $userService, + IUserManager $userManager, + IUserSession $userSession, + BackendService $backendService + ) { + parent::__construct(); + $this->globalService = $globalService; + $this->userService = $userService; + $this->userManager = $userManager; + $this->userSession = $userSession; + $this->backendService = $backendService; + } + + protected function configure() { + $this + ->setName('files_external:create') + ->setDescription('Create a new mount configuration') + ->addOption( + 'user', + null, + InputOption::VALUE_OPTIONAL, + 'user to add the mount configuration for, if not set the mount will be added as system mount' + ) + ->addArgument( + 'mount_point', + InputArgument::REQUIRED, + 'mount point for the new mount' + ) + ->addArgument( + 'storage_backend', + InputArgument::REQUIRED, + 'storage backend identifier for the new mount, see `occ files_external:backends` for possible values' + ) + ->addArgument( + 'authentication_backend', + InputArgument::REQUIRED, + 'authentication backend identifier for the new mount, see `occ files_external:backends` for possible values' + ) + ->addOption( + 'config', + 'c', + InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, + 'Mount configuration option in key=value format' + ) + ->addOption( + 'dry', + null, + InputOption::VALUE_NONE, + 'Don\'t save the created mount, only list the new mount' + ); + parent::configure(); + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $user = $input->getOption('user'); + $mountPoint = $input->getArgument('mount_point'); + $storageIdentifier = $input->getArgument('storage_backend'); + $authIdentifier = $input->getArgument('authentication_backend'); + $configInput = $input->getOption('config'); + + $storageBackend = $this->backendService->getBackend($storageIdentifier); + $authBackend = $this->backendService->getAuthMechanism($authIdentifier); + + if (!Filesystem::isValidPath($mountPoint)) { + $output->writeln('Invalid mountpoint "' . $mountPoint . '"'); + return 1; + } + if (is_null($storageBackend)) { + $output->writeln('Storage backend with identifier "' . $storageIdentifier . '" not found (see `occ files_external:backends` for possible values)'); + return 404; + } + if (is_null($authBackend)) { + $output->writeln('Authentication backend with identifier "' . $authIdentifier . '" not found (see `occ files_external:backends` for possible values)'); + return 404; + } + $supportedSchemes = array_keys($storageBackend->getAuthSchemes()); + if (!in_array($authBackend->getScheme(), $supportedSchemes)) { + $output->writeln('Authentication backend "' . $authIdentifier . '" not valid for storage backend "' . $storageIdentifier . '" (see `occ files_external:backends storage ' . $storageIdentifier . '` for possible values)'); + return 1; + } + + $config = []; + foreach ($configInput as $configOption) { + if (!strpos($configOption, '=')) { + $output->writeln('Invalid mount configuration option "' . $configOption . '"'); + return 1; + } + list($key, $value) = explode('=', $configOption, 2); + if (!$this->validateParam($key, $value, $storageBackend, $authBackend)) { + $output->writeln('Unknown configuration for backends "' . $key . '"'); + return 1; + } + $config[$key] = $value; + } + + $mount = new StorageConfig(); + $mount->setMountPoint($mountPoint); + $mount->setBackend($storageBackend); + $mount->setAuthMechanism($authBackend); + $mount->setBackendOptions($config); + + if ($user) { + if (!$this->userManager->userExists($user)) { + $output->writeln('User "' . $user . '" not found'); + return 1; + } + $mount->setApplicableUsers([$user]); + } + + if ($input->getOption('dry')) { + $this->showMount($user, $mount, $input, $output); + } else { + $this->getStorageService($user)->addStorage($mount); + if ($input->getOption('output') === self::OUTPUT_FORMAT_PLAIN) { + $output->writeln('Storage created with id ' . $mount->getId() . ''); + } else { + $output->writeln($mount->getId()); + } + } + return 0; + } + + private function validateParam($key, &$value, Backend $storageBackend, AuthMechanism $authBackend) { + $params = array_merge($storageBackend->getParameters(), $authBackend->getParameters()); + foreach ($params as $param) { + /** @var DefinitionParameter $param */ + if ($param->getName() === $key) { + if ($param->getType() === DefinitionParameter::VALUE_BOOLEAN) { + $value = ($value === 'true'); + } + return true; + } + } + return false; + } + + private function showMount($user, StorageConfig $mount, InputInterface $input, OutputInterface $output) { + $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); + $listInput = new ArrayInput([], $listCommand->getDefinition()); + $listInput->setOption('output', $input->getOption('output')); + $listInput->setOption('show-password', true); + $listCommand->listMounts($user, [$mount], $listInput, $output); + } + + protected function getStorageService($userId) { + if (!empty($userId)) { + $user = $this->userManager->get($userId); + if (is_null($user)) { + throw new NoUserException("user $userId not found"); + } + $this->userSession->setUser($user); + return $this->userService; + } else { + return $this->globalService; + } + } +} diff --git a/apps/files_external/lib/Command/Delete.php b/apps/files_external/lib/Command/Delete.php new file mode 100644 index 00000000000..a3aef7f3303 --- /dev/null +++ b/apps/files_external/lib/Command/Delete.php @@ -0,0 +1,110 @@ + + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Command; + +use OC\Core\Command\Base; +use OCA\Files_external\NotFoundException; +use OCA\Files_External\Service\GlobalStoragesService; +use OCA\Files_External\Service\UserStoragesService; +use OCP\IUserManager; +use OCP\IUserSession; +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Question\ConfirmationQuestion; + +class Delete extends Base { + /** + * @var GlobalStoragesService + */ + protected $globalService; + + /** + * @var UserStoragesService + */ + protected $userService; + + /** + * @var IUserSession + */ + protected $userSession; + + /** + * @var IUserManager + */ + protected $userManager; + + function __construct(GlobalStoragesService $globalService, UserStoragesService $userService, IUserSession $userSession, IUserManager $userManager) { + parent::__construct(); + $this->globalService = $globalService; + $this->userService = $userService; + $this->userSession = $userSession; + $this->userManager = $userManager; + } + + protected function configure() { + $this + ->setName('files_external:delete') + ->setDescription('Delete an external mount') + ->addArgument( + 'mount_id', + InputArgument::REQUIRED, + 'The id of the mount to edit' + )->addOption( + 'yes', + 'y', + InputOption::VALUE_NONE, + 'Skip confirmation' + ); + parent::configure(); + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $mountId = $input->getArgument('mount_id'); + try { + $mount = $this->globalService->getStorage($mountId); + } catch (NotFoundException $e) { + $output->writeln('Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"'); + return 404; + } + + $noConfirm = $input->getOption('yes'); + + if (!$noConfirm) { + $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); + $listInput = new ArrayInput([], $listCommand->getDefinition()); + $listInput->setOption('output', $input->getOption('output')); + $listCommand->listMounts(null, [$mount], $listInput, $output); + + $questionHelper = $this->getHelper('question'); + $question = new ConfirmationQuestion('Delete this mount? [y/N] ', false); + + if (!$questionHelper->ask($input, $output, $question)) { + return; + } + } + + $this->globalService->removeStorage($mountId); + } +} diff --git a/apps/files_external/lib/Command/Export.php b/apps/files_external/lib/Command/Export.php new file mode 100644 index 00000000000..09c5ea8a9df --- /dev/null +++ b/apps/files_external/lib/Command/Export.php @@ -0,0 +1,56 @@ + + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Command; + +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Helper\Table; +use Symfony\Component\Console\Helper\TableHelper; +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Input\Input; +use Symfony\Component\Console\Output\OutputInterface; + +class Export extends ListCommand { + + protected function configure() { + $this + ->setName('files_external:export') + ->setDescription('Export mount configurations') + ->addArgument( + 'user_id', + InputArgument::OPTIONAL, + 'user id to export the personal mounts for, if no user is provided admin mounts will be exported' + ); + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); + $listInput = new ArrayInput([], $listCommand->getDefinition()); + $listInput->setArgument('user_id', $input->getArgument('user_id')); + $listInput->setOption('output', 'json_pretty'); + $listInput->setOption('show-password', true); + $listInput->setOption('full', true); + $listCommand->execute($listInput, $output); + } +} diff --git a/apps/files_external/lib/Command/Import.php b/apps/files_external/lib/Command/Import.php new file mode 100644 index 00000000000..55528b66472 --- /dev/null +++ b/apps/files_external/lib/Command/Import.php @@ -0,0 +1,223 @@ + + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Command; + +use OC\Core\Command\Base; +use OC\User\NoUserException; +use OCA\Files_external\Lib\StorageConfig; +use OCA\Files_External\Service\BackendService; +use OCA\Files_External\Service\GlobalStoragesService; +use OCA\Files_External\Service\ImportLegacyStoragesService; +use OCA\Files_External\Service\UserStoragesService; +use OCP\IUserManager; +use OCP\IUserSession; +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class Import extends Base { + /** + * @var GlobalStoragesService + */ + private $globalService; + + /** + * @var UserStoragesService + */ + private $userService; + + /** + * @var IUserSession + */ + private $userSession; + + /** + * @var IUserManager + */ + private $userManager; + + /** @var ImportLegacyStoragesService */ + private $importLegacyStorageService; + + /** @var BackendService */ + private $backendService; + + function __construct(GlobalStoragesService $globalService, + UserStoragesService $userService, + IUserSession $userSession, + IUserManager $userManager, + ImportLegacyStoragesService $importLegacyStorageService, + BackendService $backendService + ) { + parent::__construct(); + $this->globalService = $globalService; + $this->userService = $userService; + $this->userSession = $userSession; + $this->userManager = $userManager; + $this->importLegacyStorageService = $importLegacyStorageService; + $this->backendService = $backendService; + } + + protected function configure() { + $this + ->setName('files_external:import') + ->setDescription('Import mount configurations') + ->addOption( + 'user', + null, + InputOption::VALUE_OPTIONAL, + 'user to add the mount configurations for, if not set the mount will be added as system mount' + ) + ->addArgument( + 'path', + InputArgument::REQUIRED, + 'path to a json file containing the mounts to import, use "-" to read from stdin' + ) + ->addOption( + 'dry', + null, + InputOption::VALUE_NONE, + 'Don\'t save the imported mounts, only list the new mounts' + ); + parent::configure(); + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $user = $input->getOption('user'); + $path = $input->getArgument('path'); + if ($path === '-') { + $json = file_get_contents('php://stdin'); + } else { + if (!file_exists($path)) { + $output->writeln('File not found: ' . $path . ''); + return 1; + } + $json = file_get_contents($path); + } + if (!is_string($json) || strlen($json) < 2) { + $output->writeln('Error while reading json'); + return 1; + } + $data = json_decode($json, true); + if (!is_array($data)) { + $output->writeln('Error while parsing json'); + return 1; + } + + $isLegacy = isset($data['user']) || isset($data['group']); + if ($isLegacy) { + $this->importLegacyStorageService->setData($data); + $mounts = $this->importLegacyStorageService->getAllStorages(); + foreach ($mounts as $mount) { + if ($mount->getBackendOption('password') === false) { + $output->writeln('Failed to decrypt password'); + return 1; + } + } + } else { + if (!isset($data[0])) { //normalize to an array of mounts + $data = [$data]; + } + $mounts = array_map([$this, 'parseData'], $data); + } + + if ($user) { + // ensure applicables are correct for personal mounts + foreach ($mounts as $mount) { + $mount->setApplicableGroups([]); + $mount->setApplicableUsers([$user]); + } + } + + $storageService = $this->getStorageService($user); + + $existingMounts = $storageService->getAllStorages(); + + foreach ($mounts as $mount) { + foreach ($existingMounts as $existingMount) { + if ( + $existingMount->getMountPoint() === $mount->getMountPoint() && + $existingMount->getApplicableGroups() === $mount->getApplicableGroups() && + $existingMount->getApplicableUsers() == $mount->getApplicableUsers() && + $existingMount->getBackendOptions() == $mount->getBackendOptions() + ) { + $output->writeln("Duplicate mount (" . $mount->getMountPoint() . ")"); + return 1; + } + } + } + + if ($input->getOption('dry')) { + if (count($mounts) === 0) { + $output->writeln('No mounts to be imported'); + return 1; + } + $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); + $listInput = new ArrayInput([], $listCommand->getDefinition()); + $listInput->setOption('output', $input->getOption('output')); + $listInput->setOption('show-password', true); + $listCommand->listMounts($user, $mounts, $listInput, $output); + } else { + foreach ($mounts as $mount) { + $storageService->addStorage($mount); + } + } + return 0; + } + + private function parseData(array $data) { + $mount = new StorageConfig($data['mount_id']); + $mount->setMountPoint($data['mount_point']); + $mount->setBackend($this->getBackendByClass($data['storage'])); + $authBackend = $this->backendService->getAuthMechanism($data['authentication_type']); + $mount->setAuthMechanism($authBackend); + $mount->setBackendOptions($data['configuration']); + $mount->setMountOptions($data['options']); + $mount->setApplicableUsers(isset($data['applicable_users']) ? $data['applicable_users'] : []); + $mount->setApplicableGroups(isset($data['applicable_groups']) ? $data['applicable_groups'] : []); + return $mount; + } + + private function getBackendByClass($className) { + $backends = $this->backendService->getBackends(); + foreach ($backends as $backend) { + if ($backend->getStorageClass() === $className) { + return $backend; + } + } + } + + protected function getStorageService($userId) { + if (!empty($userId)) { + $user = $this->userManager->get($userId); + if (is_null($user)) { + throw new NoUserException("user $userId not found"); + } + $this->userSession->setUser($user); + return $this->userService; + } else { + return $this->globalService; + } + } +} diff --git a/apps/files_external/lib/Command/ListCommand.php b/apps/files_external/lib/Command/ListCommand.php new file mode 100644 index 00000000000..cbe1913ccfc --- /dev/null +++ b/apps/files_external/lib/Command/ListCommand.php @@ -0,0 +1,247 @@ + + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Command; + +use OC\Core\Command\Base; +use OC\User\NoUserException; +use OCA\Files_external\Lib\StorageConfig; +use OCA\Files_External\Service\GlobalStoragesService; +use OCA\Files_External\Service\UserStoragesService; +use OCP\IUserManager; +use OCP\IUserSession; +use Symfony\Component\Console\Helper\Table; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class ListCommand extends Base { + /** + * @var GlobalStoragesService + */ + protected $globalService; + + /** + * @var UserStoragesService + */ + protected $userService; + + /** + * @var IUserSession + */ + protected $userSession; + + /** + * @var IUserManager + */ + protected $userManager; + + function __construct(GlobalStoragesService $globalService, UserStoragesService $userService, IUserSession $userSession, IUserManager $userManager) { + parent::__construct(); + $this->globalService = $globalService; + $this->userService = $userService; + $this->userSession = $userSession; + $this->userManager = $userManager; + } + + protected function configure() { + $this + ->setName('files_external:list') + ->setDescription('List configured mounts') + ->addArgument( + 'user_id', + InputArgument::OPTIONAL, + 'user id to list the personal mounts for, if no user is provided admin mounts will be listed' + )->addOption( + 'show-password', + null, + InputOption::VALUE_NONE, + 'show passwords and secrets' + )->addOption( + 'full', + null, + InputOption::VALUE_NONE, + 'don\'t truncate long values in table output' + ); + parent::configure(); + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $userId = $input->getArgument('user_id'); + $storageService = $this->getStorageService($userId); + + /** @var $mounts StorageConfig[] */ + $mounts = $storageService->getAllStorages(); + + $this->listMounts($userId, $mounts, $input, $output); + } + + /** + * @param $userId $userId + * @param StorageConfig[] $mounts + * @param InputInterface $input + * @param OutputInterface $output + */ + public function listMounts($userId, array $mounts, InputInterface $input, OutputInterface $output){ + $outputType = $input->getOption('output'); + if (count($mounts) === 0) { + if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) { + $output->writeln('[]'); + } else { + if ($userId) { + $output->writeln("No mounts configured by $userId"); + } else { + $output->writeln("No admin mounts configured"); + } + } + return; + } + + $headers = ['Mount ID', 'Mount Point', 'Storage', 'Authentication Type', 'Configuration', 'Options']; + + if (!$userId) { + $headers[] = 'Applicable Users'; + $headers[] = 'Applicable Groups'; + } + + if (!$input->getOption('show-password')) { + $hideKeys = ['password', 'refresh_token', 'token', 'client_secret', 'public_key', 'private_key']; + foreach ($mounts as $mount) { + $config = $mount->getBackendOptions(); + foreach ($config as $key => $value) { + if (in_array($key, $hideKeys)) { + $mount->setBackendOption($key, '***'); + } + } + } + } + + if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) { + $keys = array_map(function ($header) { + return strtolower(str_replace(' ', '_', $header)); + }, $headers); + + $pairs = array_map(function (StorageConfig $config) use ($keys, $userId) { + $values = [ + $config->getId(), + $config->getMountPoint(), + $config->getBackend()->getStorageClass(), + $config->getAuthMechanism()->getIdentifier(), + $config->getBackendOptions(), + $config->getMountOptions() + ]; + if (!$userId) { + $values[] = $config->getApplicableUsers(); + $values[] = $config->getApplicableGroups(); + } + + return array_combine($keys, $values); + }, $mounts); + if ($outputType === self::OUTPUT_FORMAT_JSON) { + $output->writeln(json_encode(array_values($pairs))); + } else { + $output->writeln(json_encode(array_values($pairs), JSON_PRETTY_PRINT)); + } + } else { + $full = $input->getOption('full'); + $defaultMountOptions = [ + 'encrypt' => true, + 'previews' => true, + 'filesystem_check_changes' => 1 + ]; + $rows = array_map(function (StorageConfig $config) use ($userId, $defaultMountOptions, $full) { + $storageConfig = $config->getBackendOptions(); + $keys = array_keys($storageConfig); + $values = array_values($storageConfig); + + if (!$full) { + $values = array_map(function ($value) { + if (is_string($value) && strlen($value) > 32) { + return substr($value, 0, 6) . '...' . substr($value, -6, 6); + } else { + return $value; + } + }, $values); + } + + $configStrings = array_map(function ($key, $value) { + return $key . ': ' . json_encode($value); + }, $keys, $values); + $configString = implode(', ', $configStrings); + + $mountOptions = $config->getMountOptions(); + // hide defaults + foreach ($mountOptions as $key => $value) { + if ($value === $defaultMountOptions[$key]) { + unset($mountOptions[$key]); + } + } + $keys = array_keys($mountOptions); + $values = array_values($mountOptions); + + $optionsStrings = array_map(function ($key, $value) { + return $key . ': ' . json_encode($value); + }, $keys, $values); + $optionsString = implode(', ', $optionsStrings); + + $values = [ + $config->getId(), + $config->getMountPoint(), + $config->getBackend()->getText(), + $config->getAuthMechanism()->getText(), + $configString, + $optionsString + ]; + + if (!$userId) { + $applicableUsers = implode(', ', $config->getApplicableUsers()); + $applicableGroups = implode(', ', $config->getApplicableGroups()); + if ($applicableUsers === '' && $applicableGroups === '') { + $applicableUsers = 'All'; + } + $values[] = $applicableUsers; + $values[] = $applicableGroups; + } + + return $values; + }, $mounts); + + $table = new Table($output); + $table->setHeaders($headers); + $table->setRows($rows); + $table->render(); + } + } + + protected function getStorageService($userId) { + if (!empty($userId)) { + $user = $this->userManager->get($userId); + if (is_null($user)) { + throw new NoUserException("user $userId not found"); + } + $this->userSession->setUser($user); + return $this->userService; + } else { + return $this->globalService; + } + } +} diff --git a/apps/files_external/lib/Command/Option.php b/apps/files_external/lib/Command/Option.php new file mode 100644 index 00000000000..109ca26c31f --- /dev/null +++ b/apps/files_external/lib/Command/Option.php @@ -0,0 +1,75 @@ + + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Command; + +use OCA\Files_external\Lib\StorageConfig; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Output\OutputInterface; + +class Option extends Config { + protected function configure() { + $this + ->setName('files_external:option') + ->setDescription('Manage mount options for a mount') + ->addArgument( + 'mount_id', + InputArgument::REQUIRED, + 'The id of the mount to edit' + )->addArgument( + 'key', + InputArgument::REQUIRED, + 'key of the mount option to set/get' + )->addArgument( + 'value', + InputArgument::OPTIONAL, + 'value to set the mount option to, when no value is provided the existing value will be printed' + ); + } + + /** + * @param StorageConfig $mount + * @param string $key + * @param OutputInterface $output + */ + protected function getOption(StorageConfig $mount, $key, OutputInterface $output) { + $value = $mount->getMountOption($key); + if (!is_string($value)) { // show bools and objects correctly + $value = json_encode($value); + } + $output->writeln($value); + } + + /** + * @param StorageConfig $mount + * @param string $key + * @param string $value + * @param OutputInterface $output + */ + protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output) { + $decoded = json_decode($value, true); + if (!is_null($decoded)) { + $value = $decoded; + } + $mount->setMountOption($key, $value); + $this->globalService->updateStorage($mount); + } +} diff --git a/apps/files_external/lib/Command/Verify.php b/apps/files_external/lib/Command/Verify.php new file mode 100644 index 00000000000..6ff871a6094 --- /dev/null +++ b/apps/files_external/lib/Command/Verify.php @@ -0,0 +1,142 @@ + + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Command; + +use OC\Core\Command\Base; +use OCA\Files_External\Lib\Auth\AuthMechanism; +use OCA\Files_External\Lib\Backend\Backend; +use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException; +use OCA\Files_external\Lib\StorageConfig; +use OCA\Files_external\NotFoundException; +use OCA\Files_External\Service\GlobalStoragesService; +use OCP\Files\StorageNotAvailableException; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class Verify extends Base { + /** + * @var GlobalStoragesService + */ + protected $globalService; + + function __construct(GlobalStoragesService $globalService) { + parent::__construct(); + $this->globalService = $globalService; + } + + protected function configure() { + $this + ->setName('files_external:verify') + ->setDescription('Verify mount configuration') + ->addArgument( + 'mount_id', + InputArgument::REQUIRED, + 'The id of the mount to check' + )->addOption( + 'config', + 'c', + InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, + 'Additional config option to set before checking in key=value pairs, required for certain auth backends such as login credentails' + ); + parent::configure(); + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $mountId = $input->getArgument('mount_id'); + $configInput = $input->getOption('config'); + + try { + $mount = $this->globalService->getStorage($mountId); + } catch (NotFoundException $e) { + $output->writeln('Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"'); + return 404; + } + + $this->updateStorageStatus($mount, $configInput, $output); + + $this->writeArrayInOutputFormat($input, $output, [ + 'status' => StorageNotAvailableException::getStateCodeName($mount->getStatus()), + 'code' => $mount->getStatus(), + 'message' => $mount->getStatusMessage() + ]); + } + + private function manipulateStorageConfig(StorageConfig $storage) { + /** @var AuthMechanism */ + $authMechanism = $storage->getAuthMechanism(); + $authMechanism->manipulateStorageConfig($storage); + /** @var Backend */ + $backend = $storage->getBackend(); + $backend->manipulateStorageConfig($storage); + } + + private function updateStorageStatus(StorageConfig &$storage, $configInput, OutputInterface $output) { + try { + try { + $this->manipulateStorageConfig($storage); + } catch (InsufficientDataForMeaningfulAnswerException $e) { + if (count($configInput) === 0) { // extra config options might solve the error + throw $e; + } + } + + foreach ($configInput as $configOption) { + if (!strpos($configOption, '=')) { + $output->writeln('Invalid mount configuration option "' . $configOption . '"'); + return; + } + list($key, $value) = explode('=', $configOption, 2); + $storage->setBackendOption($key, $value); + } + + /** @var Backend */ + $backend = $storage->getBackend(); + // update status (can be time-consuming) + $storage->setStatus( + \OC_Mount_Config::getBackendStatus( + $backend->getStorageClass(), + $storage->getBackendOptions(), + false + ) + ); + } catch (InsufficientDataForMeaningfulAnswerException $e) { + $status = $e->getCode() ? $e->getCode() : StorageNotAvailableException::STATUS_INDETERMINATE; + $storage->setStatus( + $status, + $e->getMessage() + ); + } catch (StorageNotAvailableException $e) { + $storage->setStatus( + $e->getCode(), + $e->getMessage() + ); + } catch (\Exception $e) { + // FIXME: convert storage exceptions to StorageNotAvailableException + $storage->setStatus( + StorageNotAvailableException::STATUS_ERROR, + get_class($e) . ': ' . $e->getMessage() + ); + } + } +} diff --git a/apps/files_external/lib/Controller/AjaxController.php b/apps/files_external/lib/Controller/AjaxController.php new file mode 100644 index 00000000000..c3df3fa8522 --- /dev/null +++ b/apps/files_external/lib/Controller/AjaxController.php @@ -0,0 +1,64 @@ + + * @author Robin Appelman + * @author Robin McCorkell + * @author Ross Nicoll + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Controller; + +use OCP\AppFramework\Controller; +use OCP\IRequest; +use OCP\AppFramework\Http\JSONResponse; +use OCA\Files_External\Lib\Auth\PublicKey\RSA; + +class AjaxController extends Controller { + /** @var RSA */ + private $rsaMechanism; + + public function __construct($appName, IRequest $request, RSA $rsaMechanism) { + parent::__construct($appName, $request); + $this->rsaMechanism = $rsaMechanism; + } + + private function generateSshKeys() { + $key = $this->rsaMechanism->createKey(); + // Replace the placeholder label with a more meaningful one + $key['publicKey'] = str_replace('phpseclib-generated-key', gethostname(), $key['publickey']); + + return $key; + } + + /** + * Generates an SSH public/private key pair. + * + * @NoAdminRequired + */ + public function getSshKeys() { + $key = $this->generateSshKeys(); + return new JSONResponse( + array('data' => array( + 'private_key' => $key['privatekey'], + 'public_key' => $key['publickey'] + ), + 'status' => 'success' + )); + } +} diff --git a/apps/files_external/lib/Controller/GlobalStoragesController.php b/apps/files_external/lib/Controller/GlobalStoragesController.php new file mode 100644 index 00000000000..2471ac861de --- /dev/null +++ b/apps/files_external/lib/Controller/GlobalStoragesController.php @@ -0,0 +1,184 @@ + + * @author Robin McCorkell + * @author Vincent Petry + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Controller; + + +use OCP\ILogger; +use \OCP\IRequest; +use \OCP\IL10N; +use \OCP\AppFramework\Http\DataResponse; +use \OCP\AppFramework\Http; +use OCA\Files_External\Service\GlobalStoragesService; +use \OCA\Files_external\NotFoundException; + +/** + * Global storages controller + */ +class GlobalStoragesController extends StoragesController { + /** + * Creates a new global storages controller. + * + * @param string $AppName application name + * @param IRequest $request request object + * @param IL10N $l10n l10n service + * @param GlobalStoragesService $globalStoragesService storage service + * @param ILogger $logger + */ + public function __construct( + $AppName, + IRequest $request, + IL10N $l10n, + GlobalStoragesService $globalStoragesService, + ILogger $logger + ) { + parent::__construct( + $AppName, + $request, + $l10n, + $globalStoragesService, + $logger + ); + } + + /** + * Create an external storage entry. + * + * @param string $mountPoint storage mount point + * @param string $backend backend identifier + * @param string $authMechanism authentication mechanism identifier + * @param array $backendOptions backend-specific options + * @param array $mountOptions mount-specific options + * @param array $applicableUsers users for which to mount the storage + * @param array $applicableGroups groups for which to mount the storage + * @param int $priority priority + * + * @return DataResponse + */ + public function create( + $mountPoint, + $backend, + $authMechanism, + $backendOptions, + $mountOptions, + $applicableUsers, + $applicableGroups, + $priority + ) { + $newStorage = $this->createStorage( + $mountPoint, + $backend, + $authMechanism, + $backendOptions, + $mountOptions, + $applicableUsers, + $applicableGroups, + $priority + ); + if ($newStorage instanceof DataResponse) { + return $newStorage; + } + + $response = $this->validate($newStorage); + if (!empty($response)) { + return $response; + } + + $newStorage = $this->service->addStorage($newStorage); + + $this->updateStorageStatus($newStorage); + + return new DataResponse( + $newStorage, + Http::STATUS_CREATED + ); + } + + /** + * Update an external storage entry. + * + * @param int $id storage id + * @param string $mountPoint storage mount point + * @param string $backend backend identifier + * @param string $authMechanism authentication mechansim identifier + * @param array $backendOptions backend-specific options + * @param array $mountOptions mount-specific options + * @param array $applicableUsers users for which to mount the storage + * @param array $applicableGroups groups for which to mount the storage + * @param int $priority priority + * + * @return DataResponse + */ + public function update( + $id, + $mountPoint, + $backend, + $authMechanism, + $backendOptions, + $mountOptions, + $applicableUsers, + $applicableGroups, + $priority + ) { + $storage = $this->createStorage( + $mountPoint, + $backend, + $authMechanism, + $backendOptions, + $mountOptions, + $applicableUsers, + $applicableGroups, + $priority + ); + if ($storage instanceof DataResponse) { + return $storage; + } + $storage->setId($id); + + $response = $this->validate($storage); + if (!empty($response)) { + return $response; + } + + try { + $storage = $this->service->updateStorage($storage); + } catch (NotFoundException $e) { + return new DataResponse( + [ + 'message' => (string)$this->l10n->t('Storage with id "%i" not found', array($id)) + ], + Http::STATUS_NOT_FOUND + ); + } + + $this->updateStorageStatus($storage); + + return new DataResponse( + $storage, + Http::STATUS_OK + ); + + } + + +} diff --git a/apps/files_external/lib/Controller/StoragesController.php b/apps/files_external/lib/Controller/StoragesController.php new file mode 100644 index 00000000000..da26f24b6b8 --- /dev/null +++ b/apps/files_external/lib/Controller/StoragesController.php @@ -0,0 +1,338 @@ + + * @author Robin Appelman + * @author Robin McCorkell + * @author Vincent Petry + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Controller; + + +use OCP\ILogger; +use \OCP\IRequest; +use \OCP\IL10N; +use \OCP\AppFramework\Http\DataResponse; +use \OCP\AppFramework\Controller; +use \OCP\AppFramework\Http; +use OCA\Files_External\Service\StoragesService; +use \OCA\Files_external\NotFoundException; +use \OCA\Files_external\Lib\StorageConfig; +use \OCA\Files_External\Lib\Backend\Backend; +use \OCA\Files_External\Lib\Auth\AuthMechanism; +use \OCP\Files\StorageNotAvailableException; +use \OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException; + +/** + * Base class for storages controllers + */ +abstract class StoragesController extends Controller { + + /** + * L10N service + * + * @var IL10N + */ + protected $l10n; + + /** + * Storages service + * + * @var StoragesService + */ + protected $service; + + /** + * @var ILogger + */ + protected $logger; + + /** + * Creates a new storages controller. + * + * @param string $AppName application name + * @param IRequest $request request object + * @param IL10N $l10n l10n service + * @param StoragesService $storagesService storage service + * @param ILogger $logger + */ + public function __construct( + $AppName, + IRequest $request, + IL10N $l10n, + StoragesService $storagesService, + ILogger $logger + ) { + parent::__construct($AppName, $request); + $this->l10n = $l10n; + $this->service = $storagesService; + $this->logger = $logger; + } + + /** + * Create a storage from its parameters + * + * @param string $mountPoint storage mount point + * @param string $backend backend identifier + * @param string $authMechanism authentication mechanism identifier + * @param array $backendOptions backend-specific options + * @param array|null $mountOptions mount-specific options + * @param array|null $applicableUsers users for which to mount the storage + * @param array|null $applicableGroups groups for which to mount the storage + * @param int|null $priority priority + * + * @return StorageConfig|DataResponse + */ + protected function createStorage( + $mountPoint, + $backend, + $authMechanism, + $backendOptions, + $mountOptions = null, + $applicableUsers = null, + $applicableGroups = null, + $priority = null + ) { + try { + return $this->service->createStorage( + $mountPoint, + $backend, + $authMechanism, + $backendOptions, + $mountOptions, + $applicableUsers, + $applicableGroups, + $priority + ); + } catch (\InvalidArgumentException $e) { + $this->logger->logException($e); + return new DataResponse( + [ + 'message' => (string)$this->l10n->t('Invalid backend or authentication mechanism class') + ], + Http::STATUS_UNPROCESSABLE_ENTITY + ); + } + } + + /** + * Validate storage config + * + * @param StorageConfig $storage storage config + *1 + * @return DataResponse|null returns response in case of validation error + */ + protected function validate(StorageConfig $storage) { + $mountPoint = $storage->getMountPoint(); + if ($mountPoint === '' || $mountPoint === '/') { + return new DataResponse( + array( + 'message' => (string)$this->l10n->t('Invalid mount point') + ), + Http::STATUS_UNPROCESSABLE_ENTITY + ); + } + + if ($storage->getBackendOption('objectstore')) { + // objectstore must not be sent from client side + return new DataResponse( + array( + 'message' => (string)$this->l10n->t('Objectstore forbidden') + ), + Http::STATUS_UNPROCESSABLE_ENTITY + ); + } + + /** @var Backend */ + $backend = $storage->getBackend(); + /** @var AuthMechanism */ + $authMechanism = $storage->getAuthMechanism(); + if ($backend->checkDependencies()) { + // invalid backend + return new DataResponse( + array( + 'message' => (string)$this->l10n->t('Invalid storage backend "%s"', [ + $backend->getIdentifier() + ]) + ), + Http::STATUS_UNPROCESSABLE_ENTITY + ); + } + + if (!$backend->isVisibleFor($this->service->getVisibilityType())) { + // not permitted to use backend + return new DataResponse( + array( + 'message' => (string)$this->l10n->t('Not permitted to use backend "%s"', [ + $backend->getIdentifier() + ]) + ), + Http::STATUS_UNPROCESSABLE_ENTITY + ); + } + if (!$authMechanism->isVisibleFor($this->service->getVisibilityType())) { + // not permitted to use auth mechanism + return new DataResponse( + array( + 'message' => (string)$this->l10n->t('Not permitted to use authentication mechanism "%s"', [ + $authMechanism->getIdentifier() + ]) + ), + Http::STATUS_UNPROCESSABLE_ENTITY + ); + } + + if (!$backend->validateStorage($storage)) { + // unsatisfied parameters + return new DataResponse( + array( + 'message' => (string)$this->l10n->t('Unsatisfied backend parameters') + ), + Http::STATUS_UNPROCESSABLE_ENTITY + ); + } + if (!$authMechanism->validateStorage($storage)) { + // unsatisfied parameters + return new DataResponse( + [ + 'message' => (string)$this->l10n->t('Unsatisfied authentication mechanism parameters') + ], + Http::STATUS_UNPROCESSABLE_ENTITY + ); + } + + return null; + } + + protected function manipulateStorageConfig(StorageConfig $storage) { + /** @var AuthMechanism */ + $authMechanism = $storage->getAuthMechanism(); + $authMechanism->manipulateStorageConfig($storage); + /** @var Backend */ + $backend = $storage->getBackend(); + $backend->manipulateStorageConfig($storage); + } + + /** + * Check whether the given storage is available / valid. + * + * Note that this operation can be time consuming depending + * on whether the remote storage is available or not. + * + * @param StorageConfig $storage storage configuration + */ + protected function updateStorageStatus(StorageConfig &$storage) { + try { + $this->manipulateStorageConfig($storage); + + /** @var Backend */ + $backend = $storage->getBackend(); + // update status (can be time-consuming) + $storage->setStatus( + \OC_Mount_Config::getBackendStatus( + $backend->getStorageClass(), + $storage->getBackendOptions(), + false + ) + ); + } catch (InsufficientDataForMeaningfulAnswerException $e) { + $status = $e->getCode() ? $e->getCode() : StorageNotAvailableException::STATUS_INDETERMINATE; + $storage->setStatus( + $status, + $this->l10n->t('Insufficient data: %s', [$e->getMessage()]) + ); + } catch (StorageNotAvailableException $e) { + $storage->setStatus( + $e->getCode(), + $this->l10n->t('%s', [$e->getMessage()]) + ); + } catch (\Exception $e) { + // FIXME: convert storage exceptions to StorageNotAvailableException + $storage->setStatus( + StorageNotAvailableException::STATUS_ERROR, + get_class($e).': '.$e->getMessage() + ); + } + } + + /** + * Get all storage entries + * + * @return DataResponse + */ + public function index() { + $storages = $this->service->getStorages(); + + return new DataResponse( + $storages, + Http::STATUS_OK + ); + } + + /** + * Get an external storage entry. + * + * @param int $id storage id + * + * @return DataResponse + */ + public function show($id) { + try { + $storage = $this->service->getStorage($id); + + $this->updateStorageStatus($storage); + } catch (NotFoundException $e) { + return new DataResponse( + [ + 'message' => (string)$this->l10n->t('Storage with id "%i" not found', array($id)) + ], + Http::STATUS_NOT_FOUND + ); + } + + return new DataResponse( + $storage, + Http::STATUS_OK + ); + } + + /** + * Deletes the storage with the given id. + * + * @param int $id storage id + * + * @return DataResponse + */ + public function destroy($id) { + try { + $this->service->removeStorage($id); + } catch (NotFoundException $e) { + return new DataResponse( + [ + 'message' => (string)$this->l10n->t('Storage with id "%i" not found', array($id)) + ], + Http::STATUS_NOT_FOUND + ); + } + + return new DataResponse([], Http::STATUS_NO_CONTENT); + } + +} + diff --git a/apps/files_external/lib/Controller/UserGlobalStoragesController.php b/apps/files_external/lib/Controller/UserGlobalStoragesController.php new file mode 100644 index 00000000000..c420d9ad59f --- /dev/null +++ b/apps/files_external/lib/Controller/UserGlobalStoragesController.php @@ -0,0 +1,201 @@ + + * @author Robin McCorkell + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Controller; + +use OCA\Files_External\Lib\Auth\AuthMechanism; +use OCA\Files_External\Lib\Auth\IUserProvided; +use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException; +use OCP\ILogger; +use \OCP\IRequest; +use \OCP\IL10N; +use \OCP\AppFramework\Http\DataResponse; +use \OCP\AppFramework\Http; +use OCA\Files_External\Service\UserGlobalStoragesService; +use \OCA\Files_external\NotFoundException; +use \OCA\Files_external\Lib\StorageConfig; +use \OCA\Files_External\Lib\Backend\Backend; +use OCP\IUserSession; + +/** + * User global storages controller + */ +class UserGlobalStoragesController extends StoragesController { + /** + * @var IUserSession + */ + private $userSession; + + /** + * Creates a new user global storages controller. + * + * @param string $AppName application name + * @param IRequest $request request object + * @param IL10N $l10n l10n service + * @param UserGlobalStoragesService $userGlobalStoragesService storage service + * @param IUserSession $userSession + */ + public function __construct( + $AppName, + IRequest $request, + IL10N $l10n, + UserGlobalStoragesService $userGlobalStoragesService, + IUserSession $userSession, + ILogger $logger + ) { + parent::__construct( + $AppName, + $request, + $l10n, + $userGlobalStoragesService, + $logger + ); + $this->userSession = $userSession; + } + + /** + * Get all storage entries + * + * @return DataResponse + * + * @NoAdminRequired + */ + public function index() { + $storages = $this->service->getUniqueStorages(); + + // remove configuration data, this must be kept private + foreach ($storages as $storage) { + $this->sanitizeStorage($storage); + } + + return new DataResponse( + $storages, + Http::STATUS_OK + ); + } + + protected function manipulateStorageConfig(StorageConfig $storage) { + /** @var AuthMechanism */ + $authMechanism = $storage->getAuthMechanism(); + $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser()); + /** @var Backend */ + $backend = $storage->getBackend(); + $backend->manipulateStorageConfig($storage, $this->userSession->getUser()); + } + + /** + * Get an external storage entry. + * + * @param int $id storage id + * @return DataResponse + * + * @NoAdminRequired + */ + public function show($id) { + try { + $storage = $this->service->getStorage($id); + + $this->updateStorageStatus($storage); + } catch (NotFoundException $e) { + return new DataResponse( + [ + 'message' => (string)$this->l10n->t('Storage with id "%i" not found', array($id)) + ], + Http::STATUS_NOT_FOUND + ); + } + + $this->sanitizeStorage($storage); + + return new DataResponse( + $storage, + Http::STATUS_OK + ); + } + + /** + * Update an external storage entry. + * Only allows setting user provided backend fields + * + * @param int $id storage id + * @param array $backendOptions backend-specific options + * + * @return DataResponse + * + * @NoAdminRequired + */ + public function update( + $id, + $backendOptions + ) { + try { + $storage = $this->service->getStorage($id); + $authMechanism = $storage->getAuthMechanism(); + if ($authMechanism instanceof IUserProvided) { + $authMechanism->saveBackendOptions($this->userSession->getUser(), $id, $backendOptions); + $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser()); + } else { + return new DataResponse( + [ + 'message' => (string)$this->l10n->t('Storage with id "%i" is not user editable', array($id)) + ], + Http::STATUS_FORBIDDEN + ); + } + } catch (NotFoundException $e) { + return new DataResponse( + [ + 'message' => (string)$this->l10n->t('Storage with id "%i" not found', array($id)) + ], + Http::STATUS_NOT_FOUND + ); + } + + $this->updateStorageStatus($storage); + $this->sanitizeStorage($storage); + + return new DataResponse( + $storage, + Http::STATUS_OK + ); + + } + + /** + * Remove sensitive data from a StorageConfig before returning it to the user + * + * @param StorageConfig $storage + */ + protected function sanitizeStorage(StorageConfig $storage) { + $storage->setBackendOptions([]); + $storage->setMountOptions([]); + + if ($storage->getAuthMechanism() instanceof IUserProvided) { + try { + $storage->getAuthMechanism()->manipulateStorageConfig($storage, $this->userSession->getUser()); + } catch (InsufficientDataForMeaningfulAnswerException $e) { + // not configured yet + } + } + } + +} diff --git a/apps/files_external/lib/Controller/UserStoragesController.php b/apps/files_external/lib/Controller/UserStoragesController.php new file mode 100644 index 00000000000..e20a41e0cc4 --- /dev/null +++ b/apps/files_external/lib/Controller/UserStoragesController.php @@ -0,0 +1,222 @@ + + * @author Robin McCorkell + * @author Vincent Petry + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Controller; + + +use OCA\Files_External\Lib\Auth\AuthMechanism; +use OCP\ILogger; +use \OCP\IUserSession; +use \OCP\IRequest; +use \OCP\IL10N; +use \OCP\AppFramework\Http\DataResponse; +use \OCP\AppFramework\Http; +use OCA\Files_External\Service\UserStoragesService; +use \OCA\Files_external\NotFoundException; +use \OCA\Files_external\Lib\StorageConfig; +use \OCA\Files_External\Lib\Backend\Backend; + +/** + * User storages controller + */ +class UserStoragesController extends StoragesController { + /** + * @var IUserSession + */ + private $userSession; + + /** + * Creates a new user storages controller. + * + * @param string $AppName application name + * @param IRequest $request request object + * @param IL10N $l10n l10n service + * @param UserStoragesService $userStoragesService storage service + * @param IUserSession $userSession + * @param ILogger $logger + */ + public function __construct( + $AppName, + IRequest $request, + IL10N $l10n, + UserStoragesService $userStoragesService, + IUserSession $userSession, + ILogger $logger + ) { + parent::__construct( + $AppName, + $request, + $l10n, + $userStoragesService, + $logger + ); + $this->userSession = $userSession; + } + + protected function manipulateStorageConfig(StorageConfig $storage) { + /** @var AuthMechanism */ + $authMechanism = $storage->getAuthMechanism(); + $authMechanism->manipulateStorageConfig($storage, $this->userSession->getUser()); + /** @var Backend */ + $backend = $storage->getBackend(); + $backend->manipulateStorageConfig($storage, $this->userSession->getUser()); + } + + /** + * Get all storage entries + * + * @NoAdminRequired + * + * @return DataResponse + */ + public function index() { + return parent::index(); + } + + /** + * Return storage + * + * @NoAdminRequired + * + * {@inheritdoc} + */ + public function show($id) { + return parent::show($id); + } + + /** + * Create an external storage entry. + * + * @param string $mountPoint storage mount point + * @param string $backend backend identifier + * @param string $authMechanism authentication mechanism identifier + * @param array $backendOptions backend-specific options + * @param array $mountOptions backend-specific mount options + * + * @return DataResponse + * + * @NoAdminRequired + */ + public function create( + $mountPoint, + $backend, + $authMechanism, + $backendOptions, + $mountOptions + ) { + $newStorage = $this->createStorage( + $mountPoint, + $backend, + $authMechanism, + $backendOptions, + $mountOptions + ); + if ($newStorage instanceOf DataResponse) { + return $newStorage; + } + + $response = $this->validate($newStorage); + if (!empty($response)) { + return $response; + } + + $newStorage = $this->service->addStorage($newStorage); + $this->updateStorageStatus($newStorage); + + return new DataResponse( + $newStorage, + Http::STATUS_CREATED + ); + } + + /** + * Update an external storage entry. + * + * @param int $id storage id + * @param string $mountPoint storage mount point + * @param string $backend backend identifier + * @param string $authMechanism authentication mechanism identifier + * @param array $backendOptions backend-specific options + * @param array $mountOptions backend-specific mount options + * + * @return DataResponse + * + * @NoAdminRequired + */ + public function update( + $id, + $mountPoint, + $backend, + $authMechanism, + $backendOptions, + $mountOptions + ) { + $storage = $this->createStorage( + $mountPoint, + $backend, + $authMechanism, + $backendOptions, + $mountOptions + ); + if ($storage instanceOf DataResponse) { + return $storage; + } + $storage->setId($id); + + $response = $this->validate($storage); + if (!empty($response)) { + return $response; + } + + try { + $storage = $this->service->updateStorage($storage); + } catch (NotFoundException $e) { + return new DataResponse( + [ + 'message' => (string)$this->l10n->t('Storage with id "%i" not found', array($id)) + ], + Http::STATUS_NOT_FOUND + ); + } + + $this->updateStorageStatus($storage); + + return new DataResponse( + $storage, + Http::STATUS_OK + ); + + } + + /** + * Delete storage + * + * @NoAdminRequired + * + * {@inheritdoc} + */ + public function destroy($id) { + return parent::destroy($id); + } + +} diff --git a/apps/files_external/lib/Migration/DummyUserSession.php b/apps/files_external/lib/Migration/DummyUserSession.php new file mode 100644 index 00000000000..b2157aafebb --- /dev/null +++ b/apps/files_external/lib/Migration/DummyUserSession.php @@ -0,0 +1,51 @@ + + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Migration; + +use OCP\IUser; +use OCP\IUserSession; + +class DummyUserSession implements IUserSession { + + /** + * @var IUser + */ + private $user; + + public function login($user, $password) { + } + + public function logout() { + } + + public function setUser($user) { + $this->user = $user; + } + + public function getUser() { + return $this->user; + } + + public function isLoggedIn() { + return !is_null($this->user); + } +} diff --git a/apps/files_external/lib/Migration/StorageMigrator.php b/apps/files_external/lib/Migration/StorageMigrator.php new file mode 100644 index 00000000000..a8b0461e7de --- /dev/null +++ b/apps/files_external/lib/Migration/StorageMigrator.php @@ -0,0 +1,143 @@ + + * @author Vincent Petry + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Migration; + +use OCA\Files_External\Service\BackendService; +use OCA\Files_External\Service\DBConfigService; +use OCA\Files_External\Service\GlobalStoragesService; +use OCA\Files_External\Service\LegacyStoragesService; +use OCA\Files_External\Service\StoragesService; +use OCP\Files\Config\IUserMountCache; +use OCP\IConfig; +use OCP\IDBConnection; +use OCP\ILogger; +use OCP\IUser; + +/** + * Migrate mount config from mount.json to the database + */ +class StorageMigrator { + /** + * @var BackendService + */ + private $backendService; + + /** + * @var DBConfigService + */ + private $dbConfig; + + /** + * @var IConfig + */ + private $config; + + /** + * @var IDBConnection + */ + private $connection; + + /** + * @var ILogger + */ + private $logger; + + /** @var IUserMountCache */ + private $userMountCache; + + /** + * StorageMigrator constructor. + * + * @param BackendService $backendService + * @param DBConfigService $dbConfig + * @param IConfig $config + * @param IDBConnection $connection + * @param ILogger $logger + * @param IUserMountCache $userMountCache + */ + public function __construct( + BackendService $backendService, + DBConfigService $dbConfig, + IConfig $config, + IDBConnection $connection, + ILogger $logger, + IUserMountCache $userMountCache + ) { + $this->backendService = $backendService; + $this->dbConfig = $dbConfig; + $this->config = $config; + $this->connection = $connection; + $this->logger = $logger; + $this->userMountCache = $userMountCache; + } + + private function migrate(LegacyStoragesService $legacyService, StoragesService $storageService) { + $existingStorage = $legacyService->getAllStorages(); + + $this->connection->beginTransaction(); + try { + foreach ($existingStorage as $storage) { + $mountOptions = $storage->getMountOptions(); + if (!empty($mountOptions) && !isset($mountOptions['enable_sharing'])) { + // existing mounts must have sharing enabled by default to avoid surprises + $mountOptions['enable_sharing'] = true; + $storage->setMountOptions($mountOptions); + } + $storageService->addStorage($storage); + } + $this->connection->commit(); + } catch (\Exception $e) { + $this->logger->logException($e); + $this->connection->rollBack(); + } + } + + /** + * Migrate admin configured storages + */ + public function migrateGlobal() { + $legacyService = new GlobalLegacyStoragesService($this->backendService); + $storageService = new GlobalStoragesService($this->backendService, $this->dbConfig, $this->userMountCache); + + $this->migrate($legacyService, $storageService); + } + + /** + * Migrate personal storages configured by the current user + * + * @param IUser $user + */ + public function migrateUser(IUser $user) { + $dummySession = new DummyUserSession(); + $dummySession->setUser($user); + $userId = $user->getUID(); + $userVersion = $this->config->getUserValue($userId, 'files_external', 'config_version', '0.0.0'); + if (version_compare($userVersion, '0.5.0', '<')) { + $this->config->setUserValue($userId, 'files_external', 'config_version', '0.5.0'); + $legacyService = new UserLegacyStoragesService($this->backendService, $dummySession); + $storageService = new UserStoragesService($this->backendService, $this->dbConfig, $dummySession, $this->userMountCache); + + $this->migrate($legacyService, $storageService); + } + } +} diff --git a/apps/files_external/lib/Service/BackendService.php b/apps/files_external/lib/Service/BackendService.php new file mode 100644 index 00000000000..c3dc4da0177 --- /dev/null +++ b/apps/files_external/lib/Service/BackendService.php @@ -0,0 +1,282 @@ + + * @author Robin McCorkell + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Service; + +use \OCP\IConfig; + +use \OCA\Files_External\Lib\Backend\Backend; +use \OCA\Files_External\Lib\Auth\AuthMechanism; +use \OCA\Files_External\Lib\Config\IBackendProvider; +use \OCA\Files_External\Lib\Config\IAuthMechanismProvider; + +/** + * Service class to manage backend definitions + */ +class BackendService { + + /** Visibility constants for VisibilityTrait */ + const VISIBILITY_NONE = 0; + const VISIBILITY_PERSONAL = 1; + const VISIBILITY_ADMIN = 2; + //const VISIBILITY_ALIENS = 4; + + const VISIBILITY_DEFAULT = 3; // PERSONAL | ADMIN + + /** Priority constants for PriorityTrait */ + const PRIORITY_DEFAULT = 100; + + /** @var IConfig */ + protected $config; + + /** @var bool */ + private $userMountingAllowed = true; + + /** @var string[] */ + private $userMountingBackends = []; + + /** @var Backend[] */ + private $backends = []; + + /** @var IBackendProvider[] */ + private $backendProviders = []; + + /** @var AuthMechanism[] */ + private $authMechanisms = []; + + /** @var IAuthMechanismProvider[] */ + private $authMechanismProviders = []; + + /** + * @param IConfig $config + */ + public function __construct( + IConfig $config + ) { + $this->config = $config; + + // Load config values + if ($this->config->getAppValue('files_external', 'allow_user_mounting', 'yes') !== 'yes') { + $this->userMountingAllowed = false; + } + $this->userMountingBackends = explode(',', + $this->config->getAppValue('files_external', 'user_mounting_backends', '') + ); + + // if no backend is in the list an empty string is in the array and user mounting is disabled + if ($this->userMountingBackends === ['']) { + $this->userMountingAllowed = false; + } + } + + /** + * Register a backend provider + * + * @since 9.1.0 + * @param IBackendProvider $provider + */ + public function registerBackendProvider(IBackendProvider $provider) { + $this->backendProviders[] = $provider; + } + + private function loadBackendProviders() { + foreach ($this->backendProviders as $provider) { + $this->registerBackends($provider->getBackends()); + } + $this->backendProviders = []; + } + + /** + * Register an auth mechanism provider + * + * @since 9.1.0 + * @param IAuthMechanismProvider $provider + */ + public function registerAuthMechanismProvider(IAuthMechanismProvider $provider) { + $this->authMechanismProviders[] = $provider; + } + + private function loadAuthMechanismProviders() { + foreach ($this->authMechanismProviders as $provider) { + $this->registerAuthMechanisms($provider->getAuthMechanisms()); + } + $this->authMechanismProviders = []; + } + + /** + * Register a backend + * + * @deprecated 9.1.0 use registerBackendProvider() + * @param Backend $backend + */ + public function registerBackend(Backend $backend) { + if (!$this->isAllowedUserBackend($backend)) { + $backend->removeVisibility(BackendService::VISIBILITY_PERSONAL); + } + foreach ($backend->getIdentifierAliases() as $alias) { + $this->backends[$alias] = $backend; + } + } + + /** + * @deprecated 9.1.0 use registerBackendProvider() + * @param Backend[] $backends + */ + public function registerBackends(array $backends) { + foreach ($backends as $backend) { + $this->registerBackend($backend); + } + } + /** + * Register an authentication mechanism + * + * @deprecated 9.1.0 use registerAuthMechanismProvider() + * @param AuthMechanism $authMech + */ + public function registerAuthMechanism(AuthMechanism $authMech) { + if (!$this->isAllowedAuthMechanism($authMech)) { + $authMech->removeVisibility(BackendService::VISIBILITY_PERSONAL); + } + foreach ($authMech->getIdentifierAliases() as $alias) { + $this->authMechanisms[$alias] = $authMech; + } + } + + /** + * @deprecated 9.1.0 use registerAuthMechanismProvider() + * @param AuthMechanism[] $mechanisms + */ + public function registerAuthMechanisms(array $mechanisms) { + foreach ($mechanisms as $mechanism) { + $this->registerAuthMechanism($mechanism); + } + } + + /** + * Get all backends + * + * @return Backend[] + */ + public function getBackends() { + $this->loadBackendProviders(); + // only return real identifiers, no aliases + $backends = []; + foreach ($this->backends as $backend) { + $backends[$backend->getIdentifier()] = $backend; + } + return $backends; + } + + /** + * Get all available backends + * + * @return Backend[] + */ + public function getAvailableBackends() { + return array_filter($this->getBackends(), function($backend) { + return !($backend->checkDependencies()); + }); + } + + /** + * @param string $identifier + * @return Backend|null + */ + public function getBackend($identifier) { + $this->loadBackendProviders(); + if (isset($this->backends[$identifier])) { + return $this->backends[$identifier]; + } + return null; + } + + /** + * Get all authentication mechanisms + * + * @return AuthMechanism[] + */ + public function getAuthMechanisms() { + $this->loadAuthMechanismProviders(); + // only return real identifiers, no aliases + $mechanisms = []; + foreach ($this->authMechanisms as $mechanism) { + $mechanisms[$mechanism->getIdentifier()] = $mechanism; + } + return $mechanisms; + } + + /** + * Get all authentication mechanisms for schemes + * + * @param string[] $schemes + * @return AuthMechanism[] + */ + public function getAuthMechanismsByScheme(array $schemes) { + return array_filter($this->getAuthMechanisms(), function($authMech) use ($schemes) { + return in_array($authMech->getScheme(), $schemes, true); + }); + } + + /** + * @param string $identifier + * @return AuthMechanism|null + */ + public function getAuthMechanism($identifier) { + $this->loadAuthMechanismProviders(); + if (isset($this->authMechanisms[$identifier])) { + return $this->authMechanisms[$identifier]; + } + return null; + } + + /** + * @return bool + */ + public function isUserMountingAllowed() { + return $this->userMountingAllowed; + } + + /** + * Check a backend if a user is allowed to mount it + * + * @param Backend $backend + * @return bool + */ + protected function isAllowedUserBackend(Backend $backend) { + if ($this->userMountingAllowed && + array_intersect($backend->getIdentifierAliases(), $this->userMountingBackends) + ) { + return true; + } + return false; + } + + /** + * Check an authentication mechanism if a user is allowed to use it + * + * @param AuthMechanism $authMechanism + * @return bool + */ + protected function isAllowedAuthMechanism(AuthMechanism $authMechanism) { + return true; // not implemented + } +} diff --git a/apps/files_external/lib/Service/DBConfigService.php b/apps/files_external/lib/Service/DBConfigService.php new file mode 100644 index 00000000000..9f7061eb938 --- /dev/null +++ b/apps/files_external/lib/Service/DBConfigService.php @@ -0,0 +1,451 @@ + + * @author Robin Appelman + * @author Robin McCorkell + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Service; + +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\IDBConnection; +use OCP\Security\ICrypto; + +/** + * Stores the mount config in the database + */ +class DBConfigService { + const MOUNT_TYPE_ADMIN = 1; + const MOUNT_TYPE_PERSONAl = 2; + + const APPLICABLE_TYPE_GLOBAL = 1; + const APPLICABLE_TYPE_GROUP = 2; + const APPLICABLE_TYPE_USER = 3; + + /** + * @var IDBConnection + */ + private $connection; + + /** + * @var ICrypto + */ + private $crypto; + + /** + * DBConfigService constructor. + * + * @param IDBConnection $connection + * @param ICrypto $crypto + */ + public function __construct(IDBConnection $connection, ICrypto $crypto) { + $this->connection = $connection; + $this->crypto = $crypto; + } + + /** + * @param int $mountId + * @return array + */ + public function getMountById($mountId) { + $builder = $this->connection->getQueryBuilder(); + $query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type']) + ->from('external_mounts', 'm') + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); + $mounts = $this->getMountsFromQuery($query); + if (count($mounts) > 0) { + return $mounts[0]; + } else { + return null; + } + } + + /** + * Get admin defined mounts + * + * @return array + */ + public function getAdminMounts() { + $builder = $this->connection->getQueryBuilder(); + $query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type']) + ->from('external_mounts') + ->where($builder->expr()->eq('type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT))); + return $this->getMountsFromQuery($query); + } + + protected function getForQuery(IQueryBuilder $builder, $type, $value) { + $query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type']) + ->from('external_mounts', 'm') + ->innerJoin('m', 'external_applicable', 'a', $builder->expr()->eq('m.mount_id', 'a.mount_id')) + ->where($builder->expr()->eq('a.type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT))); + + if (is_null($value)) { + $query = $query->andWhere($builder->expr()->isNull('a.value')); + } else { + $query = $query->andWhere($builder->expr()->eq('a.value', $builder->createNamedParameter($value))); + } + + return $query; + } + + /** + * Get mounts by applicable + * + * @param int $type any of the self::APPLICABLE_TYPE_ constants + * @param string|null $value user_id, group_id or null for global mounts + * @return array + */ + public function getMountsFor($type, $value) { + $builder = $this->connection->getQueryBuilder(); + $query = $this->getForQuery($builder, $type, $value); + + return $this->getMountsFromQuery($query); + } + + /** + * Get admin defined mounts by applicable + * + * @param int $type any of the self::APPLICABLE_TYPE_ constants + * @param string|null $value user_id, group_id or null for global mounts + * @return array + */ + public function getAdminMountsFor($type, $value) { + $builder = $this->connection->getQueryBuilder(); + $query = $this->getForQuery($builder, $type, $value); + $query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT))); + + return $this->getMountsFromQuery($query); + } + + /** + * Get admin defined mounts for multiple applicable + * + * @param int $type any of the self::APPLICABLE_TYPE_ constants + * @param string[] $values user_ids or group_ids + * @return array + */ + public function getAdminMountsForMultiple($type, array $values) { + $builder = $this->connection->getQueryBuilder(); + $params = array_map(function ($value) use ($builder) { + return $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR); + }, $values); + + $query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type']) + ->from('external_mounts', 'm') + ->innerJoin('m', 'external_applicable', 'a', $builder->expr()->eq('m.mount_id', 'a.mount_id')) + ->where($builder->expr()->eq('a.type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT))) + ->andWhere($builder->expr()->in('a.value', $params)); + $query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT))); + + return $this->getMountsFromQuery($query); + } + + /** + * Get user defined mounts by applicable + * + * @param int $type any of the self::APPLICABLE_TYPE_ constants + * @param string|null $value user_id, group_id or null for global mounts + * @return array + */ + public function getUserMountsFor($type, $value) { + $builder = $this->connection->getQueryBuilder(); + $query = $this->getForQuery($builder, $type, $value); + $query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_PERSONAl, IQueryBuilder::PARAM_INT))); + + return $this->getMountsFromQuery($query); + } + + /** + * Add a mount to the database + * + * @param string $mountPoint + * @param string $storageBackend + * @param string $authBackend + * @param int $priority + * @param int $type self::MOUNT_TYPE_ADMIN or self::MOUNT_TYPE_PERSONAL + * @return int the id of the new mount + */ + public function addMount($mountPoint, $storageBackend, $authBackend, $priority, $type) { + if (!$priority) { + $priority = 100; + } + $builder = $this->connection->getQueryBuilder(); + $query = $builder->insert('external_mounts') + ->values([ + 'mount_point' => $builder->createNamedParameter($mountPoint, IQueryBuilder::PARAM_STR), + 'storage_backend' => $builder->createNamedParameter($storageBackend, IQueryBuilder::PARAM_STR), + 'auth_backend' => $builder->createNamedParameter($authBackend, IQueryBuilder::PARAM_STR), + 'priority' => $builder->createNamedParameter($priority, IQueryBuilder::PARAM_INT), + 'type' => $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT) + ]); + $query->execute(); + return (int)$this->connection->lastInsertId('external_mounts'); + } + + /** + * Remove a mount from the database + * + * @param int $mountId + */ + public function removeMount($mountId) { + $builder = $this->connection->getQueryBuilder(); + $query = $builder->delete('external_mounts') + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); + $query->execute(); + + $query = $builder->delete('external_applicable') + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); + $query->execute(); + + $query = $builder->delete('external_config') + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); + $query->execute(); + + $query = $builder->delete('external_options') + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); + $query->execute(); + } + + /** + * @param int $mountId + * @param string $newMountPoint + */ + public function setMountPoint($mountId, $newMountPoint) { + $builder = $this->connection->getQueryBuilder(); + + $query = $builder->update('external_mounts') + ->set('mount_point', $builder->createNamedParameter($newMountPoint)) + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); + + $query->execute(); + } + + /** + * @param int $mountId + * @param string $newAuthBackend + */ + public function setAuthBackend($mountId, $newAuthBackend) { + $builder = $this->connection->getQueryBuilder(); + + $query = $builder->update('external_mounts') + ->set('auth_backend', $builder->createNamedParameter($newAuthBackend)) + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); + + $query->execute(); + } + + /** + * @param int $mountId + * @param string $key + * @param string $value + */ + public function setConfig($mountId, $key, $value) { + if ($key === 'password') { + $value = $this->encryptValue($value); + } + $count = $this->connection->insertIfNotExist('*PREFIX*external_config', [ + 'mount_id' => $mountId, + 'key' => $key, + 'value' => $value + ], ['mount_id', 'key']); + if ($count === 0) { + $builder = $this->connection->getQueryBuilder(); + $query = $builder->update('external_config') + ->set('value', $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR)) + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))) + ->andWhere($builder->expr()->eq('key', $builder->createNamedParameter($key, IQueryBuilder::PARAM_STR))); + $query->execute(); + } + } + + /** + * @param int $mountId + * @param string $key + * @param string $value + */ + public function setOption($mountId, $key, $value) { + + $count = $this->connection->insertIfNotExist('*PREFIX*external_options', [ + 'mount_id' => $mountId, + 'key' => $key, + 'value' => json_encode($value) + ], ['mount_id', 'key']); + if ($count === 0) { + $builder = $this->connection->getQueryBuilder(); + $query = $builder->update('external_options') + ->set('value', $builder->createNamedParameter(json_encode($value), IQueryBuilder::PARAM_STR)) + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))) + ->andWhere($builder->expr()->eq('key', $builder->createNamedParameter($key, IQueryBuilder::PARAM_STR))); + $query->execute(); + } + } + + public function addApplicable($mountId, $type, $value) { + $this->connection->insertIfNotExist('*PREFIX*external_applicable', [ + 'mount_id' => $mountId, + 'type' => $type, + 'value' => $value + ], ['mount_id', 'type', 'value']); + } + + public function removeApplicable($mountId, $type, $value) { + $builder = $this->connection->getQueryBuilder(); + $query = $builder->delete('external_applicable') + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))) + ->andWhere($builder->expr()->eq('type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT))); + + if (is_null($value)) { + $query = $query->andWhere($builder->expr()->isNull('value')); + } else { + $query = $query->andWhere($builder->expr()->eq('value', $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR))); + } + + $query->execute(); + } + + private function getMountsFromQuery(IQueryBuilder $query) { + $result = $query->execute(); + $mounts = $result->fetchAll(); + $uniqueMounts = []; + foreach ($mounts as $mount) { + $id = $mount['mount_id']; + if (!isset($uniqueMounts[$id])) { + $uniqueMounts[$id] = $mount; + } + } + $uniqueMounts = array_values($uniqueMounts); + + $mountIds = array_map(function ($mount) { + return $mount['mount_id']; + }, $uniqueMounts); + $mountIds = array_values(array_unique($mountIds)); + + $applicable = $this->getApplicableForMounts($mountIds); + $config = $this->getConfigForMounts($mountIds); + $options = $this->getOptionsForMounts($mountIds); + + return array_map(function ($mount, $applicable, $config, $options) { + $mount['type'] = (int)$mount['type']; + $mount['priority'] = (int)$mount['priority']; + $mount['applicable'] = $applicable; + $mount['config'] = $config; + $mount['options'] = $options; + return $mount; + }, $uniqueMounts, $applicable, $config, $options); + } + + /** + * Get mount options from a table grouped by mount id + * + * @param string $table + * @param string[] $fields + * @param int[] $mountIds + * @return array [$mountId => [['field1' => $value1, ...], ...], ...] + */ + private function selectForMounts($table, array $fields, array $mountIds) { + if (count($mountIds) === 0) { + return []; + } + $builder = $this->connection->getQueryBuilder(); + $fields[] = 'mount_id'; + $placeHolders = array_map(function ($id) use ($builder) { + return $builder->createPositionalParameter($id, IQueryBuilder::PARAM_INT); + }, $mountIds); + $query = $builder->select($fields) + ->from($table) + ->where($builder->expr()->in('mount_id', $placeHolders)); + $rows = $query->execute()->fetchAll(); + + $result = []; + foreach ($mountIds as $mountId) { + $result[$mountId] = []; + } + foreach ($rows as $row) { + if (isset($row['type'])) { + $row['type'] = (int)$row['type']; + } + $result[$row['mount_id']][] = $row; + } + return $result; + } + + /** + * @param int[] $mountIds + * @return array [$id => [['type' => $type, 'value' => $value], ...], ...] + */ + public function getApplicableForMounts($mountIds) { + return $this->selectForMounts('external_applicable', ['type', 'value'], $mountIds); + } + + /** + * @param int[] $mountIds + * @return array [$id => ['key1' => $value1, ...], ...] + */ + public function getConfigForMounts($mountIds) { + $mountConfigs = $this->selectForMounts('external_config', ['key', 'value'], $mountIds); + return array_map([$this, 'createKeyValueMap'], $mountConfigs); + } + + /** + * @param int[] $mountIds + * @return array [$id => ['key1' => $value1, ...], ...] + */ + public function getOptionsForMounts($mountIds) { + $mountOptions = $this->selectForMounts('external_options', ['key', 'value'], $mountIds); + $optionsMap = array_map([$this, 'createKeyValueMap'], $mountOptions); + return array_map(function (array $options) { + return array_map(function ($option) { + return json_decode($option); + }, $options); + }, $optionsMap); + } + + /** + * @param array $keyValuePairs [['key'=>$key, 'value=>$value], ...] + * @return array ['key1' => $value1, ...] + */ + private function createKeyValueMap(array $keyValuePairs) { + $decryptedPairts = array_map(function ($pair) { + if ($pair['key'] === 'password') { + $pair['value'] = $this->decryptValue($pair['value']); + } + return $pair; + }, $keyValuePairs); + $keys = array_map(function ($pair) { + return $pair['key']; + }, $decryptedPairts); + $values = array_map(function ($pair) { + return $pair['value']; + }, $decryptedPairts); + + return array_combine($keys, $values); + } + + private function encryptValue($value) { + return $this->crypto->encrypt($value); + } + + private function decryptValue($value) { + try { + return $this->crypto->decrypt($value); + } catch (\Exception $e) { + return $value; + } + } +} diff --git a/apps/files_external/lib/Service/GlobalLegacyStoragesService.php b/apps/files_external/lib/Service/GlobalLegacyStoragesService.php new file mode 100644 index 00000000000..4c44881051a --- /dev/null +++ b/apps/files_external/lib/Service/GlobalLegacyStoragesService.php @@ -0,0 +1,44 @@ + + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Service; + +/** + * Read admin defined mounts from the legacy mount.json + */ +class GlobalLegacyStoragesService extends LegacyStoragesService { + /** + * @param BackendService $backendService + */ + public function __construct(BackendService $backendService) { + $this->backendService = $backendService; + } + + /** + * Read legacy config data + * + * @return array list of mount configs + */ + protected function readLegacyConfig() { + // read global config + return \OC_Mount_Config::readData(); + } +} diff --git a/apps/files_external/lib/Service/GlobalStoragesService.php b/apps/files_external/lib/Service/GlobalStoragesService.php new file mode 100644 index 00000000000..6032a827735 --- /dev/null +++ b/apps/files_external/lib/Service/GlobalStoragesService.php @@ -0,0 +1,165 @@ + + * @author Robin Appelman + * @author Robin McCorkell + * @author Vincent Petry + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Service; + +use \OCP\IUserSession; +use \OC\Files\Filesystem; + +use \OCA\Files_external\Lib\StorageConfig; +use \OCA\Files_external\NotFoundException; + +/** + * Service class to manage global external storages + */ +class GlobalStoragesService extends StoragesService { + /** + * Triggers $signal for all applicable users of the given + * storage + * + * @param StorageConfig $storage storage data + * @param string $signal signal to trigger + */ + protected function triggerHooks(StorageConfig $storage, $signal) { + // FIXME: Use as expression in empty once PHP 5.4 support is dropped + $applicableUsers = $storage->getApplicableUsers(); + $applicableGroups = $storage->getApplicableGroups(); + if (empty($applicableUsers) && empty($applicableGroups)) { + // raise for user "all" + $this->triggerApplicableHooks( + $signal, + $storage->getMountPoint(), + \OC_Mount_Config::MOUNT_TYPE_USER, + ['all'] + ); + return; + } + + $this->triggerApplicableHooks( + $signal, + $storage->getMountPoint(), + \OC_Mount_Config::MOUNT_TYPE_USER, + $applicableUsers + ); + $this->triggerApplicableHooks( + $signal, + $storage->getMountPoint(), + \OC_Mount_Config::MOUNT_TYPE_GROUP, + $applicableGroups + ); + } + + /** + * Triggers signal_create_mount or signal_delete_mount to + * accommodate for additions/deletions in applicableUsers + * and applicableGroups fields. + * + * @param StorageConfig $oldStorage old storage config + * @param StorageConfig $newStorage new storage config + */ + protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage) { + // if mount point changed, it's like a deletion + creation + if ($oldStorage->getMountPoint() !== $newStorage->getMountPoint()) { + $this->triggerHooks($oldStorage, Filesystem::signal_delete_mount); + $this->triggerHooks($newStorage, Filesystem::signal_create_mount); + return; + } + + $userAdditions = array_diff($newStorage->getApplicableUsers(), $oldStorage->getApplicableUsers()); + $userDeletions = array_diff($oldStorage->getApplicableUsers(), $newStorage->getApplicableUsers()); + $groupAdditions = array_diff($newStorage->getApplicableGroups(), $oldStorage->getApplicableGroups()); + $groupDeletions = array_diff($oldStorage->getApplicableGroups(), $newStorage->getApplicableGroups()); + + // FIXME: Use as expression in empty once PHP 5.4 support is dropped + // if no applicable were set, raise a signal for "all" + $oldApplicableUsers = $oldStorage->getApplicableUsers(); + $oldApplicableGroups = $oldStorage->getApplicableGroups(); + if (empty($oldApplicableUsers) && empty($oldApplicableGroups)) { + $this->triggerApplicableHooks( + Filesystem::signal_delete_mount, + $oldStorage->getMountPoint(), + \OC_Mount_Config::MOUNT_TYPE_USER, + ['all'] + ); + } + + // trigger delete for removed users + $this->triggerApplicableHooks( + Filesystem::signal_delete_mount, + $oldStorage->getMountPoint(), + \OC_Mount_Config::MOUNT_TYPE_USER, + $userDeletions + ); + + // trigger delete for removed groups + $this->triggerApplicableHooks( + Filesystem::signal_delete_mount, + $oldStorage->getMountPoint(), + \OC_Mount_Config::MOUNT_TYPE_GROUP, + $groupDeletions + ); + + // and now add the new users + $this->triggerApplicableHooks( + Filesystem::signal_create_mount, + $newStorage->getMountPoint(), + \OC_Mount_Config::MOUNT_TYPE_USER, + $userAdditions + ); + + // and now add the new groups + $this->triggerApplicableHooks( + Filesystem::signal_create_mount, + $newStorage->getMountPoint(), + \OC_Mount_Config::MOUNT_TYPE_GROUP, + $groupAdditions + ); + + // FIXME: Use as expression in empty once PHP 5.4 support is dropped + // if no applicable, raise a signal for "all" + $newApplicableUsers = $newStorage->getApplicableUsers(); + $newApplicableGroups = $newStorage->getApplicableGroups(); + if (empty($newApplicableUsers) && empty($newApplicableGroups)) { + $this->triggerApplicableHooks( + Filesystem::signal_create_mount, + $newStorage->getMountPoint(), + \OC_Mount_Config::MOUNT_TYPE_USER, + ['all'] + ); + } + } + + /** + * Get the visibility type for this controller, used in validation + * + * @return string BackendService::VISIBILITY_* constants + */ + public function getVisibilityType() { + return BackendService::VISIBILITY_ADMIN; + } + + protected function isApplicable(StorageConfig $config) { + return true; + } +} diff --git a/apps/files_external/lib/Service/ImportLegacyStoragesService.php b/apps/files_external/lib/Service/ImportLegacyStoragesService.php new file mode 100644 index 00000000000..cb020fb495c --- /dev/null +++ b/apps/files_external/lib/Service/ImportLegacyStoragesService.php @@ -0,0 +1,46 @@ + + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Service; + +class ImportLegacyStoragesService extends LegacyStoragesService { + private $data; + + /** + * @param BackendService $backendService + */ + public function __construct(BackendService $backendService) { + $this->backendService = $backendService; + } + + public function setData($data) { + $this->data = $data; + } + + /** + * Read legacy config data + * + * @return array list of mount configs + */ + protected function readLegacyConfig() { + return $this->data; + } +} diff --git a/apps/files_external/lib/Service/LegacyStoragesService.php b/apps/files_external/lib/Service/LegacyStoragesService.php new file mode 100644 index 00000000000..40f57e45506 --- /dev/null +++ b/apps/files_external/lib/Service/LegacyStoragesService.php @@ -0,0 +1,209 @@ + + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Service; + +use \OCA\Files_external\Lib\StorageConfig; + +/** + * Read mount config from legacy mount.json + */ +abstract class LegacyStoragesService { + /** @var BackendService */ + protected $backendService; + + /** + * Read legacy config data + * + * @return array list of mount configs + */ + abstract protected function readLegacyConfig(); + + /** + * Copy legacy storage options into the given storage config object. + * + * @param StorageConfig $storageConfig storage config to populate + * @param string $mountType mount type + * @param string $applicable applicable user or group + * @param array $storageOptions legacy storage options + * + * @return StorageConfig populated storage config + */ + protected function populateStorageConfigWithLegacyOptions( + &$storageConfig, + $mountType, + $applicable, + $storageOptions + ) { + $backend = $this->backendService->getBackend($storageOptions['backend']); + if (!$backend) { + throw new \UnexpectedValueException('Invalid backend ' . $storageOptions['backend']); + } + $storageConfig->setBackend($backend); + if (isset($storageOptions['authMechanism']) && $storageOptions['authMechanism'] !== 'builtin::builtin') { + $authMechanism = $this->backendService->getAuthMechanism($storageOptions['authMechanism']); + } else { + $authMechanism = $backend->getLegacyAuthMechanism($storageOptions); + $storageOptions['authMechanism'] = 'null'; // to make error handling easier + } + if (!$authMechanism) { + throw new \UnexpectedValueException('Invalid authentication mechanism ' . $storageOptions['authMechanism']); + } + $storageConfig->setAuthMechanism($authMechanism); + $storageConfig->setBackendOptions($storageOptions['options']); + if (isset($storageOptions['mountOptions'])) { + $storageConfig->setMountOptions($storageOptions['mountOptions']); + } + if (!isset($storageOptions['priority'])) { + $storageOptions['priority'] = $backend->getPriority(); + } + $storageConfig->setPriority($storageOptions['priority']); + if ($mountType === \OC_Mount_Config::MOUNT_TYPE_USER) { + $applicableUsers = $storageConfig->getApplicableUsers(); + if ($applicable !== 'all') { + $applicableUsers[] = $applicable; + $storageConfig->setApplicableUsers($applicableUsers); + } + } else if ($mountType === \OC_Mount_Config::MOUNT_TYPE_GROUP) { + $applicableGroups = $storageConfig->getApplicableGroups(); + $applicableGroups[] = $applicable; + $storageConfig->setApplicableGroups($applicableGroups); + } + return $storageConfig; + } + + /** + * Read the external storages config + * + * @return StorageConfig[] map of storage id to storage config + */ + public function getAllStorages() { + $mountPoints = $this->readLegacyConfig(); + /** + * Here is the how the horribly messy mount point array looks like + * from the mount.json file: + * + * $storageOptions = $mountPoints[$mountType][$applicable][$mountPath] + * + * - $mountType is either "user" or "group" + * - $applicable is the name of a user or group (or the current user for personal mounts) + * - $mountPath is the mount point path (where the storage must be mounted) + * - $storageOptions is a map of storage options: + * - "priority": storage priority + * - "backend": backend identifier + * - "class": LEGACY backend class name + * - "options": backend-specific options + * - "authMechanism": authentication mechanism identifier + * - "mountOptions": mount-specific options (ex: disable previews, scanner, etc) + */ + // group by storage id + /** @var StorageConfig[] $storages */ + $storages = []; + // for storages without id (legacy), group by config hash for + // later processing + $storagesWithConfigHash = []; + foreach ($mountPoints as $mountType => $applicables) { + foreach ($applicables as $applicable => $mountPaths) { + foreach ($mountPaths as $rootMountPath => $storageOptions) { + $currentStorage = null; + /** + * Flag whether the config that was read already has an id. + * If not, it will use a config hash instead and generate + * a proper id later + * + * @var boolean + */ + $hasId = false; + // the root mount point is in the format "/$user/files/the/mount/point" + // we remove the "/$user/files" prefix + $parts = explode('/', ltrim($rootMountPath, '/'), 3); + if (count($parts) < 3) { + // something went wrong, skip + \OCP\Util::writeLog( + 'files_external', + 'Could not parse mount point "' . $rootMountPath . '"', + \OCP\Util::ERROR + ); + continue; + } + $relativeMountPath = rtrim($parts[2], '/'); + // note: we cannot do this after the loop because the decrypted config + // options might be needed for the config hash + $storageOptions['options'] = \OC_Mount_Config::decryptPasswords($storageOptions['options']); + if (!isset($storageOptions['backend'])) { + $storageOptions['backend'] = $storageOptions['class']; // legacy compat + } + if (!isset($storageOptions['authMechanism'])) { + $storageOptions['authMechanism'] = null; // ensure config hash works + } + if (isset($storageOptions['id'])) { + $configId = (int)$storageOptions['id']; + if (isset($storages[$configId])) { + $currentStorage = $storages[$configId]; + } + $hasId = true; + } else { + // missing id in legacy config, need to generate + // but at this point we don't know the max-id, so use + // first group it by config hash + $storageOptions['mountpoint'] = $rootMountPath; + $configId = \OC_Mount_Config::makeConfigHash($storageOptions); + if (isset($storagesWithConfigHash[$configId])) { + $currentStorage = $storagesWithConfigHash[$configId]; + } + } + if (is_null($currentStorage)) { + // create new + $currentStorage = new StorageConfig($configId); + $currentStorage->setMountPoint($relativeMountPath); + } + try { + $this->populateStorageConfigWithLegacyOptions( + $currentStorage, + $mountType, + $applicable, + $storageOptions + ); + if ($hasId) { + $storages[$configId] = $currentStorage; + } else { + $storagesWithConfigHash[$configId] = $currentStorage; + } + } catch (\UnexpectedValueException $e) { + // don't die if a storage backend doesn't exist + \OCP\Util::writeLog( + 'files_external', + 'Could not load storage: "' . $e->getMessage() . '"', + \OCP\Util::ERROR + ); + } + } + } + } + + // convert parameter values + foreach ($storages as $storage) { + $storage->getBackend()->validateStorageDefinition($storage); + $storage->getAuthMechanism()->validateStorageDefinition($storage); + } + return $storages; + } +} diff --git a/apps/files_external/lib/Service/StoragesService.php b/apps/files_external/lib/Service/StoragesService.php new file mode 100644 index 00000000000..0999c0a7adf --- /dev/null +++ b/apps/files_external/lib/Service/StoragesService.php @@ -0,0 +1,527 @@ + + * @author Lukas Reschke + * @author Robin Appelman + * @author Robin McCorkell + * @author Vincent Petry + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Service; + +use \OC\Files\Filesystem; +use \OCA\Files_external\Lib\StorageConfig; +use \OCA\Files_external\NotFoundException; +use \OCA\Files_External\Lib\Backend\Backend; +use \OCA\Files_External\Lib\Auth\AuthMechanism; +use OCP\Files\Config\IUserMountCache; +use \OCP\Files\StorageNotAvailableException; + +/** + * Service class to manage external storages + */ +abstract class StoragesService { + + /** @var BackendService */ + protected $backendService; + + /** + * @var DBConfigService + */ + protected $dbConfig; + + /** + * @var IUserMountCache + */ + protected $userMountCache; + + /** + * @param BackendService $backendService + * @param DBConfigService $dbConfigService + * @param IUserMountCache $userMountCache + */ + public function __construct(BackendService $backendService, DBConfigService $dbConfigService, IUserMountCache $userMountCache) { + $this->backendService = $backendService; + $this->dbConfig = $dbConfigService; + $this->userMountCache = $userMountCache; + } + + protected function readDBConfig() { + return $this->dbConfig->getAdminMounts(); + } + + protected function getStorageConfigFromDBMount(array $mount) { + $applicableUsers = array_filter($mount['applicable'], function ($applicable) { + return $applicable['type'] === DBConfigService::APPLICABLE_TYPE_USER; + }); + $applicableUsers = array_map(function ($applicable) { + return $applicable['value']; + }, $applicableUsers); + + $applicableGroups = array_filter($mount['applicable'], function ($applicable) { + return $applicable['type'] === DBConfigService::APPLICABLE_TYPE_GROUP; + }); + $applicableGroups = array_map(function ($applicable) { + return $applicable['value']; + }, $applicableGroups); + + try { + $config = $this->createStorage( + $mount['mount_point'], + $mount['storage_backend'], + $mount['auth_backend'], + $mount['config'], + $mount['options'], + array_values($applicableUsers), + array_values($applicableGroups), + $mount['priority'] + ); + $config->setType($mount['type']); + $config->setId((int)$mount['mount_id']); + return $config; + } catch (\UnexpectedValueException $e) { + // don't die if a storage backend doesn't exist + \OCP\Util::writeLog( + 'files_external', + 'Could not load storage: "' . $e->getMessage() . '"', + \OCP\Util::ERROR + ); + return null; + } catch (\InvalidArgumentException $e) { + \OCP\Util::writeLog( + 'files_external', + 'Could not load storage: "' . $e->getMessage() . '"', + \OCP\Util::ERROR + ); + return null; + } + } + + /** + * Read the external storages config + * + * @return array map of storage id to storage config + */ + protected function readConfig() { + $mounts = $this->readDBConfig(); + $configs = array_map([$this, 'getStorageConfigFromDBMount'], $mounts); + $configs = array_filter($configs, function ($config) { + return $config instanceof StorageConfig; + }); + + $keys = array_map(function (StorageConfig $config) { + return $config->getId(); + }, $configs); + + return array_combine($keys, $configs); + } + + /** + * Get a storage with status + * + * @param int $id storage id + * + * @return StorageConfig + * @throws NotFoundException if the storage with the given id was not found + */ + public function getStorage($id) { + $mount = $this->dbConfig->getMountById($id); + + if (!is_array($mount)) { + throw new NotFoundException('Storage with id "' . $id . '" not found'); + } + + $config = $this->getStorageConfigFromDBMount($mount); + if ($this->isApplicable($config)) { + return $config; + } else { + throw new NotFoundException('Storage with id "' . $id . '" not found'); + } + } + + /** + * Check whether this storage service should provide access to a storage + * + * @param StorageConfig $config + * @return bool + */ + abstract protected function isApplicable(StorageConfig $config); + + /** + * Gets all storages, valid or not + * + * @return StorageConfig[] array of storage configs + */ + public function getAllStorages() { + return $this->readConfig(); + } + + /** + * Gets all valid storages + * + * @return StorageConfig[] + */ + public function getStorages() { + return array_filter($this->getAllStorages(), [$this, 'validateStorage']); + } + + /** + * Validate storage + * FIXME: De-duplicate with StoragesController::validate() + * + * @param StorageConfig $storage + * @return bool + */ + protected function validateStorage(StorageConfig $storage) { + /** @var Backend */ + $backend = $storage->getBackend(); + /** @var AuthMechanism */ + $authMechanism = $storage->getAuthMechanism(); + + if (!$backend->isVisibleFor($this->getVisibilityType())) { + // not permitted to use backend + return false; + } + if (!$authMechanism->isVisibleFor($this->getVisibilityType())) { + // not permitted to use auth mechanism + return false; + } + + return true; + } + + /** + * Get the visibility type for this controller, used in validation + * + * @return string BackendService::VISIBILITY_* constants + */ + abstract public function getVisibilityType(); + + /** + * @return integer + */ + protected function getType() { + return DBConfigService::MOUNT_TYPE_ADMIN; + } + + /** + * Add new storage to the configuration + * + * @param StorageConfig $newStorage storage attributes + * + * @return StorageConfig storage config, with added id + */ + public function addStorage(StorageConfig $newStorage) { + $allStorages = $this->readConfig(); + + $configId = $this->dbConfig->addMount( + $newStorage->getMountPoint(), + $newStorage->getBackend()->getIdentifier(), + $newStorage->getAuthMechanism()->getIdentifier(), + $newStorage->getPriority(), + $this->getType() + ); + + $newStorage->setId($configId); + + foreach ($newStorage->getApplicableUsers() as $user) { + $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_USER, $user); + } + foreach ($newStorage->getApplicableGroups() as $group) { + $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_GROUP, $group); + } + foreach ($newStorage->getBackendOptions() as $key => $value) { + $this->dbConfig->setConfig($configId, $key, $value); + } + foreach ($newStorage->getMountOptions() as $key => $value) { + $this->dbConfig->setOption($configId, $key, $value); + } + + if (count($newStorage->getApplicableUsers()) === 0 && count($newStorage->getApplicableGroups()) === 0) { + $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); + } + + // add new storage + $allStorages[$configId] = $newStorage; + + $this->triggerHooks($newStorage, Filesystem::signal_create_mount); + + $newStorage->setStatus(StorageNotAvailableException::STATUS_SUCCESS); + return $newStorage; + } + + /** + * Create a storage from its parameters + * + * @param string $mountPoint storage mount point + * @param string $backendIdentifier backend identifier + * @param string $authMechanismIdentifier authentication mechanism identifier + * @param array $backendOptions backend-specific options + * @param array|null $mountOptions mount-specific options + * @param array|null $applicableUsers users for which to mount the storage + * @param array|null $applicableGroups groups for which to mount the storage + * @param int|null $priority priority + * + * @return StorageConfig + */ + public function createStorage( + $mountPoint, + $backendIdentifier, + $authMechanismIdentifier, + $backendOptions, + $mountOptions = null, + $applicableUsers = null, + $applicableGroups = null, + $priority = null + ) { + $backend = $this->backendService->getBackend($backendIdentifier); + if (!$backend) { + throw new \InvalidArgumentException('Unable to get backend for ' . $backendIdentifier); + } + $authMechanism = $this->backendService->getAuthMechanism($authMechanismIdentifier); + if (!$authMechanism) { + throw new \InvalidArgumentException('Unable to get authentication mechanism for ' . $authMechanismIdentifier); + } + $newStorage = new StorageConfig(); + $newStorage->setMountPoint($mountPoint); + $newStorage->setBackend($backend); + $newStorage->setAuthMechanism($authMechanism); + $newStorage->setBackendOptions($backendOptions); + if (isset($mountOptions)) { + $newStorage->setMountOptions($mountOptions); + } + if (isset($applicableUsers)) { + $newStorage->setApplicableUsers($applicableUsers); + } + if (isset($applicableGroups)) { + $newStorage->setApplicableGroups($applicableGroups); + } + if (isset($priority)) { + $newStorage->setPriority($priority); + } + + return $newStorage; + } + + /** + * Triggers the given hook signal for all the applicables given + * + * @param string $signal signal + * @param string $mountPoint hook mount pount param + * @param string $mountType hook mount type param + * @param array $applicableArray array of applicable users/groups for which to trigger the hook + */ + protected function triggerApplicableHooks($signal, $mountPoint, $mountType, $applicableArray) { + foreach ($applicableArray as $applicable) { + \OCP\Util::emitHook( + Filesystem::CLASSNAME, + $signal, + [ + Filesystem::signal_param_path => $mountPoint, + Filesystem::signal_param_mount_type => $mountType, + Filesystem::signal_param_users => $applicable, + ] + ); + } + } + + /** + * Triggers $signal for all applicable users of the given + * storage + * + * @param StorageConfig $storage storage data + * @param string $signal signal to trigger + */ + abstract protected function triggerHooks(StorageConfig $storage, $signal); + + /** + * Triggers signal_create_mount or signal_delete_mount to + * accommodate for additions/deletions in applicableUsers + * and applicableGroups fields. + * + * @param StorageConfig $oldStorage old storage data + * @param StorageConfig $newStorage new storage data + */ + abstract protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage); + + /** + * Update storage to the configuration + * + * @param StorageConfig $updatedStorage storage attributes + * + * @return StorageConfig storage config + * @throws NotFoundException if the given storage does not exist in the config + */ + public function updateStorage(StorageConfig $updatedStorage) { + $id = $updatedStorage->getId(); + + $existingMount = $this->dbConfig->getMountById($id); + + if (!is_array($existingMount)) { + throw new NotFoundException('Storage with id "' . $id . '" not found while updating storage'); + } + + $oldStorage = $this->getStorageConfigFromDBMount($existingMount); + + $removedUsers = array_diff($oldStorage->getApplicableUsers(), $updatedStorage->getApplicableUsers()); + $removedGroups = array_diff($oldStorage->getApplicableGroups(), $updatedStorage->getApplicableGroups()); + $addedUsers = array_diff($updatedStorage->getApplicableUsers(), $oldStorage->getApplicableUsers()); + $addedGroups = array_diff($updatedStorage->getApplicableGroups(), $oldStorage->getApplicableGroups()); + + $oldUserCount = count($oldStorage->getApplicableUsers()); + $oldGroupCount = count($oldStorage->getApplicableGroups()); + $newUserCount = count($updatedStorage->getApplicableUsers()); + $newGroupCount = count($updatedStorage->getApplicableGroups()); + $wasGlobal = ($oldUserCount + $oldGroupCount) === 0; + $isGlobal = ($newUserCount + $newGroupCount) === 0; + + foreach ($removedUsers as $user) { + $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, $user); + } + foreach ($removedGroups as $group) { + $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, $group); + } + foreach ($addedUsers as $user) { + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, $user); + } + foreach ($addedGroups as $group) { + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, $group); + } + + if ($wasGlobal && !$isGlobal) { + $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); + } else if (!$wasGlobal && $isGlobal) { + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); + } + + $changedConfig = array_diff_assoc($updatedStorage->getBackendOptions(), $oldStorage->getBackendOptions()); + $changedOptions = array_diff_assoc($updatedStorage->getMountOptions(), $oldStorage->getMountOptions()); + + foreach ($changedConfig as $key => $value) { + $this->dbConfig->setConfig($id, $key, $value); + } + foreach ($changedOptions as $key => $value) { + $this->dbConfig->setOption($id, $key, $value); + } + + if ($updatedStorage->getMountPoint() !== $oldStorage->getMountPoint()) { + $this->dbConfig->setMountPoint($id, $updatedStorage->getMountPoint()); + } + + if ($updatedStorage->getAuthMechanism()->getIdentifier() !== $oldStorage->getAuthMechanism()->getIdentifier()) { + $this->dbConfig->setAuthBackend($id, $updatedStorage->getAuthMechanism()->getIdentifier()); + } + + $this->triggerChangeHooks($oldStorage, $updatedStorage); + + if (($wasGlobal && !$isGlobal) || count($removedGroups) > 0) { // to expensive to properly handle these on the fly + $this->userMountCache->remoteStorageMounts($this->getStorageId($updatedStorage)); + } else { + $storageId = $this->getStorageId($updatedStorage); + foreach ($removedUsers as $userId) { + $this->userMountCache->removeUserStorageMount($storageId, $userId); + } + } + + return $this->getStorage($id); + } + + /** + * Delete the storage with the given id. + * + * @param int $id storage id + * + * @throws NotFoundException if no storage was found with the given id + */ + public function removeStorage($id) { + $existingMount = $this->dbConfig->getMountById($id); + + if (!is_array($existingMount)) { + throw new NotFoundException('Storage with id "' . $id . '" not found'); + } + + $this->dbConfig->removeMount($id); + + $deletedStorage = $this->getStorageConfigFromDBMount($existingMount); + $this->triggerHooks($deletedStorage, Filesystem::signal_delete_mount); + + // delete oc_storages entries and oc_filecache + try { + $rustyStorageId = $this->getRustyStorageIdFromConfig($deletedStorage); + \OC\Files\Cache\Storage::remove($rustyStorageId); + } catch (\Exception $e) { + // can happen either for invalid configs where the storage could not + // be instantiated or whenever $user vars where used, in which case + // the storage id could not be computed + \OCP\Util::writeLog( + 'files_external', + 'Exception: "' . $e->getMessage() . '"', + \OCP\Util::ERROR + ); + } + } + + /** + * Returns the rusty storage id from oc_storages from the given storage config. + * + * @param StorageConfig $storageConfig + * @return string rusty storage id + */ + private function getRustyStorageIdFromConfig(StorageConfig $storageConfig) { + // if any of the storage options contains $user, it is not possible + // to compute the possible storage id as we don't know which users + // mounted it already (and we certainly don't want to iterate over ALL users) + foreach ($storageConfig->getBackendOptions() as $value) { + if (strpos($value, '$user') !== false) { + throw new \Exception('Cannot compute storage id for deletion due to $user vars in the configuration'); + } + } + + // note: similar to ConfigAdapter->prepateStorageConfig() + $storageConfig->getAuthMechanism()->manipulateStorageConfig($storageConfig); + $storageConfig->getBackend()->manipulateStorageConfig($storageConfig); + + $class = $storageConfig->getBackend()->getStorageClass(); + $storageImpl = new $class($storageConfig->getBackendOptions()); + + return $storageImpl->getId(); + } + + /** + * Construct the storage implementation + * + * @param StorageConfig $storageConfig + * @return int + */ + private function getStorageId(StorageConfig $storageConfig) { + try { + $class = $storageConfig->getBackend()->getStorageClass(); + /** @var \OC\Files\Storage\Storage $storage */ + $storage = new $class($storageConfig->getBackendOptions()); + + // auth mechanism should fire first + $storage = $storageConfig->getBackend()->wrapStorage($storage); + $storage = $storageConfig->getAuthMechanism()->wrapStorage($storage); + + return $storage->getStorageCache()->getNumericId(); + } catch (\Exception $e) { + return -1; + } + } +} diff --git a/apps/files_external/lib/Service/UserGlobalStoragesService.php b/apps/files_external/lib/Service/UserGlobalStoragesService.php new file mode 100644 index 00000000000..b5e2d13c712 --- /dev/null +++ b/apps/files_external/lib/Service/UserGlobalStoragesService.php @@ -0,0 +1,174 @@ + + * @author Robin McCorkell + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Service; + +use OCP\Files\Config\IUserMountCache; +use \OCP\IUserSession; +use \OCP\IGroupManager; +use \OCA\Files_External\Lib\StorageConfig; + +/** + * Service class to read global storages applicable to the user + * Read-only access available, attempting to write will throw DomainException + */ +class UserGlobalStoragesService extends GlobalStoragesService { + + use UserTrait; + + /** @var IGroupManager */ + protected $groupManager; + + /** + * @param BackendService $backendService + * @param DBConfigService $dbConfig + * @param IUserSession $userSession + * @param IGroupManager $groupManager + * @param IUserMountCache $userMountCache + */ + public function __construct( + BackendService $backendService, + DBConfigService $dbConfig, + IUserSession $userSession, + IGroupManager $groupManager, + IUserMountCache $userMountCache + ) { + parent::__construct($backendService, $dbConfig, $userMountCache); + $this->userSession = $userSession; + $this->groupManager = $groupManager; + } + + /** + * Replace config hash ID with real IDs, for migrating legacy storages + * + * @param StorageConfig[] $storages Storages with real IDs + * @param StorageConfig[] $storagesWithConfigHash Storages with config hash IDs + */ + protected function setRealStorageIds(array &$storages, array $storagesWithConfigHash) { + // as a read-only view, storage IDs don't need to be real + foreach ($storagesWithConfigHash as $storage) { + $storages[$storage->getId()] = $storage; + } + } + + protected function readDBConfig() { + $userMounts = $this->dbConfig->getAdminMountsFor(DBConfigService::APPLICABLE_TYPE_USER, $this->getUser()->getUID()); + $globalMounts = $this->dbConfig->getAdminMountsFor(DBConfigService::APPLICABLE_TYPE_GLOBAL, null); + $groups = $this->groupManager->getUserGroupIds($this->getUser()); + if (is_array($groups) && count($groups) !== 0) { + $groupMounts = $this->dbConfig->getAdminMountsForMultiple(DBConfigService::APPLICABLE_TYPE_GROUP, $groups); + } else { + $groupMounts = []; + } + return array_merge($userMounts, $groupMounts, $globalMounts); + } + + public function addStorage(StorageConfig $newStorage) { + throw new \DomainException('UserGlobalStoragesService writing disallowed'); + } + + public function updateStorage(StorageConfig $updatedStorage) { + throw new \DomainException('UserGlobalStoragesService writing disallowed'); + } + + /** + * @param integer $id + */ + public function removeStorage($id) { + throw new \DomainException('UserGlobalStoragesService writing disallowed'); + } + + /** + * Get unique storages, in case two are defined with the same mountpoint + * Higher priority storages take precedence + * + * @return StorageConfig[] + */ + public function getUniqueStorages() { + $storages = $this->getStorages(); + + $storagesByMountpoint = []; + foreach ($storages as $storage) { + $storagesByMountpoint[$storage->getMountPoint()][] = $storage; + } + + $result = []; + foreach ($storagesByMountpoint as $storageList) { + $storage = array_reduce($storageList, function ($carry, $item) { + if (isset($carry)) { + $carryPriorityType = $this->getPriorityType($carry); + $itemPriorityType = $this->getPriorityType($item); + if ($carryPriorityType > $itemPriorityType) { + return $carry; + } elseif ($carryPriorityType === $itemPriorityType) { + if ($carry->getPriority() > $item->getPriority()) { + return $carry; + } + } + } + return $item; + }); + $result[$storage->getID()] = $storage; + } + + return $result; + } + + /** + * Get a priority 'type', where a bigger number means higher priority + * user applicable > group applicable > 'all' + * + * @param StorageConfig $storage + * @return int + */ + protected function getPriorityType(StorageConfig $storage) { + $applicableUsers = $storage->getApplicableUsers(); + $applicableGroups = $storage->getApplicableGroups(); + + if ($applicableUsers && $applicableUsers[0] !== 'all') { + return 2; + } + if ($applicableGroups) { + return 1; + } + return 0; + } + + protected function isApplicable(StorageConfig $config) { + $applicableUsers = $config->getApplicableUsers(); + $applicableGroups = $config->getApplicableGroups(); + + if (count($applicableUsers) === 0 && count($applicableGroups) === 0) { + return true; + } + if (in_array($this->getUser()->getUID(), $applicableUsers, true)) { + return true; + } + $groupIds = $this->groupManager->getUserGroupIds($this->getUser()); + foreach ($groupIds as $groupId) { + if (in_array($groupId, $applicableGroups, true)) { + return true; + } + } + return false; + } +} diff --git a/apps/files_external/lib/Service/UserLegacyStoragesService.php b/apps/files_external/lib/Service/UserLegacyStoragesService.php new file mode 100644 index 00000000000..c64520c8b02 --- /dev/null +++ b/apps/files_external/lib/Service/UserLegacyStoragesService.php @@ -0,0 +1,54 @@ + + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Service; + +use OCP\IUserSession; + +/** + * Read user defined mounts from the legacy mount.json + */ +class UserLegacyStoragesService extends LegacyStoragesService { + /** + * @var IUserSession + */ + private $userSession; + + /** + * @param BackendService $backendService + * @param IUserSession $userSession + */ + public function __construct(BackendService $backendService, IUserSession $userSession) { + $this->backendService = $backendService; + $this->userSession = $userSession; + } + + /** + * Read legacy config data + * + * @return array list of storage configs + */ + protected function readLegacyConfig() { + // read user config + $user = $this->userSession->getUser()->getUID(); + return \OC_Mount_Config::readData($user); + } +} diff --git a/apps/files_external/lib/Service/UserStoragesService.php b/apps/files_external/lib/Service/UserStoragesService.php new file mode 100644 index 00000000000..b64f289b3ee --- /dev/null +++ b/apps/files_external/lib/Service/UserStoragesService.php @@ -0,0 +1,141 @@ + + * @author Robin McCorkell + * @author Vincent Petry + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Service; + +use OCP\Files\Config\IUserMountCache; +use \OCP\IUserSession; +use \OC\Files\Filesystem; + +use \OCA\Files_external\Lib\StorageConfig; +use \OCA\Files_external\NotFoundException; +use \OCA\Files_External\Service\BackendService; +use \OCA\Files_External\Service\UserTrait; + +/** + * Service class to manage user external storages + * (aka personal storages) + */ +class UserStoragesService extends StoragesService { + use UserTrait; + + /** + * Create a user storages service + * + * @param BackendService $backendService + * @param DBConfigService $dbConfig + * @param IUserSession $userSession user session + * @param IUserMountCache $userMountCache + */ + public function __construct( + BackendService $backendService, + DBConfigService $dbConfig, + IUserSession $userSession, + IUserMountCache $userMountCache + ) { + $this->userSession = $userSession; + parent::__construct($backendService, $dbConfig, $userMountCache); + } + + protected function readDBConfig() { + return $this->dbConfig->getUserMountsFor(DBConfigService::APPLICABLE_TYPE_USER, $this->getUser()->getUID()); + } + + /** + * Triggers $signal for all applicable users of the given + * storage + * + * @param StorageConfig $storage storage data + * @param string $signal signal to trigger + */ + protected function triggerHooks(StorageConfig $storage, $signal) { + $user = $this->getUser()->getUID(); + + // trigger hook for the current user + $this->triggerApplicableHooks( + $signal, + $storage->getMountPoint(), + \OC_Mount_Config::MOUNT_TYPE_USER, + [$user] + ); + } + + /** + * Triggers signal_create_mount or signal_delete_mount to + * accommodate for additions/deletions in applicableUsers + * and applicableGroups fields. + * + * @param StorageConfig $oldStorage old storage data + * @param StorageConfig $newStorage new storage data + */ + protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage) { + // if mount point changed, it's like a deletion + creation + if ($oldStorage->getMountPoint() !== $newStorage->getMountPoint()) { + $this->triggerHooks($oldStorage, Filesystem::signal_delete_mount); + $this->triggerHooks($newStorage, Filesystem::signal_create_mount); + } + } + + protected function getType() { + return DBConfigService::MOUNT_TYPE_PERSONAl; + } + + /** + * Add new storage to the configuration + * + * @param StorageConfig $newStorage storage attributes + * + * @return StorageConfig storage config, with added id + */ + public function addStorage(StorageConfig $newStorage) { + $newStorage->setApplicableUsers([$this->getUser()->getUID()]); + $config = parent::addStorage($newStorage); + return $config; + } + + /** + * Update storage to the configuration + * + * @param StorageConfig $updatedStorage storage attributes + * + * @return StorageConfig storage config + * @throws NotFoundException if the given storage does not exist in the config + */ + public function updateStorage(StorageConfig $updatedStorage) { + $updatedStorage->setApplicableUsers([$this->getUser()->getUID()]); + return parent::updateStorage($updatedStorage); + } + + /** + * Get the visibility type for this controller, used in validation + * + * @return string BackendService::VISIBILITY_* constants + */ + public function getVisibilityType() { + return BackendService::VISIBILITY_PERSONAL; + } + + protected function isApplicable(StorageConfig $config) { + return ($config->getApplicableUsers() === [$this->getUser()->getUID()]) && $config->getType() === StorageConfig::MOUNT_TYPE_PERSONAl; + } +} diff --git a/apps/files_external/lib/Service/UserTrait.php b/apps/files_external/lib/Service/UserTrait.php new file mode 100644 index 00000000000..536c0f67e1f --- /dev/null +++ b/apps/files_external/lib/Service/UserTrait.php @@ -0,0 +1,74 @@ + + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Service; + +use \OCP\IUserSession; +use \OCP\IUser; + +/** + * Trait for getting user information in a service + */ +trait UserTrait { + + /** @var IUserSession */ + protected $userSession; + + /** + * User override + * + * @var IUser|null + */ + private $user = null; + + /** + * @return IUser|null + */ + protected function getUser() { + if ($this->user) { + return $this->user; + } + return $this->userSession->getUser(); + } + + /** + * Override the user from the session + * Unset with ->resetUser() when finished! + * + * @param IUser + * @return self + */ + public function setUser(IUser $user) { + $this->user = $user; + return $this; + } + + /** + * Reset the user override + * + * @return self + */ + public function resetUser() { + $this->user = null; + return $this; + } +} + diff --git a/apps/files_external/lib/config/configadapter.php b/apps/files_external/lib/config/configadapter.php index a19a111d3d9..a05ff555f72 100644 --- a/apps/files_external/lib/config/configadapter.php +++ b/apps/files_external/lib/config/configadapter.php @@ -24,14 +24,14 @@ namespace OCA\Files_External\Config; use OC\Files\Storage\Wrapper\Availability; -use OCA\Files_external\Migration\StorageMigrator; +use OCA\Files_External\Migration\StorageMigrator; use OCP\Files\Storage; use OC\Files\Mount\MountPoint; use OCP\Files\Storage\IStorageFactory; use OCA\Files_External\Lib\PersonalMount; use OCP\Files\Config\IMountProvider; use OCP\IUser; -use OCA\Files_external\Service\UserStoragesService; +use OCA\Files_External\Service\UserStoragesService; use OCA\Files_External\Service\UserGlobalStoragesService; use OCA\Files_External\Lib\StorageConfig; use OC\Files\Storage\FailedStorage; diff --git a/apps/files_external/migration/dummyusersession.php b/apps/files_external/migration/dummyusersession.php deleted file mode 100644 index bb4ef19dace..00000000000 --- a/apps/files_external/migration/dummyusersession.php +++ /dev/null @@ -1,51 +0,0 @@ - - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_external\Migration; - -use OCP\IUser; -use OCP\IUserSession; - -class DummyUserSession implements IUserSession { - - /** - * @var IUser - */ - private $user; - - public function login($user, $password) { - } - - public function logout() { - } - - public function setUser($user) { - $this->user = $user; - } - - public function getUser() { - return $this->user; - } - - public function isLoggedIn() { - return !is_null($this->user); - } -} diff --git a/apps/files_external/migration/storagemigrator.php b/apps/files_external/migration/storagemigrator.php deleted file mode 100644 index 48ab52768e2..00000000000 --- a/apps/files_external/migration/storagemigrator.php +++ /dev/null @@ -1,147 +0,0 @@ - - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_external\Migration; - -use OCA\Files_External\Service\BackendService; -use OCA\Files_External\Service\DBConfigService; -use OCA\Files_external\Service\GlobalLegacyStoragesService; -use OCA\Files_external\Service\GlobalStoragesService; -use OCA\Files_external\Service\LegacyStoragesService; -use OCA\Files_external\Service\StoragesService; -use OCA\Files_external\Service\UserLegacyStoragesService; -use OCA\Files_external\Service\UserStoragesService; -use OCP\Files\Config\IUserMountCache; -use OCP\IConfig; -use OCP\IDBConnection; -use OCP\ILogger; -use OCP\IUser; -use OCP\IUserSession; - -/** - * Migrate mount config from mount.json to the database - */ -class StorageMigrator { - /** - * @var BackendService - */ - private $backendService; - - /** - * @var DBConfigService - */ - private $dbConfig; - - /** - * @var IConfig - */ - private $config; - - /** - * @var IDBConnection - */ - private $connection; - - /** - * @var ILogger - */ - private $logger; - - /** @var IUserMountCache */ - private $userMountCache; - - /** - * StorageMigrator constructor. - * - * @param BackendService $backendService - * @param DBConfigService $dbConfig - * @param IConfig $config - * @param IDBConnection $connection - * @param ILogger $logger - * @param IUserMountCache $userMountCache - */ - public function __construct( - BackendService $backendService, - DBConfigService $dbConfig, - IConfig $config, - IDBConnection $connection, - ILogger $logger, - IUserMountCache $userMountCache - ) { - $this->backendService = $backendService; - $this->dbConfig = $dbConfig; - $this->config = $config; - $this->connection = $connection; - $this->logger = $logger; - $this->userMountCache = $userMountCache; - } - - private function migrate(LegacyStoragesService $legacyService, StoragesService $storageService) { - $existingStorage = $legacyService->getAllStorages(); - - $this->connection->beginTransaction(); - try { - foreach ($existingStorage as $storage) { - $mountOptions = $storage->getMountOptions(); - if (!empty($mountOptions) && !isset($mountOptions['enable_sharing'])) { - // existing mounts must have sharing enabled by default to avoid surprises - $mountOptions['enable_sharing'] = true; - $storage->setMountOptions($mountOptions); - } - $storageService->addStorage($storage); - } - $this->connection->commit(); - } catch (\Exception $e) { - $this->logger->logException($e); - $this->connection->rollBack(); - } - } - - /** - * Migrate admin configured storages - */ - public function migrateGlobal() { - $legacyService = new GlobalLegacyStoragesService($this->backendService); - $storageService = new GlobalStoragesService($this->backendService, $this->dbConfig, $this->userMountCache); - - $this->migrate($legacyService, $storageService); - } - - /** - * Migrate personal storages configured by the current user - * - * @param IUser $user - */ - public function migrateUser(IUser $user) { - $dummySession = new DummyUserSession(); - $dummySession->setUser($user); - $userId = $user->getUID(); - $userVersion = $this->config->getUserValue($userId, 'files_external', 'config_version', '0.0.0'); - if (version_compare($userVersion, '0.5.0', '<')) { - $this->config->setUserValue($userId, 'files_external', 'config_version', '0.5.0'); - $legacyService = new UserLegacyStoragesService($this->backendService, $dummySession); - $storageService = new UserStoragesService($this->backendService, $this->dbConfig, $dummySession, $this->userMountCache); - - $this->migrate($legacyService, $storageService); - } - } -} diff --git a/apps/files_external/personal.php b/apps/files_external/personal.php index 5c568f45b7d..195b826a4c2 100644 --- a/apps/files_external/personal.php +++ b/apps/files_external/personal.php @@ -29,7 +29,7 @@ use \OCA\Files_External\Service\BackendService; // we must use the same container $appContainer = \OC_Mount_Config::$app->getContainer(); $backendService = $appContainer->query('OCA\Files_External\Service\BackendService'); -$userStoragesService = $appContainer->query('OCA\Files_external\Service\UserStoragesService'); +$userStoragesService = $appContainer->query('OCA\Files_External\Service\UserStoragesService'); $tmpl = new OCP\Template('files_external', 'settings'); $tmpl->assign('encryptionEnabled', \OC::$server->getEncryptionManager()->isEnabled()); diff --git a/apps/files_external/service/backendservice.php b/apps/files_external/service/backendservice.php deleted file mode 100644 index c3dc4da0177..00000000000 --- a/apps/files_external/service/backendservice.php +++ /dev/null @@ -1,282 +0,0 @@ - - * @author Robin McCorkell - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_External\Service; - -use \OCP\IConfig; - -use \OCA\Files_External\Lib\Backend\Backend; -use \OCA\Files_External\Lib\Auth\AuthMechanism; -use \OCA\Files_External\Lib\Config\IBackendProvider; -use \OCA\Files_External\Lib\Config\IAuthMechanismProvider; - -/** - * Service class to manage backend definitions - */ -class BackendService { - - /** Visibility constants for VisibilityTrait */ - const VISIBILITY_NONE = 0; - const VISIBILITY_PERSONAL = 1; - const VISIBILITY_ADMIN = 2; - //const VISIBILITY_ALIENS = 4; - - const VISIBILITY_DEFAULT = 3; // PERSONAL | ADMIN - - /** Priority constants for PriorityTrait */ - const PRIORITY_DEFAULT = 100; - - /** @var IConfig */ - protected $config; - - /** @var bool */ - private $userMountingAllowed = true; - - /** @var string[] */ - private $userMountingBackends = []; - - /** @var Backend[] */ - private $backends = []; - - /** @var IBackendProvider[] */ - private $backendProviders = []; - - /** @var AuthMechanism[] */ - private $authMechanisms = []; - - /** @var IAuthMechanismProvider[] */ - private $authMechanismProviders = []; - - /** - * @param IConfig $config - */ - public function __construct( - IConfig $config - ) { - $this->config = $config; - - // Load config values - if ($this->config->getAppValue('files_external', 'allow_user_mounting', 'yes') !== 'yes') { - $this->userMountingAllowed = false; - } - $this->userMountingBackends = explode(',', - $this->config->getAppValue('files_external', 'user_mounting_backends', '') - ); - - // if no backend is in the list an empty string is in the array and user mounting is disabled - if ($this->userMountingBackends === ['']) { - $this->userMountingAllowed = false; - } - } - - /** - * Register a backend provider - * - * @since 9.1.0 - * @param IBackendProvider $provider - */ - public function registerBackendProvider(IBackendProvider $provider) { - $this->backendProviders[] = $provider; - } - - private function loadBackendProviders() { - foreach ($this->backendProviders as $provider) { - $this->registerBackends($provider->getBackends()); - } - $this->backendProviders = []; - } - - /** - * Register an auth mechanism provider - * - * @since 9.1.0 - * @param IAuthMechanismProvider $provider - */ - public function registerAuthMechanismProvider(IAuthMechanismProvider $provider) { - $this->authMechanismProviders[] = $provider; - } - - private function loadAuthMechanismProviders() { - foreach ($this->authMechanismProviders as $provider) { - $this->registerAuthMechanisms($provider->getAuthMechanisms()); - } - $this->authMechanismProviders = []; - } - - /** - * Register a backend - * - * @deprecated 9.1.0 use registerBackendProvider() - * @param Backend $backend - */ - public function registerBackend(Backend $backend) { - if (!$this->isAllowedUserBackend($backend)) { - $backend->removeVisibility(BackendService::VISIBILITY_PERSONAL); - } - foreach ($backend->getIdentifierAliases() as $alias) { - $this->backends[$alias] = $backend; - } - } - - /** - * @deprecated 9.1.0 use registerBackendProvider() - * @param Backend[] $backends - */ - public function registerBackends(array $backends) { - foreach ($backends as $backend) { - $this->registerBackend($backend); - } - } - /** - * Register an authentication mechanism - * - * @deprecated 9.1.0 use registerAuthMechanismProvider() - * @param AuthMechanism $authMech - */ - public function registerAuthMechanism(AuthMechanism $authMech) { - if (!$this->isAllowedAuthMechanism($authMech)) { - $authMech->removeVisibility(BackendService::VISIBILITY_PERSONAL); - } - foreach ($authMech->getIdentifierAliases() as $alias) { - $this->authMechanisms[$alias] = $authMech; - } - } - - /** - * @deprecated 9.1.0 use registerAuthMechanismProvider() - * @param AuthMechanism[] $mechanisms - */ - public function registerAuthMechanisms(array $mechanisms) { - foreach ($mechanisms as $mechanism) { - $this->registerAuthMechanism($mechanism); - } - } - - /** - * Get all backends - * - * @return Backend[] - */ - public function getBackends() { - $this->loadBackendProviders(); - // only return real identifiers, no aliases - $backends = []; - foreach ($this->backends as $backend) { - $backends[$backend->getIdentifier()] = $backend; - } - return $backends; - } - - /** - * Get all available backends - * - * @return Backend[] - */ - public function getAvailableBackends() { - return array_filter($this->getBackends(), function($backend) { - return !($backend->checkDependencies()); - }); - } - - /** - * @param string $identifier - * @return Backend|null - */ - public function getBackend($identifier) { - $this->loadBackendProviders(); - if (isset($this->backends[$identifier])) { - return $this->backends[$identifier]; - } - return null; - } - - /** - * Get all authentication mechanisms - * - * @return AuthMechanism[] - */ - public function getAuthMechanisms() { - $this->loadAuthMechanismProviders(); - // only return real identifiers, no aliases - $mechanisms = []; - foreach ($this->authMechanisms as $mechanism) { - $mechanisms[$mechanism->getIdentifier()] = $mechanism; - } - return $mechanisms; - } - - /** - * Get all authentication mechanisms for schemes - * - * @param string[] $schemes - * @return AuthMechanism[] - */ - public function getAuthMechanismsByScheme(array $schemes) { - return array_filter($this->getAuthMechanisms(), function($authMech) use ($schemes) { - return in_array($authMech->getScheme(), $schemes, true); - }); - } - - /** - * @param string $identifier - * @return AuthMechanism|null - */ - public function getAuthMechanism($identifier) { - $this->loadAuthMechanismProviders(); - if (isset($this->authMechanisms[$identifier])) { - return $this->authMechanisms[$identifier]; - } - return null; - } - - /** - * @return bool - */ - public function isUserMountingAllowed() { - return $this->userMountingAllowed; - } - - /** - * Check a backend if a user is allowed to mount it - * - * @param Backend $backend - * @return bool - */ - protected function isAllowedUserBackend(Backend $backend) { - if ($this->userMountingAllowed && - array_intersect($backend->getIdentifierAliases(), $this->userMountingBackends) - ) { - return true; - } - return false; - } - - /** - * Check an authentication mechanism if a user is allowed to use it - * - * @param AuthMechanism $authMechanism - * @return bool - */ - protected function isAllowedAuthMechanism(AuthMechanism $authMechanism) { - return true; // not implemented - } -} diff --git a/apps/files_external/service/dbconfigservice.php b/apps/files_external/service/dbconfigservice.php deleted file mode 100644 index 9f7061eb938..00000000000 --- a/apps/files_external/service/dbconfigservice.php +++ /dev/null @@ -1,451 +0,0 @@ - - * @author Robin Appelman - * @author Robin McCorkell - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_External\Service; - -use OCP\DB\QueryBuilder\IQueryBuilder; -use OCP\IDBConnection; -use OCP\Security\ICrypto; - -/** - * Stores the mount config in the database - */ -class DBConfigService { - const MOUNT_TYPE_ADMIN = 1; - const MOUNT_TYPE_PERSONAl = 2; - - const APPLICABLE_TYPE_GLOBAL = 1; - const APPLICABLE_TYPE_GROUP = 2; - const APPLICABLE_TYPE_USER = 3; - - /** - * @var IDBConnection - */ - private $connection; - - /** - * @var ICrypto - */ - private $crypto; - - /** - * DBConfigService constructor. - * - * @param IDBConnection $connection - * @param ICrypto $crypto - */ - public function __construct(IDBConnection $connection, ICrypto $crypto) { - $this->connection = $connection; - $this->crypto = $crypto; - } - - /** - * @param int $mountId - * @return array - */ - public function getMountById($mountId) { - $builder = $this->connection->getQueryBuilder(); - $query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type']) - ->from('external_mounts', 'm') - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); - $mounts = $this->getMountsFromQuery($query); - if (count($mounts) > 0) { - return $mounts[0]; - } else { - return null; - } - } - - /** - * Get admin defined mounts - * - * @return array - */ - public function getAdminMounts() { - $builder = $this->connection->getQueryBuilder(); - $query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type']) - ->from('external_mounts') - ->where($builder->expr()->eq('type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT))); - return $this->getMountsFromQuery($query); - } - - protected function getForQuery(IQueryBuilder $builder, $type, $value) { - $query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type']) - ->from('external_mounts', 'm') - ->innerJoin('m', 'external_applicable', 'a', $builder->expr()->eq('m.mount_id', 'a.mount_id')) - ->where($builder->expr()->eq('a.type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT))); - - if (is_null($value)) { - $query = $query->andWhere($builder->expr()->isNull('a.value')); - } else { - $query = $query->andWhere($builder->expr()->eq('a.value', $builder->createNamedParameter($value))); - } - - return $query; - } - - /** - * Get mounts by applicable - * - * @param int $type any of the self::APPLICABLE_TYPE_ constants - * @param string|null $value user_id, group_id or null for global mounts - * @return array - */ - public function getMountsFor($type, $value) { - $builder = $this->connection->getQueryBuilder(); - $query = $this->getForQuery($builder, $type, $value); - - return $this->getMountsFromQuery($query); - } - - /** - * Get admin defined mounts by applicable - * - * @param int $type any of the self::APPLICABLE_TYPE_ constants - * @param string|null $value user_id, group_id or null for global mounts - * @return array - */ - public function getAdminMountsFor($type, $value) { - $builder = $this->connection->getQueryBuilder(); - $query = $this->getForQuery($builder, $type, $value); - $query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT))); - - return $this->getMountsFromQuery($query); - } - - /** - * Get admin defined mounts for multiple applicable - * - * @param int $type any of the self::APPLICABLE_TYPE_ constants - * @param string[] $values user_ids or group_ids - * @return array - */ - public function getAdminMountsForMultiple($type, array $values) { - $builder = $this->connection->getQueryBuilder(); - $params = array_map(function ($value) use ($builder) { - return $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR); - }, $values); - - $query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type']) - ->from('external_mounts', 'm') - ->innerJoin('m', 'external_applicable', 'a', $builder->expr()->eq('m.mount_id', 'a.mount_id')) - ->where($builder->expr()->eq('a.type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT))) - ->andWhere($builder->expr()->in('a.value', $params)); - $query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT))); - - return $this->getMountsFromQuery($query); - } - - /** - * Get user defined mounts by applicable - * - * @param int $type any of the self::APPLICABLE_TYPE_ constants - * @param string|null $value user_id, group_id or null for global mounts - * @return array - */ - public function getUserMountsFor($type, $value) { - $builder = $this->connection->getQueryBuilder(); - $query = $this->getForQuery($builder, $type, $value); - $query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_PERSONAl, IQueryBuilder::PARAM_INT))); - - return $this->getMountsFromQuery($query); - } - - /** - * Add a mount to the database - * - * @param string $mountPoint - * @param string $storageBackend - * @param string $authBackend - * @param int $priority - * @param int $type self::MOUNT_TYPE_ADMIN or self::MOUNT_TYPE_PERSONAL - * @return int the id of the new mount - */ - public function addMount($mountPoint, $storageBackend, $authBackend, $priority, $type) { - if (!$priority) { - $priority = 100; - } - $builder = $this->connection->getQueryBuilder(); - $query = $builder->insert('external_mounts') - ->values([ - 'mount_point' => $builder->createNamedParameter($mountPoint, IQueryBuilder::PARAM_STR), - 'storage_backend' => $builder->createNamedParameter($storageBackend, IQueryBuilder::PARAM_STR), - 'auth_backend' => $builder->createNamedParameter($authBackend, IQueryBuilder::PARAM_STR), - 'priority' => $builder->createNamedParameter($priority, IQueryBuilder::PARAM_INT), - 'type' => $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT) - ]); - $query->execute(); - return (int)$this->connection->lastInsertId('external_mounts'); - } - - /** - * Remove a mount from the database - * - * @param int $mountId - */ - public function removeMount($mountId) { - $builder = $this->connection->getQueryBuilder(); - $query = $builder->delete('external_mounts') - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); - $query->execute(); - - $query = $builder->delete('external_applicable') - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); - $query->execute(); - - $query = $builder->delete('external_config') - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); - $query->execute(); - - $query = $builder->delete('external_options') - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); - $query->execute(); - } - - /** - * @param int $mountId - * @param string $newMountPoint - */ - public function setMountPoint($mountId, $newMountPoint) { - $builder = $this->connection->getQueryBuilder(); - - $query = $builder->update('external_mounts') - ->set('mount_point', $builder->createNamedParameter($newMountPoint)) - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); - - $query->execute(); - } - - /** - * @param int $mountId - * @param string $newAuthBackend - */ - public function setAuthBackend($mountId, $newAuthBackend) { - $builder = $this->connection->getQueryBuilder(); - - $query = $builder->update('external_mounts') - ->set('auth_backend', $builder->createNamedParameter($newAuthBackend)) - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); - - $query->execute(); - } - - /** - * @param int $mountId - * @param string $key - * @param string $value - */ - public function setConfig($mountId, $key, $value) { - if ($key === 'password') { - $value = $this->encryptValue($value); - } - $count = $this->connection->insertIfNotExist('*PREFIX*external_config', [ - 'mount_id' => $mountId, - 'key' => $key, - 'value' => $value - ], ['mount_id', 'key']); - if ($count === 0) { - $builder = $this->connection->getQueryBuilder(); - $query = $builder->update('external_config') - ->set('value', $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR)) - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))) - ->andWhere($builder->expr()->eq('key', $builder->createNamedParameter($key, IQueryBuilder::PARAM_STR))); - $query->execute(); - } - } - - /** - * @param int $mountId - * @param string $key - * @param string $value - */ - public function setOption($mountId, $key, $value) { - - $count = $this->connection->insertIfNotExist('*PREFIX*external_options', [ - 'mount_id' => $mountId, - 'key' => $key, - 'value' => json_encode($value) - ], ['mount_id', 'key']); - if ($count === 0) { - $builder = $this->connection->getQueryBuilder(); - $query = $builder->update('external_options') - ->set('value', $builder->createNamedParameter(json_encode($value), IQueryBuilder::PARAM_STR)) - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))) - ->andWhere($builder->expr()->eq('key', $builder->createNamedParameter($key, IQueryBuilder::PARAM_STR))); - $query->execute(); - } - } - - public function addApplicable($mountId, $type, $value) { - $this->connection->insertIfNotExist('*PREFIX*external_applicable', [ - 'mount_id' => $mountId, - 'type' => $type, - 'value' => $value - ], ['mount_id', 'type', 'value']); - } - - public function removeApplicable($mountId, $type, $value) { - $builder = $this->connection->getQueryBuilder(); - $query = $builder->delete('external_applicable') - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))) - ->andWhere($builder->expr()->eq('type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT))); - - if (is_null($value)) { - $query = $query->andWhere($builder->expr()->isNull('value')); - } else { - $query = $query->andWhere($builder->expr()->eq('value', $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR))); - } - - $query->execute(); - } - - private function getMountsFromQuery(IQueryBuilder $query) { - $result = $query->execute(); - $mounts = $result->fetchAll(); - $uniqueMounts = []; - foreach ($mounts as $mount) { - $id = $mount['mount_id']; - if (!isset($uniqueMounts[$id])) { - $uniqueMounts[$id] = $mount; - } - } - $uniqueMounts = array_values($uniqueMounts); - - $mountIds = array_map(function ($mount) { - return $mount['mount_id']; - }, $uniqueMounts); - $mountIds = array_values(array_unique($mountIds)); - - $applicable = $this->getApplicableForMounts($mountIds); - $config = $this->getConfigForMounts($mountIds); - $options = $this->getOptionsForMounts($mountIds); - - return array_map(function ($mount, $applicable, $config, $options) { - $mount['type'] = (int)$mount['type']; - $mount['priority'] = (int)$mount['priority']; - $mount['applicable'] = $applicable; - $mount['config'] = $config; - $mount['options'] = $options; - return $mount; - }, $uniqueMounts, $applicable, $config, $options); - } - - /** - * Get mount options from a table grouped by mount id - * - * @param string $table - * @param string[] $fields - * @param int[] $mountIds - * @return array [$mountId => [['field1' => $value1, ...], ...], ...] - */ - private function selectForMounts($table, array $fields, array $mountIds) { - if (count($mountIds) === 0) { - return []; - } - $builder = $this->connection->getQueryBuilder(); - $fields[] = 'mount_id'; - $placeHolders = array_map(function ($id) use ($builder) { - return $builder->createPositionalParameter($id, IQueryBuilder::PARAM_INT); - }, $mountIds); - $query = $builder->select($fields) - ->from($table) - ->where($builder->expr()->in('mount_id', $placeHolders)); - $rows = $query->execute()->fetchAll(); - - $result = []; - foreach ($mountIds as $mountId) { - $result[$mountId] = []; - } - foreach ($rows as $row) { - if (isset($row['type'])) { - $row['type'] = (int)$row['type']; - } - $result[$row['mount_id']][] = $row; - } - return $result; - } - - /** - * @param int[] $mountIds - * @return array [$id => [['type' => $type, 'value' => $value], ...], ...] - */ - public function getApplicableForMounts($mountIds) { - return $this->selectForMounts('external_applicable', ['type', 'value'], $mountIds); - } - - /** - * @param int[] $mountIds - * @return array [$id => ['key1' => $value1, ...], ...] - */ - public function getConfigForMounts($mountIds) { - $mountConfigs = $this->selectForMounts('external_config', ['key', 'value'], $mountIds); - return array_map([$this, 'createKeyValueMap'], $mountConfigs); - } - - /** - * @param int[] $mountIds - * @return array [$id => ['key1' => $value1, ...], ...] - */ - public function getOptionsForMounts($mountIds) { - $mountOptions = $this->selectForMounts('external_options', ['key', 'value'], $mountIds); - $optionsMap = array_map([$this, 'createKeyValueMap'], $mountOptions); - return array_map(function (array $options) { - return array_map(function ($option) { - return json_decode($option); - }, $options); - }, $optionsMap); - } - - /** - * @param array $keyValuePairs [['key'=>$key, 'value=>$value], ...] - * @return array ['key1' => $value1, ...] - */ - private function createKeyValueMap(array $keyValuePairs) { - $decryptedPairts = array_map(function ($pair) { - if ($pair['key'] === 'password') { - $pair['value'] = $this->decryptValue($pair['value']); - } - return $pair; - }, $keyValuePairs); - $keys = array_map(function ($pair) { - return $pair['key']; - }, $decryptedPairts); - $values = array_map(function ($pair) { - return $pair['value']; - }, $decryptedPairts); - - return array_combine($keys, $values); - } - - private function encryptValue($value) { - return $this->crypto->encrypt($value); - } - - private function decryptValue($value) { - try { - return $this->crypto->decrypt($value); - } catch (\Exception $e) { - return $value; - } - } -} diff --git a/apps/files_external/service/globallegacystoragesservice.php b/apps/files_external/service/globallegacystoragesservice.php deleted file mode 100644 index ed9b1bcf075..00000000000 --- a/apps/files_external/service/globallegacystoragesservice.php +++ /dev/null @@ -1,44 +0,0 @@ - - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_external\Service; - -/** - * Read admin defined mounts from the legacy mount.json - */ -class GlobalLegacyStoragesService extends LegacyStoragesService { - /** - * @param BackendService $backendService - */ - public function __construct(BackendService $backendService) { - $this->backendService = $backendService; - } - - /** - * Read legacy config data - * - * @return array list of mount configs - */ - protected function readLegacyConfig() { - // read global config - return \OC_Mount_Config::readData(); - } -} diff --git a/apps/files_external/service/globalstoragesservice.php b/apps/files_external/service/globalstoragesservice.php deleted file mode 100644 index 62da314c5fa..00000000000 --- a/apps/files_external/service/globalstoragesservice.php +++ /dev/null @@ -1,165 +0,0 @@ - - * @author Robin Appelman - * @author Robin McCorkell - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_external\Service; - -use \OCP\IUserSession; -use \OC\Files\Filesystem; - -use \OCA\Files_external\Lib\StorageConfig; -use \OCA\Files_external\NotFoundException; - -/** - * Service class to manage global external storages - */ -class GlobalStoragesService extends StoragesService { - /** - * Triggers $signal for all applicable users of the given - * storage - * - * @param StorageConfig $storage storage data - * @param string $signal signal to trigger - */ - protected function triggerHooks(StorageConfig $storage, $signal) { - // FIXME: Use as expression in empty once PHP 5.4 support is dropped - $applicableUsers = $storage->getApplicableUsers(); - $applicableGroups = $storage->getApplicableGroups(); - if (empty($applicableUsers) && empty($applicableGroups)) { - // raise for user "all" - $this->triggerApplicableHooks( - $signal, - $storage->getMountPoint(), - \OC_Mount_Config::MOUNT_TYPE_USER, - ['all'] - ); - return; - } - - $this->triggerApplicableHooks( - $signal, - $storage->getMountPoint(), - \OC_Mount_Config::MOUNT_TYPE_USER, - $applicableUsers - ); - $this->triggerApplicableHooks( - $signal, - $storage->getMountPoint(), - \OC_Mount_Config::MOUNT_TYPE_GROUP, - $applicableGroups - ); - } - - /** - * Triggers signal_create_mount or signal_delete_mount to - * accommodate for additions/deletions in applicableUsers - * and applicableGroups fields. - * - * @param StorageConfig $oldStorage old storage config - * @param StorageConfig $newStorage new storage config - */ - protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage) { - // if mount point changed, it's like a deletion + creation - if ($oldStorage->getMountPoint() !== $newStorage->getMountPoint()) { - $this->triggerHooks($oldStorage, Filesystem::signal_delete_mount); - $this->triggerHooks($newStorage, Filesystem::signal_create_mount); - return; - } - - $userAdditions = array_diff($newStorage->getApplicableUsers(), $oldStorage->getApplicableUsers()); - $userDeletions = array_diff($oldStorage->getApplicableUsers(), $newStorage->getApplicableUsers()); - $groupAdditions = array_diff($newStorage->getApplicableGroups(), $oldStorage->getApplicableGroups()); - $groupDeletions = array_diff($oldStorage->getApplicableGroups(), $newStorage->getApplicableGroups()); - - // FIXME: Use as expression in empty once PHP 5.4 support is dropped - // if no applicable were set, raise a signal for "all" - $oldApplicableUsers = $oldStorage->getApplicableUsers(); - $oldApplicableGroups = $oldStorage->getApplicableGroups(); - if (empty($oldApplicableUsers) && empty($oldApplicableGroups)) { - $this->triggerApplicableHooks( - Filesystem::signal_delete_mount, - $oldStorage->getMountPoint(), - \OC_Mount_Config::MOUNT_TYPE_USER, - ['all'] - ); - } - - // trigger delete for removed users - $this->triggerApplicableHooks( - Filesystem::signal_delete_mount, - $oldStorage->getMountPoint(), - \OC_Mount_Config::MOUNT_TYPE_USER, - $userDeletions - ); - - // trigger delete for removed groups - $this->triggerApplicableHooks( - Filesystem::signal_delete_mount, - $oldStorage->getMountPoint(), - \OC_Mount_Config::MOUNT_TYPE_GROUP, - $groupDeletions - ); - - // and now add the new users - $this->triggerApplicableHooks( - Filesystem::signal_create_mount, - $newStorage->getMountPoint(), - \OC_Mount_Config::MOUNT_TYPE_USER, - $userAdditions - ); - - // and now add the new groups - $this->triggerApplicableHooks( - Filesystem::signal_create_mount, - $newStorage->getMountPoint(), - \OC_Mount_Config::MOUNT_TYPE_GROUP, - $groupAdditions - ); - - // FIXME: Use as expression in empty once PHP 5.4 support is dropped - // if no applicable, raise a signal for "all" - $newApplicableUsers = $newStorage->getApplicableUsers(); - $newApplicableGroups = $newStorage->getApplicableGroups(); - if (empty($newApplicableUsers) && empty($newApplicableGroups)) { - $this->triggerApplicableHooks( - Filesystem::signal_create_mount, - $newStorage->getMountPoint(), - \OC_Mount_Config::MOUNT_TYPE_USER, - ['all'] - ); - } - } - - /** - * Get the visibility type for this controller, used in validation - * - * @return string BackendService::VISIBILITY_* constants - */ - public function getVisibilityType() { - return BackendService::VISIBILITY_ADMIN; - } - - protected function isApplicable(StorageConfig $config) { - return true; - } -} diff --git a/apps/files_external/service/importlegacystoragesservice.php b/apps/files_external/service/importlegacystoragesservice.php deleted file mode 100644 index 8a790c5de9b..00000000000 --- a/apps/files_external/service/importlegacystoragesservice.php +++ /dev/null @@ -1,46 +0,0 @@ - - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_external\Service; - -class ImportLegacyStoragesService extends LegacyStoragesService { - private $data; - - /** - * @param BackendService $backendService - */ - public function __construct(BackendService $backendService) { - $this->backendService = $backendService; - } - - public function setData($data) { - $this->data = $data; - } - - /** - * Read legacy config data - * - * @return array list of mount configs - */ - protected function readLegacyConfig() { - return $this->data; - } -} diff --git a/apps/files_external/service/legacystoragesservice.php b/apps/files_external/service/legacystoragesservice.php deleted file mode 100644 index 158fc2b5137..00000000000 --- a/apps/files_external/service/legacystoragesservice.php +++ /dev/null @@ -1,209 +0,0 @@ - - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_external\Service; - -use \OCA\Files_external\Lib\StorageConfig; - -/** - * Read mount config from legacy mount.json - */ -abstract class LegacyStoragesService { - /** @var BackendService */ - protected $backendService; - - /** - * Read legacy config data - * - * @return array list of mount configs - */ - abstract protected function readLegacyConfig(); - - /** - * Copy legacy storage options into the given storage config object. - * - * @param StorageConfig $storageConfig storage config to populate - * @param string $mountType mount type - * @param string $applicable applicable user or group - * @param array $storageOptions legacy storage options - * - * @return StorageConfig populated storage config - */ - protected function populateStorageConfigWithLegacyOptions( - &$storageConfig, - $mountType, - $applicable, - $storageOptions - ) { - $backend = $this->backendService->getBackend($storageOptions['backend']); - if (!$backend) { - throw new \UnexpectedValueException('Invalid backend ' . $storageOptions['backend']); - } - $storageConfig->setBackend($backend); - if (isset($storageOptions['authMechanism']) && $storageOptions['authMechanism'] !== 'builtin::builtin') { - $authMechanism = $this->backendService->getAuthMechanism($storageOptions['authMechanism']); - } else { - $authMechanism = $backend->getLegacyAuthMechanism($storageOptions); - $storageOptions['authMechanism'] = 'null'; // to make error handling easier - } - if (!$authMechanism) { - throw new \UnexpectedValueException('Invalid authentication mechanism ' . $storageOptions['authMechanism']); - } - $storageConfig->setAuthMechanism($authMechanism); - $storageConfig->setBackendOptions($storageOptions['options']); - if (isset($storageOptions['mountOptions'])) { - $storageConfig->setMountOptions($storageOptions['mountOptions']); - } - if (!isset($storageOptions['priority'])) { - $storageOptions['priority'] = $backend->getPriority(); - } - $storageConfig->setPriority($storageOptions['priority']); - if ($mountType === \OC_Mount_Config::MOUNT_TYPE_USER) { - $applicableUsers = $storageConfig->getApplicableUsers(); - if ($applicable !== 'all') { - $applicableUsers[] = $applicable; - $storageConfig->setApplicableUsers($applicableUsers); - } - } else if ($mountType === \OC_Mount_Config::MOUNT_TYPE_GROUP) { - $applicableGroups = $storageConfig->getApplicableGroups(); - $applicableGroups[] = $applicable; - $storageConfig->setApplicableGroups($applicableGroups); - } - return $storageConfig; - } - - /** - * Read the external storages config - * - * @return StorageConfig[] map of storage id to storage config - */ - public function getAllStorages() { - $mountPoints = $this->readLegacyConfig(); - /** - * Here is the how the horribly messy mount point array looks like - * from the mount.json file: - * - * $storageOptions = $mountPoints[$mountType][$applicable][$mountPath] - * - * - $mountType is either "user" or "group" - * - $applicable is the name of a user or group (or the current user for personal mounts) - * - $mountPath is the mount point path (where the storage must be mounted) - * - $storageOptions is a map of storage options: - * - "priority": storage priority - * - "backend": backend identifier - * - "class": LEGACY backend class name - * - "options": backend-specific options - * - "authMechanism": authentication mechanism identifier - * - "mountOptions": mount-specific options (ex: disable previews, scanner, etc) - */ - // group by storage id - /** @var StorageConfig[] $storages */ - $storages = []; - // for storages without id (legacy), group by config hash for - // later processing - $storagesWithConfigHash = []; - foreach ($mountPoints as $mountType => $applicables) { - foreach ($applicables as $applicable => $mountPaths) { - foreach ($mountPaths as $rootMountPath => $storageOptions) { - $currentStorage = null; - /** - * Flag whether the config that was read already has an id. - * If not, it will use a config hash instead and generate - * a proper id later - * - * @var boolean - */ - $hasId = false; - // the root mount point is in the format "/$user/files/the/mount/point" - // we remove the "/$user/files" prefix - $parts = explode('/', ltrim($rootMountPath, '/'), 3); - if (count($parts) < 3) { - // something went wrong, skip - \OCP\Util::writeLog( - 'files_external', - 'Could not parse mount point "' . $rootMountPath . '"', - \OCP\Util::ERROR - ); - continue; - } - $relativeMountPath = rtrim($parts[2], '/'); - // note: we cannot do this after the loop because the decrypted config - // options might be needed for the config hash - $storageOptions['options'] = \OC_Mount_Config::decryptPasswords($storageOptions['options']); - if (!isset($storageOptions['backend'])) { - $storageOptions['backend'] = $storageOptions['class']; // legacy compat - } - if (!isset($storageOptions['authMechanism'])) { - $storageOptions['authMechanism'] = null; // ensure config hash works - } - if (isset($storageOptions['id'])) { - $configId = (int)$storageOptions['id']; - if (isset($storages[$configId])) { - $currentStorage = $storages[$configId]; - } - $hasId = true; - } else { - // missing id in legacy config, need to generate - // but at this point we don't know the max-id, so use - // first group it by config hash - $storageOptions['mountpoint'] = $rootMountPath; - $configId = \OC_Mount_Config::makeConfigHash($storageOptions); - if (isset($storagesWithConfigHash[$configId])) { - $currentStorage = $storagesWithConfigHash[$configId]; - } - } - if (is_null($currentStorage)) { - // create new - $currentStorage = new StorageConfig($configId); - $currentStorage->setMountPoint($relativeMountPath); - } - try { - $this->populateStorageConfigWithLegacyOptions( - $currentStorage, - $mountType, - $applicable, - $storageOptions - ); - if ($hasId) { - $storages[$configId] = $currentStorage; - } else { - $storagesWithConfigHash[$configId] = $currentStorage; - } - } catch (\UnexpectedValueException $e) { - // don't die if a storage backend doesn't exist - \OCP\Util::writeLog( - 'files_external', - 'Could not load storage: "' . $e->getMessage() . '"', - \OCP\Util::ERROR - ); - } - } - } - } - - // convert parameter values - foreach ($storages as $storage) { - $storage->getBackend()->validateStorageDefinition($storage); - $storage->getAuthMechanism()->validateStorageDefinition($storage); - } - return $storages; - } -} diff --git a/apps/files_external/service/storagesservice.php b/apps/files_external/service/storagesservice.php deleted file mode 100644 index 6542be146d6..00000000000 --- a/apps/files_external/service/storagesservice.php +++ /dev/null @@ -1,527 +0,0 @@ - - * @author Lukas Reschke - * @author Robin Appelman - * @author Robin McCorkell - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_external\Service; - -use \OC\Files\Filesystem; -use \OCA\Files_external\Lib\StorageConfig; -use \OCA\Files_external\NotFoundException; -use \OCA\Files_External\Lib\Backend\Backend; -use \OCA\Files_External\Lib\Auth\AuthMechanism; -use OCP\Files\Config\IUserMountCache; -use \OCP\Files\StorageNotAvailableException; - -/** - * Service class to manage external storages - */ -abstract class StoragesService { - - /** @var BackendService */ - protected $backendService; - - /** - * @var DBConfigService - */ - protected $dbConfig; - - /** - * @var IUserMountCache - */ - protected $userMountCache; - - /** - * @param BackendService $backendService - * @param DBConfigService $dbConfigService - * @param IUserMountCache $userMountCache - */ - public function __construct(BackendService $backendService, DBConfigService $dbConfigService, IUserMountCache $userMountCache) { - $this->backendService = $backendService; - $this->dbConfig = $dbConfigService; - $this->userMountCache = $userMountCache; - } - - protected function readDBConfig() { - return $this->dbConfig->getAdminMounts(); - } - - protected function getStorageConfigFromDBMount(array $mount) { - $applicableUsers = array_filter($mount['applicable'], function ($applicable) { - return $applicable['type'] === DBConfigService::APPLICABLE_TYPE_USER; - }); - $applicableUsers = array_map(function ($applicable) { - return $applicable['value']; - }, $applicableUsers); - - $applicableGroups = array_filter($mount['applicable'], function ($applicable) { - return $applicable['type'] === DBConfigService::APPLICABLE_TYPE_GROUP; - }); - $applicableGroups = array_map(function ($applicable) { - return $applicable['value']; - }, $applicableGroups); - - try { - $config = $this->createStorage( - $mount['mount_point'], - $mount['storage_backend'], - $mount['auth_backend'], - $mount['config'], - $mount['options'], - array_values($applicableUsers), - array_values($applicableGroups), - $mount['priority'] - ); - $config->setType($mount['type']); - $config->setId((int)$mount['mount_id']); - return $config; - } catch (\UnexpectedValueException $e) { - // don't die if a storage backend doesn't exist - \OCP\Util::writeLog( - 'files_external', - 'Could not load storage: "' . $e->getMessage() . '"', - \OCP\Util::ERROR - ); - return null; - } catch (\InvalidArgumentException $e) { - \OCP\Util::writeLog( - 'files_external', - 'Could not load storage: "' . $e->getMessage() . '"', - \OCP\Util::ERROR - ); - return null; - } - } - - /** - * Read the external storages config - * - * @return array map of storage id to storage config - */ - protected function readConfig() { - $mounts = $this->readDBConfig(); - $configs = array_map([$this, 'getStorageConfigFromDBMount'], $mounts); - $configs = array_filter($configs, function ($config) { - return $config instanceof StorageConfig; - }); - - $keys = array_map(function (StorageConfig $config) { - return $config->getId(); - }, $configs); - - return array_combine($keys, $configs); - } - - /** - * Get a storage with status - * - * @param int $id storage id - * - * @return StorageConfig - * @throws NotFoundException if the storage with the given id was not found - */ - public function getStorage($id) { - $mount = $this->dbConfig->getMountById($id); - - if (!is_array($mount)) { - throw new NotFoundException('Storage with id "' . $id . '" not found'); - } - - $config = $this->getStorageConfigFromDBMount($mount); - if ($this->isApplicable($config)) { - return $config; - } else { - throw new NotFoundException('Storage with id "' . $id . '" not found'); - } - } - - /** - * Check whether this storage service should provide access to a storage - * - * @param StorageConfig $config - * @return bool - */ - abstract protected function isApplicable(StorageConfig $config); - - /** - * Gets all storages, valid or not - * - * @return StorageConfig[] array of storage configs - */ - public function getAllStorages() { - return $this->readConfig(); - } - - /** - * Gets all valid storages - * - * @return StorageConfig[] - */ - public function getStorages() { - return array_filter($this->getAllStorages(), [$this, 'validateStorage']); - } - - /** - * Validate storage - * FIXME: De-duplicate with StoragesController::validate() - * - * @param StorageConfig $storage - * @return bool - */ - protected function validateStorage(StorageConfig $storage) { - /** @var Backend */ - $backend = $storage->getBackend(); - /** @var AuthMechanism */ - $authMechanism = $storage->getAuthMechanism(); - - if (!$backend->isVisibleFor($this->getVisibilityType())) { - // not permitted to use backend - return false; - } - if (!$authMechanism->isVisibleFor($this->getVisibilityType())) { - // not permitted to use auth mechanism - return false; - } - - return true; - } - - /** - * Get the visibility type for this controller, used in validation - * - * @return string BackendService::VISIBILITY_* constants - */ - abstract public function getVisibilityType(); - - /** - * @return integer - */ - protected function getType() { - return DBConfigService::MOUNT_TYPE_ADMIN; - } - - /** - * Add new storage to the configuration - * - * @param StorageConfig $newStorage storage attributes - * - * @return StorageConfig storage config, with added id - */ - public function addStorage(StorageConfig $newStorage) { - $allStorages = $this->readConfig(); - - $configId = $this->dbConfig->addMount( - $newStorage->getMountPoint(), - $newStorage->getBackend()->getIdentifier(), - $newStorage->getAuthMechanism()->getIdentifier(), - $newStorage->getPriority(), - $this->getType() - ); - - $newStorage->setId($configId); - - foreach ($newStorage->getApplicableUsers() as $user) { - $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_USER, $user); - } - foreach ($newStorage->getApplicableGroups() as $group) { - $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_GROUP, $group); - } - foreach ($newStorage->getBackendOptions() as $key => $value) { - $this->dbConfig->setConfig($configId, $key, $value); - } - foreach ($newStorage->getMountOptions() as $key => $value) { - $this->dbConfig->setOption($configId, $key, $value); - } - - if (count($newStorage->getApplicableUsers()) === 0 && count($newStorage->getApplicableGroups()) === 0) { - $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); - } - - // add new storage - $allStorages[$configId] = $newStorage; - - $this->triggerHooks($newStorage, Filesystem::signal_create_mount); - - $newStorage->setStatus(StorageNotAvailableException::STATUS_SUCCESS); - return $newStorage; - } - - /** - * Create a storage from its parameters - * - * @param string $mountPoint storage mount point - * @param string $backendIdentifier backend identifier - * @param string $authMechanismIdentifier authentication mechanism identifier - * @param array $backendOptions backend-specific options - * @param array|null $mountOptions mount-specific options - * @param array|null $applicableUsers users for which to mount the storage - * @param array|null $applicableGroups groups for which to mount the storage - * @param int|null $priority priority - * - * @return StorageConfig - */ - public function createStorage( - $mountPoint, - $backendIdentifier, - $authMechanismIdentifier, - $backendOptions, - $mountOptions = null, - $applicableUsers = null, - $applicableGroups = null, - $priority = null - ) { - $backend = $this->backendService->getBackend($backendIdentifier); - if (!$backend) { - throw new \InvalidArgumentException('Unable to get backend for ' . $backendIdentifier); - } - $authMechanism = $this->backendService->getAuthMechanism($authMechanismIdentifier); - if (!$authMechanism) { - throw new \InvalidArgumentException('Unable to get authentication mechanism for ' . $authMechanismIdentifier); - } - $newStorage = new StorageConfig(); - $newStorage->setMountPoint($mountPoint); - $newStorage->setBackend($backend); - $newStorage->setAuthMechanism($authMechanism); - $newStorage->setBackendOptions($backendOptions); - if (isset($mountOptions)) { - $newStorage->setMountOptions($mountOptions); - } - if (isset($applicableUsers)) { - $newStorage->setApplicableUsers($applicableUsers); - } - if (isset($applicableGroups)) { - $newStorage->setApplicableGroups($applicableGroups); - } - if (isset($priority)) { - $newStorage->setPriority($priority); - } - - return $newStorage; - } - - /** - * Triggers the given hook signal for all the applicables given - * - * @param string $signal signal - * @param string $mountPoint hook mount pount param - * @param string $mountType hook mount type param - * @param array $applicableArray array of applicable users/groups for which to trigger the hook - */ - protected function triggerApplicableHooks($signal, $mountPoint, $mountType, $applicableArray) { - foreach ($applicableArray as $applicable) { - \OCP\Util::emitHook( - Filesystem::CLASSNAME, - $signal, - [ - Filesystem::signal_param_path => $mountPoint, - Filesystem::signal_param_mount_type => $mountType, - Filesystem::signal_param_users => $applicable, - ] - ); - } - } - - /** - * Triggers $signal for all applicable users of the given - * storage - * - * @param StorageConfig $storage storage data - * @param string $signal signal to trigger - */ - abstract protected function triggerHooks(StorageConfig $storage, $signal); - - /** - * Triggers signal_create_mount or signal_delete_mount to - * accommodate for additions/deletions in applicableUsers - * and applicableGroups fields. - * - * @param StorageConfig $oldStorage old storage data - * @param StorageConfig $newStorage new storage data - */ - abstract protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage); - - /** - * Update storage to the configuration - * - * @param StorageConfig $updatedStorage storage attributes - * - * @return StorageConfig storage config - * @throws NotFoundException if the given storage does not exist in the config - */ - public function updateStorage(StorageConfig $updatedStorage) { - $id = $updatedStorage->getId(); - - $existingMount = $this->dbConfig->getMountById($id); - - if (!is_array($existingMount)) { - throw new NotFoundException('Storage with id "' . $id . '" not found while updating storage'); - } - - $oldStorage = $this->getStorageConfigFromDBMount($existingMount); - - $removedUsers = array_diff($oldStorage->getApplicableUsers(), $updatedStorage->getApplicableUsers()); - $removedGroups = array_diff($oldStorage->getApplicableGroups(), $updatedStorage->getApplicableGroups()); - $addedUsers = array_diff($updatedStorage->getApplicableUsers(), $oldStorage->getApplicableUsers()); - $addedGroups = array_diff($updatedStorage->getApplicableGroups(), $oldStorage->getApplicableGroups()); - - $oldUserCount = count($oldStorage->getApplicableUsers()); - $oldGroupCount = count($oldStorage->getApplicableGroups()); - $newUserCount = count($updatedStorage->getApplicableUsers()); - $newGroupCount = count($updatedStorage->getApplicableGroups()); - $wasGlobal = ($oldUserCount + $oldGroupCount) === 0; - $isGlobal = ($newUserCount + $newGroupCount) === 0; - - foreach ($removedUsers as $user) { - $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, $user); - } - foreach ($removedGroups as $group) { - $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, $group); - } - foreach ($addedUsers as $user) { - $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, $user); - } - foreach ($addedGroups as $group) { - $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, $group); - } - - if ($wasGlobal && !$isGlobal) { - $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); - } else if (!$wasGlobal && $isGlobal) { - $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); - } - - $changedConfig = array_diff_assoc($updatedStorage->getBackendOptions(), $oldStorage->getBackendOptions()); - $changedOptions = array_diff_assoc($updatedStorage->getMountOptions(), $oldStorage->getMountOptions()); - - foreach ($changedConfig as $key => $value) { - $this->dbConfig->setConfig($id, $key, $value); - } - foreach ($changedOptions as $key => $value) { - $this->dbConfig->setOption($id, $key, $value); - } - - if ($updatedStorage->getMountPoint() !== $oldStorage->getMountPoint()) { - $this->dbConfig->setMountPoint($id, $updatedStorage->getMountPoint()); - } - - if ($updatedStorage->getAuthMechanism()->getIdentifier() !== $oldStorage->getAuthMechanism()->getIdentifier()) { - $this->dbConfig->setAuthBackend($id, $updatedStorage->getAuthMechanism()->getIdentifier()); - } - - $this->triggerChangeHooks($oldStorage, $updatedStorage); - - if (($wasGlobal && !$isGlobal) || count($removedGroups) > 0) { // to expensive to properly handle these on the fly - $this->userMountCache->remoteStorageMounts($this->getStorageId($updatedStorage)); - } else { - $storageId = $this->getStorageId($updatedStorage); - foreach ($removedUsers as $userId) { - $this->userMountCache->removeUserStorageMount($storageId, $userId); - } - } - - return $this->getStorage($id); - } - - /** - * Delete the storage with the given id. - * - * @param int $id storage id - * - * @throws NotFoundException if no storage was found with the given id - */ - public function removeStorage($id) { - $existingMount = $this->dbConfig->getMountById($id); - - if (!is_array($existingMount)) { - throw new NotFoundException('Storage with id "' . $id . '" not found'); - } - - $this->dbConfig->removeMount($id); - - $deletedStorage = $this->getStorageConfigFromDBMount($existingMount); - $this->triggerHooks($deletedStorage, Filesystem::signal_delete_mount); - - // delete oc_storages entries and oc_filecache - try { - $rustyStorageId = $this->getRustyStorageIdFromConfig($deletedStorage); - \OC\Files\Cache\Storage::remove($rustyStorageId); - } catch (\Exception $e) { - // can happen either for invalid configs where the storage could not - // be instantiated or whenever $user vars where used, in which case - // the storage id could not be computed - \OCP\Util::writeLog( - 'files_external', - 'Exception: "' . $e->getMessage() . '"', - \OCP\Util::ERROR - ); - } - } - - /** - * Returns the rusty storage id from oc_storages from the given storage config. - * - * @param StorageConfig $storageConfig - * @return string rusty storage id - */ - private function getRustyStorageIdFromConfig(StorageConfig $storageConfig) { - // if any of the storage options contains $user, it is not possible - // to compute the possible storage id as we don't know which users - // mounted it already (and we certainly don't want to iterate over ALL users) - foreach ($storageConfig->getBackendOptions() as $value) { - if (strpos($value, '$user') !== false) { - throw new \Exception('Cannot compute storage id for deletion due to $user vars in the configuration'); - } - } - - // note: similar to ConfigAdapter->prepateStorageConfig() - $storageConfig->getAuthMechanism()->manipulateStorageConfig($storageConfig); - $storageConfig->getBackend()->manipulateStorageConfig($storageConfig); - - $class = $storageConfig->getBackend()->getStorageClass(); - $storageImpl = new $class($storageConfig->getBackendOptions()); - - return $storageImpl->getId(); - } - - /** - * Construct the storage implementation - * - * @param StorageConfig $storageConfig - * @return int - */ - private function getStorageId(StorageConfig $storageConfig) { - try { - $class = $storageConfig->getBackend()->getStorageClass(); - /** @var \OC\Files\Storage\Storage $storage */ - $storage = new $class($storageConfig->getBackendOptions()); - - // auth mechanism should fire first - $storage = $storageConfig->getBackend()->wrapStorage($storage); - $storage = $storageConfig->getAuthMechanism()->wrapStorage($storage); - - return $storage->getStorageCache()->getNumericId(); - } catch (\Exception $e) { - return -1; - } - } -} diff --git a/apps/files_external/service/userglobalstoragesservice.php b/apps/files_external/service/userglobalstoragesservice.php deleted file mode 100644 index 111d4586924..00000000000 --- a/apps/files_external/service/userglobalstoragesservice.php +++ /dev/null @@ -1,177 +0,0 @@ - - * @author Robin McCorkell - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_External\Service; - -use \OCA\Files_external\Service\GlobalStoragesService; -use \OCA\Files_External\Service\BackendService; -use OCP\Files\Config\IUserMountCache; -use \OCP\IUserSession; -use \OCP\IGroupManager; -use \OCA\Files_External\Service\UserTrait; -use \OCA\Files_External\Lib\StorageConfig; - -/** - * Service class to read global storages applicable to the user - * Read-only access available, attempting to write will throw DomainException - */ -class UserGlobalStoragesService extends GlobalStoragesService { - - use UserTrait; - - /** @var IGroupManager */ - protected $groupManager; - - /** - * @param BackendService $backendService - * @param DBConfigService $dbConfig - * @param IUserSession $userSession - * @param IGroupManager $groupManager - * @param IUserMountCache $userMountCache - */ - public function __construct( - BackendService $backendService, - DBConfigService $dbConfig, - IUserSession $userSession, - IGroupManager $groupManager, - IUserMountCache $userMountCache - ) { - parent::__construct($backendService, $dbConfig, $userMountCache); - $this->userSession = $userSession; - $this->groupManager = $groupManager; - } - - /** - * Replace config hash ID with real IDs, for migrating legacy storages - * - * @param StorageConfig[] $storages Storages with real IDs - * @param StorageConfig[] $storagesWithConfigHash Storages with config hash IDs - */ - protected function setRealStorageIds(array &$storages, array $storagesWithConfigHash) { - // as a read-only view, storage IDs don't need to be real - foreach ($storagesWithConfigHash as $storage) { - $storages[$storage->getId()] = $storage; - } - } - - protected function readDBConfig() { - $userMounts = $this->dbConfig->getAdminMountsFor(DBConfigService::APPLICABLE_TYPE_USER, $this->getUser()->getUID()); - $globalMounts = $this->dbConfig->getAdminMountsFor(DBConfigService::APPLICABLE_TYPE_GLOBAL, null); - $groups = $this->groupManager->getUserGroupIds($this->getUser()); - if (is_array($groups) && count($groups) !== 0) { - $groupMounts = $this->dbConfig->getAdminMountsForMultiple(DBConfigService::APPLICABLE_TYPE_GROUP, $groups); - } else { - $groupMounts = []; - } - return array_merge($userMounts, $groupMounts, $globalMounts); - } - - public function addStorage(StorageConfig $newStorage) { - throw new \DomainException('UserGlobalStoragesService writing disallowed'); - } - - public function updateStorage(StorageConfig $updatedStorage) { - throw new \DomainException('UserGlobalStoragesService writing disallowed'); - } - - /** - * @param integer $id - */ - public function removeStorage($id) { - throw new \DomainException('UserGlobalStoragesService writing disallowed'); - } - - /** - * Get unique storages, in case two are defined with the same mountpoint - * Higher priority storages take precedence - * - * @return StorageConfig[] - */ - public function getUniqueStorages() { - $storages = $this->getStorages(); - - $storagesByMountpoint = []; - foreach ($storages as $storage) { - $storagesByMountpoint[$storage->getMountPoint()][] = $storage; - } - - $result = []; - foreach ($storagesByMountpoint as $storageList) { - $storage = array_reduce($storageList, function ($carry, $item) { - if (isset($carry)) { - $carryPriorityType = $this->getPriorityType($carry); - $itemPriorityType = $this->getPriorityType($item); - if ($carryPriorityType > $itemPriorityType) { - return $carry; - } elseif ($carryPriorityType === $itemPriorityType) { - if ($carry->getPriority() > $item->getPriority()) { - return $carry; - } - } - } - return $item; - }); - $result[$storage->getID()] = $storage; - } - - return $result; - } - - /** - * Get a priority 'type', where a bigger number means higher priority - * user applicable > group applicable > 'all' - * - * @param StorageConfig $storage - * @return int - */ - protected function getPriorityType(StorageConfig $storage) { - $applicableUsers = $storage->getApplicableUsers(); - $applicableGroups = $storage->getApplicableGroups(); - - if ($applicableUsers && $applicableUsers[0] !== 'all') { - return 2; - } - if ($applicableGroups) { - return 1; - } - return 0; - } - - protected function isApplicable(StorageConfig $config) { - $applicableUsers = $config->getApplicableUsers(); - $applicableGroups = $config->getApplicableGroups(); - - if (count($applicableUsers) === 0 && count($applicableGroups) === 0) { - return true; - } - if (in_array($this->getUser()->getUID(), $applicableUsers, true)) { - return true; - } - $groupIds = $this->groupManager->getUserGroupIds($this->getUser()); - foreach ($groupIds as $groupId) { - if (in_array($groupId, $applicableGroups, true)) { - return true; - } - } - return false; - } -} diff --git a/apps/files_external/service/userlegacystoragesservice.php b/apps/files_external/service/userlegacystoragesservice.php deleted file mode 100644 index 2f14857c633..00000000000 --- a/apps/files_external/service/userlegacystoragesservice.php +++ /dev/null @@ -1,54 +0,0 @@ - - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_external\Service; - -use OCP\IUserSession; - -/** - * Read user defined mounts from the legacy mount.json - */ -class UserLegacyStoragesService extends LegacyStoragesService { - /** - * @var IUserSession - */ - private $userSession; - - /** - * @param BackendService $backendService - * @param IUserSession $userSession - */ - public function __construct(BackendService $backendService, IUserSession $userSession) { - $this->backendService = $backendService; - $this->userSession = $userSession; - } - - /** - * Read legacy config data - * - * @return array list of storage configs - */ - protected function readLegacyConfig() { - // read user config - $user = $this->userSession->getUser()->getUID(); - return \OC_Mount_Config::readData($user); - } -} diff --git a/apps/files_external/service/userstoragesservice.php b/apps/files_external/service/userstoragesservice.php deleted file mode 100644 index c46846e76d6..00000000000 --- a/apps/files_external/service/userstoragesservice.php +++ /dev/null @@ -1,141 +0,0 @@ - - * @author Robin McCorkell - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_external\Service; - -use OCP\Files\Config\IUserMountCache; -use \OCP\IUserSession; -use \OC\Files\Filesystem; - -use \OCA\Files_external\Lib\StorageConfig; -use \OCA\Files_external\NotFoundException; -use \OCA\Files_External\Service\BackendService; -use \OCA\Files_External\Service\UserTrait; - -/** - * Service class to manage user external storages - * (aka personal storages) - */ -class UserStoragesService extends StoragesService { - use UserTrait; - - /** - * Create a user storages service - * - * @param BackendService $backendService - * @param DBConfigService $dbConfig - * @param IUserSession $userSession user session - * @param IUserMountCache $userMountCache - */ - public function __construct( - BackendService $backendService, - DBConfigService $dbConfig, - IUserSession $userSession, - IUserMountCache $userMountCache - ) { - $this->userSession = $userSession; - parent::__construct($backendService, $dbConfig, $userMountCache); - } - - protected function readDBConfig() { - return $this->dbConfig->getUserMountsFor(DBConfigService::APPLICABLE_TYPE_USER, $this->getUser()->getUID()); - } - - /** - * Triggers $signal for all applicable users of the given - * storage - * - * @param StorageConfig $storage storage data - * @param string $signal signal to trigger - */ - protected function triggerHooks(StorageConfig $storage, $signal) { - $user = $this->getUser()->getUID(); - - // trigger hook for the current user - $this->triggerApplicableHooks( - $signal, - $storage->getMountPoint(), - \OC_Mount_Config::MOUNT_TYPE_USER, - [$user] - ); - } - - /** - * Triggers signal_create_mount or signal_delete_mount to - * accommodate for additions/deletions in applicableUsers - * and applicableGroups fields. - * - * @param StorageConfig $oldStorage old storage data - * @param StorageConfig $newStorage new storage data - */ - protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage) { - // if mount point changed, it's like a deletion + creation - if ($oldStorage->getMountPoint() !== $newStorage->getMountPoint()) { - $this->triggerHooks($oldStorage, Filesystem::signal_delete_mount); - $this->triggerHooks($newStorage, Filesystem::signal_create_mount); - } - } - - protected function getType() { - return DBConfigService::MOUNT_TYPE_PERSONAl; - } - - /** - * Add new storage to the configuration - * - * @param StorageConfig $newStorage storage attributes - * - * @return StorageConfig storage config, with added id - */ - public function addStorage(StorageConfig $newStorage) { - $newStorage->setApplicableUsers([$this->getUser()->getUID()]); - $config = parent::addStorage($newStorage); - return $config; - } - - /** - * Update storage to the configuration - * - * @param StorageConfig $updatedStorage storage attributes - * - * @return StorageConfig storage config - * @throws NotFoundException if the given storage does not exist in the config - */ - public function updateStorage(StorageConfig $updatedStorage) { - $updatedStorage->setApplicableUsers([$this->getUser()->getUID()]); - return parent::updateStorage($updatedStorage); - } - - /** - * Get the visibility type for this controller, used in validation - * - * @return string BackendService::VISIBILITY_* constants - */ - public function getVisibilityType() { - return BackendService::VISIBILITY_PERSONAL; - } - - protected function isApplicable(StorageConfig $config) { - return ($config->getApplicableUsers() === [$this->getUser()->getUID()]) && $config->getType() === StorageConfig::MOUNT_TYPE_PERSONAl; - } -} diff --git a/apps/files_external/service/usertrait.php b/apps/files_external/service/usertrait.php deleted file mode 100644 index 536c0f67e1f..00000000000 --- a/apps/files_external/service/usertrait.php +++ /dev/null @@ -1,74 +0,0 @@ - - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_External\Service; - -use \OCP\IUserSession; -use \OCP\IUser; - -/** - * Trait for getting user information in a service - */ -trait UserTrait { - - /** @var IUserSession */ - protected $userSession; - - /** - * User override - * - * @var IUser|null - */ - private $user = null; - - /** - * @return IUser|null - */ - protected function getUser() { - if ($this->user) { - return $this->user; - } - return $this->userSession->getUser(); - } - - /** - * Override the user from the session - * Unset with ->resetUser() when finished! - * - * @param IUser - * @return self - */ - public function setUser(IUser $user) { - $this->user = $user; - return $this; - } - - /** - * Reset the user override - * - * @return self - */ - public function resetUser() { - $this->user = null; - return $this; - } -} - diff --git a/apps/files_external/settings.php b/apps/files_external/settings.php index 0d83d26ff97..8f44c170ce6 100644 --- a/apps/files_external/settings.php +++ b/apps/files_external/settings.php @@ -31,7 +31,7 @@ use \OCA\Files_External\Service\BackendService; // we must use the same container $appContainer = \OC_Mount_Config::$app->getContainer(); $backendService = $appContainer->query('OCA\Files_External\Service\BackendService'); -$globalStoragesService = $appContainer->query('OCA\Files_external\Service\GlobalStoragesService'); +$globalStoragesService = $appContainer->query('OCA\Files_External\Service\GlobalStoragesService'); \OC_Util::addVendorScript('select2/select2'); \OC_Util::addVendorStyle('select2/select2'); diff --git a/apps/files_external/tests/Command/ApplicableTest.php b/apps/files_external/tests/Command/ApplicableTest.php new file mode 100644 index 00000000000..64d41f6f245 --- /dev/null +++ b/apps/files_external/tests/Command/ApplicableTest.php @@ -0,0 +1,168 @@ + + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Tests\Command; + +use OCA\Files_External\Command\Applicable; + +class ApplicableTest extends CommandTest { + private function getInstance($storageService) { + /** @var \OCP\IUserManager|\PHPUnit_Framework_MockObject_MockObject $userManager */ + $userManager = $this->getMock('\OCP\IUserManager'); + /** @var \OCP\IGroupManager|\PHPUnit_Framework_MockObject_MockObject $groupManager */ + $groupManager = $this->getMock('\OCP\IGroupManager'); + + $userManager->expects($this->any()) + ->method('userExists') + ->will($this->returnValue(true)); + + $groupManager->expects($this->any()) + ->method('groupExists') + ->will($this->returnValue(true)); + + return new Applicable($storageService, $userManager, $groupManager); + } + + public function testListEmpty() { + $mount = $this->getMount(1, '', ''); + + $storageService = $this->getGlobalStorageService([$mount]); + $command = $this->getInstance($storageService); + + $input = $this->getInput($command, [ + 'mount_id' => 1 + ], [ + 'output' => 'json' + ]); + + $result = json_decode($this->executeCommand($command, $input), true); + + $this->assertEquals(['users' => [], 'groups' => []], $result); + } + + public function testList() { + $mount = $this->getMount(1, '', '', '', [], [], ['test', 'asd']); + + $storageService = $this->getGlobalStorageService([$mount]); + $command = $this->getInstance($storageService); + + $input = $this->getInput($command, [ + 'mount_id' => 1 + ], [ + 'output' => 'json' + ]); + + $result = json_decode($this->executeCommand($command, $input), true); + + $this->assertEquals(['users' => ['test', 'asd'], 'groups' => []], $result); + } + + public function testAddSingle() { + $mount = $this->getMount(1, '', '', '', [], [], []); + + $storageService = $this->getGlobalStorageService([$mount]); + $command = $this->getInstance($storageService); + + $input = $this->getInput($command, [ + 'mount_id' => 1 + ], [ + 'output' => 'json', + 'add-user' => ['foo'] + ]); + + $this->executeCommand($command, $input); + + $this->assertEquals(['foo'], $mount->getApplicableUsers()); + } + + public function testAddDuplicate() { + $mount = $this->getMount(1, '', '', '', [], [], ['foo']); + + $storageService = $this->getGlobalStorageService([$mount]); + $command = $this->getInstance($storageService); + + $input = $this->getInput($command, [ + 'mount_id' => 1 + ], [ + 'output' => 'json', + 'add-user' => ['foo', 'bar'] + ]); + + $this->executeCommand($command, $input); + + $this->assertEquals(['foo', 'bar'], $mount->getApplicableUsers()); + } + + public function testRemoveSingle() { + $mount = $this->getMount(1, '', '', '', [], [], ['foo', 'bar']); + + $storageService = $this->getGlobalStorageService([$mount]); + $command = $this->getInstance($storageService); + + $input = $this->getInput($command, [ + 'mount_id' => 1 + ], [ + 'output' => 'json', + 'remove-user' => ['bar'] + ]); + + $this->executeCommand($command, $input); + + $this->assertEquals(['foo'], $mount->getApplicableUsers()); + } + + public function testRemoveNonExisting() { + $mount = $this->getMount(1, '', '', '', [], [], ['foo', 'bar']); + + $storageService = $this->getGlobalStorageService([$mount]); + $command = $this->getInstance($storageService); + + $input = $this->getInput($command, [ + 'mount_id' => 1 + ], [ + 'output' => 'json', + 'remove-user' => ['bar', 'asd'] + ]); + + $this->executeCommand($command, $input); + + $this->assertEquals(['foo'], $mount->getApplicableUsers()); + } + + public function testRemoveAddRemove() { + $mount = $this->getMount(1, '', '', '', [], [], ['foo', 'bar']); + + $storageService = $this->getGlobalStorageService([$mount]); + $command = $this->getInstance($storageService); + + $input = $this->getInput($command, [ + 'mount_id' => 1 + ], [ + 'output' => 'json', + 'remove-user' => ['bar', 'asd'], + 'add-user' => ['test'] + ]); + + $this->executeCommand($command, $input); + + $this->assertEquals(['foo', 'test'], $mount->getApplicableUsers()); + } +} diff --git a/apps/files_external/tests/Command/CommandTest.php b/apps/files_external/tests/Command/CommandTest.php new file mode 100644 index 00000000000..73a1ea8315c --- /dev/null +++ b/apps/files_external/tests/Command/CommandTest.php @@ -0,0 +1,104 @@ + + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Tests\Command; + +use OCA\Files_external\Lib\StorageConfig; +use OCA\Files_external\NotFoundException; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Input\Input; +use Symfony\Component\Console\Output\BufferedOutput; +use Test\TestCase; + +abstract class CommandTest extends TestCase { + /** + * @param StorageConfig[] $mounts + * @return \OCA\Files_External\Service\GlobalStoragesService|\PHPUnit_Framework_MockObject_MockObject + */ + protected function getGlobalStorageService(array $mounts = []) { + $mock = $this->getMockBuilder('OCA\Files_External\Service\GlobalStoragesService') + ->disableOriginalConstructor() + ->getMock(); + + $this->bindMounts($mock, $mounts); + + return $mock; + } + + /** + * @param \PHPUnit_Framework_MockObject_MockObject $mock + * @param StorageConfig[] $mounts + */ + protected function bindMounts(\PHPUnit_Framework_MockObject_MockObject $mock, array $mounts) { + $mock->expects($this->any()) + ->method('getStorage') + ->will($this->returnCallback(function ($id) use ($mounts) { + foreach ($mounts as $mount) { + if ($mount->getId() === $id) { + return $mount; + } + } + throw new NotFoundException(); + })); + } + + /** + * @param $id + * @param $mountPoint + * @param $backendClass + * @param string $applicableIdentifier + * @param array $config + * @param array $options + * @param array $users + * @param array $groups + * @return StorageConfig + */ + protected function getMount($id, $mountPoint, $backendClass, $applicableIdentifier = 'password::password', $config = [], $options = [], $users = [], $groups = []) { + $mount = new StorageConfig($id); + + $mount->setMountPoint($mountPoint); + $mount->setBackendOptions($config); + $mount->setMountOptions($options); + $mount->setApplicableUsers($users); + $mount->setApplicableGroups($groups); + + return $mount; + } + + protected function getInput(Command $command, array $arguments = [], array $options = []) { + $input = new ArrayInput([]); + $input->bind($command->getDefinition()); + foreach ($arguments as $key => $value) { + $input->setArgument($key, $value); + } + foreach ($options as $key => $value) { + $input->setOption($key, $value); + } + return $input; + } + + protected function executeCommand(Command $command, Input $input) { + $output = new BufferedOutput(); + $this->invokePrivate($command, 'execute', [$input, $output]); + return $output->fetch(); + } +} diff --git a/apps/files_external/tests/Command/ListCommandTest.php b/apps/files_external/tests/Command/ListCommandTest.php new file mode 100644 index 00000000000..1475249ae43 --- /dev/null +++ b/apps/files_external/tests/Command/ListCommandTest.php @@ -0,0 +1,70 @@ + + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Tests\Command; + +use OCA\Files_External\Command\ListCommand; +use OCA\Files_External\Lib\Auth\NullMechanism; +use OCA\Files_External\Lib\Auth\Password\Password; +use OCA\Files_External\Lib\Auth\Password\SessionCredentials; +use OCA\Files_External\Lib\Backend\Local; +use OCA\Files_external\Lib\StorageConfig; +use Symfony\Component\Console\Output\BufferedOutput; + +class ListCommandTest extends CommandTest { + /** + * @return \OCA\Files_External\Command\ListCommand|\PHPUnit_Framework_MockObject_MockObject + */ + private function getInstance() { + /** @var \OCA\Files_External\Service\GlobalStoragesService|\PHPUnit_Framework_MockObject_MockObject $globalService */ + $globalService = $this->getMock('\OCA\Files_External\Service\GlobalStoragesService', null, [], '', false); + /** @var \OCA\Files_External\Service\UserStoragesService|\PHPUnit_Framework_MockObject_MockObject $userService */ + $userService = $this->getMock('\OCA\Files_External\Service\UserStoragesService', null, [], '', false); + /** @var \OCP\IUserManager|\PHPUnit_Framework_MockObject_MockObject $userManager */ + $userManager = $this->getMock('\OCP\IUserManager'); + /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject $userSession */ + $userSession = $this->getMock('\OCP\IUserSession'); + + return new ListCommand($globalService, $userService, $userSession, $userManager); + } + + public function testListAuthIdentifier() { + $l10n = $this->getMock('\OC_L10N', null, [], '', false); + $session = $this->getMock('\OCP\ISession'); + $crypto = $this->getMock('\OCP\Security\ICrypto'); + $instance = $this->getInstance(); + $mount1 = new StorageConfig(); + $mount1->setAuthMechanism(new Password($l10n)); + $mount1->setBackend(new Local($l10n, new NullMechanism($l10n))); + $mount2 = new StorageConfig(); + $mount2->setAuthMechanism(new SessionCredentials($l10n, $session, $crypto)); + $mount2->setBackend(new Local($l10n, new NullMechanism($l10n))); + $input = $this->getInput($instance, [], [ + 'output' => 'json' + ]); + $output = new BufferedOutput(); + + $instance->listMounts('', [$mount1, $mount2], $input, $output); + $output = json_decode($output->fetch(), true); + + $this->assertNotEquals($output[0]['authentication_type'], $output[1]['authentication_type']); + } +} diff --git a/apps/files_external/tests/Controller/GlobalStoragesControllerTest.php b/apps/files_external/tests/Controller/GlobalStoragesControllerTest.php new file mode 100644 index 00000000000..9761a4e23f9 --- /dev/null +++ b/apps/files_external/tests/Controller/GlobalStoragesControllerTest.php @@ -0,0 +1,47 @@ + + * @author Robin McCorkell + * @author Vincent Petry + * + * @copyright Copyright (c) 2016, 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 + * + */ +namespace OCA\Files_External\Tests\Controller; + +use OCA\Files_External\Controller\GlobalStoragesController; +use \OCP\AppFramework\Http; +use \OCA\Files_External\Service\BackendService; + +class GlobalStoragesControllerTest extends StoragesControllerTest { + public function setUp() { + parent::setUp(); + $this->service = $this->getMockBuilder('\OCA\Files_External\Service\GlobalStoragesService') + ->disableOriginalConstructor() + ->getMock(); + + $this->service->method('getVisibilityType') + ->willReturn(BackendService::VISIBILITY_ADMIN); + + $this->controller = new GlobalStoragesController( + 'files_external', + $this->getMock('\OCP\IRequest'), + $this->getMock('\OCP\IL10N'), + $this->service, + $this->getMock('\OCP\ILogger') + ); + } +} diff --git a/apps/files_external/tests/Controller/StoragesControllerTest.php b/apps/files_external/tests/Controller/StoragesControllerTest.php new file mode 100644 index 00000000000..81625fbbcb9 --- /dev/null +++ b/apps/files_external/tests/Controller/StoragesControllerTest.php @@ -0,0 +1,394 @@ + + * @author Vincent Petry + * + * @copyright Copyright (c) 2016, 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 + * + */ +namespace OCA\Files_External\Tests\Controller; + +use \OCP\AppFramework\Http; + +use \OCA\Files_External\Controller\GlobalStoragesController; +use \OCA\Files_External\Service\GlobalStoragesService; +use \OCA\Files_external\Lib\StorageConfig; +use \OCA\Files_external\NotFoundException; + +abstract class StoragesControllerTest extends \Test\TestCase { + + /** + * @var GlobalStoragesController + */ + protected $controller; + + /** + * @var GlobalStoragesService + */ + protected $service; + + public function setUp() { + \OC_Mount_Config::$skipTest = true; + } + + public function tearDown() { + \OC_Mount_Config::$skipTest = false; + } + + /** + * @return \OCA\Files_External\Lib\Backend\Backend + */ + protected function getBackendMock($class = '\OCA\Files_External\Lib\Backend\SMB', $storageClass = '\OCA\Files_External\Lib\Storage\SMB') { + $backend = $this->getMockBuilder('\OCA\Files_External\Lib\Backend\Backend') + ->disableOriginalConstructor() + ->getMock(); + $backend->method('getStorageClass') + ->willReturn($storageClass); + $backend->method('getIdentifier') + ->willReturn('identifier:'.$class); + return $backend; + } + + /** + * @return \OCA\Files_External\Lib\Auth\AuthMechanism + */ + protected function getAuthMechMock($scheme = 'null', $class = '\OCA\Files_External\Lib\Auth\NullMechanism') { + $authMech = $this->getMockBuilder('\OCA\Files_External\Lib\Auth\AuthMechanism') + ->disableOriginalConstructor() + ->getMock(); + $authMech->method('getScheme') + ->willReturn($scheme); + $authMech->method('getIdentifier') + ->willReturn('identifier:'.$class); + + return $authMech; + } + + public function testAddStorage() { + $authMech = $this->getAuthMechMock(); + $authMech->method('validateStorage') + ->willReturn(true); + $authMech->method('isVisibleFor') + ->willReturn(true); + $backend = $this->getBackendMock(); + $backend->method('validateStorage') + ->willReturn(true); + $backend->method('isVisibleFor') + ->willReturn(true); + + $storageConfig = new StorageConfig(1); + $storageConfig->setMountPoint('mount'); + $storageConfig->setBackend($backend); + $storageConfig->setAuthMechanism($authMech); + $storageConfig->setBackendOptions([]); + + $this->service->expects($this->once()) + ->method('createStorage') + ->will($this->returnValue($storageConfig)); + $this->service->expects($this->once()) + ->method('addStorage') + ->will($this->returnValue($storageConfig)); + + $response = $this->controller->create( + 'mount', + '\OCA\Files_External\Lib\Storage\SMB', + '\OCA\Files_External\Lib\Auth\NullMechanism', + array(), + [], + [], + [], + null + ); + + $data = $response->getData(); + $this->assertEquals(Http::STATUS_CREATED, $response->getStatus()); + $this->assertEquals($storageConfig, $data); + } + + public function testUpdateStorage() { + $authMech = $this->getAuthMechMock(); + $authMech->method('validateStorage') + ->willReturn(true); + $authMech->method('isVisibleFor') + ->willReturn(true); + $backend = $this->getBackendMock(); + $backend->method('validateStorage') + ->willReturn(true); + $backend->method('isVisibleFor') + ->willReturn(true); + + $storageConfig = new StorageConfig(1); + $storageConfig->setMountPoint('mount'); + $storageConfig->setBackend($backend); + $storageConfig->setAuthMechanism($authMech); + $storageConfig->setBackendOptions([]); + + $this->service->expects($this->once()) + ->method('createStorage') + ->will($this->returnValue($storageConfig)); + $this->service->expects($this->once()) + ->method('updateStorage') + ->will($this->returnValue($storageConfig)); + + $response = $this->controller->update( + 1, + 'mount', + '\OCA\Files_External\Lib\Storage\SMB', + '\OCA\Files_External\Lib\Auth\NullMechanism', + array(), + [], + [], + [], + null + ); + + $data = $response->getData(); + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + $this->assertEquals($storageConfig, $data); + } + + function mountPointNamesProvider() { + return array( + array(''), + array('/'), + array('//'), + ); + } + + /** + * @dataProvider mountPointNamesProvider + */ + public function testAddOrUpdateStorageInvalidMountPoint($mountPoint) { + $storageConfig = new StorageConfig(1); + $storageConfig->setMountPoint($mountPoint); + $storageConfig->setBackend($this->getBackendMock()); + $storageConfig->setAuthMechanism($this->getAuthMechMock()); + $storageConfig->setBackendOptions([]); + + $this->service->expects($this->exactly(2)) + ->method('createStorage') + ->will($this->returnValue($storageConfig)); + $this->service->expects($this->never()) + ->method('addStorage'); + $this->service->expects($this->never()) + ->method('updateStorage'); + + $response = $this->controller->create( + $mountPoint, + '\OCA\Files_External\Lib\Storage\SMB', + '\OCA\Files_External\Lib\Auth\NullMechanism', + array(), + [], + [], + [], + null + ); + + $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus()); + + $response = $this->controller->update( + 1, + $mountPoint, + '\OCA\Files_External\Lib\Storage\SMB', + '\OCA\Files_External\Lib\Auth\NullMechanism', + array(), + [], + [], + [], + null + ); + + $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus()); + } + + public function testAddOrUpdateStorageInvalidBackend() { + $this->service->expects($this->exactly(2)) + ->method('createStorage') + ->will($this->throwException(new \InvalidArgumentException())); + $this->service->expects($this->never()) + ->method('addStorage'); + $this->service->expects($this->never()) + ->method('updateStorage'); + + $response = $this->controller->create( + 'mount', + '\OC\Files\Storage\InvalidStorage', + '\OCA\Files_External\Lib\Auth\NullMechanism', + array(), + [], + [], + [], + null + ); + + $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus()); + + $response = $this->controller->update( + 1, + 'mount', + '\OC\Files\Storage\InvalidStorage', + '\OCA\Files_External\Lib\Auth\NullMechanism', + array(), + [], + [], + [], + null + ); + + $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus()); + } + + public function testUpdateStorageNonExisting() { + $authMech = $this->getAuthMechMock(); + $authMech->method('validateStorage') + ->willReturn(true); + $authMech->method('isVisibleFor') + ->willReturn(true); + $backend = $this->getBackendMock(); + $backend->method('validateStorage') + ->willReturn(true); + $backend->method('isVisibleFor') + ->willReturn(true); + + $storageConfig = new StorageConfig(255); + $storageConfig->setMountPoint('mount'); + $storageConfig->setBackend($backend); + $storageConfig->setAuthMechanism($authMech); + $storageConfig->setBackendOptions([]); + + $this->service->expects($this->once()) + ->method('createStorage') + ->will($this->returnValue($storageConfig)); + $this->service->expects($this->once()) + ->method('updateStorage') + ->will($this->throwException(new NotFoundException())); + + $response = $this->controller->update( + 255, + 'mount', + '\OCA\Files_External\Lib\Storage\SMB', + '\OCA\Files_External\Lib\Auth\NullMechanism', + array(), + [], + [], + [], + null + ); + + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); + } + + public function testDeleteStorage() { + $this->service->expects($this->once()) + ->method('removeStorage'); + + $response = $this->controller->destroy(1); + $this->assertEquals(Http::STATUS_NO_CONTENT, $response->getStatus()); + } + + public function testDeleteStorageNonExisting() { + $this->service->expects($this->once()) + ->method('removeStorage') + ->will($this->throwException(new NotFoundException())); + + $response = $this->controller->destroy(255); + $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); + } + + public function testGetStorage() { + $backend = $this->getBackendMock(); + $authMech = $this->getAuthMechMock(); + $storageConfig = new StorageConfig(1); + $storageConfig->setMountPoint('test'); + $storageConfig->setBackend($backend); + $storageConfig->setAuthMechanism($authMech); + $storageConfig->setBackendOptions(['user' => 'test', 'password', 'password123']); + $storageConfig->setMountOptions(['priority' => false]); + + $this->service->expects($this->once()) + ->method('getStorage') + ->with(1) + ->will($this->returnValue($storageConfig)); + $response = $this->controller->show(1); + + $this->assertEquals(Http::STATUS_OK, $response->getStatus()); + $this->assertEquals($storageConfig, $response->getData()); + } + + public function validateStorageProvider() { + return [ + [true, true, true], + [false, true, false], + [true, false, false], + [false, false, false] + ]; + } + + /** + * @dataProvider validateStorageProvider + */ + public function testValidateStorage($backendValidate, $authMechValidate, $expectSuccess) { + $backend = $this->getBackendMock(); + $backend->method('validateStorage') + ->willReturn($backendValidate); + $backend->method('isVisibleFor') + ->willReturn(true); + + $authMech = $this->getAuthMechMock(); + $authMech->method('validateStorage') + ->will($this->returnValue($authMechValidate)); + $authMech->method('isVisibleFor') + ->willReturn(true); + + $storageConfig = new StorageConfig(); + $storageConfig->setMountPoint('mount'); + $storageConfig->setBackend($backend); + $storageConfig->setAuthMechanism($authMech); + $storageConfig->setBackendOptions([]); + + $this->service->expects($this->once()) + ->method('createStorage') + ->will($this->returnValue($storageConfig)); + + if ($expectSuccess) { + $this->service->expects($this->once()) + ->method('addStorage') + ->with($storageConfig) + ->will($this->returnValue($storageConfig)); + } else { + $this->service->expects($this->never()) + ->method('addStorage'); + } + + $response = $this->controller->create( + 'mount', + '\OCA\Files_External\Lib\Storage\SMB', + '\OCA\Files_External\Lib\Auth\NullMechanism', + array(), + [], + [], + [], + null + ); + + if ($expectSuccess) { + $this->assertEquals(Http::STATUS_CREATED, $response->getStatus()); + } else { + $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus()); + } + } + +} diff --git a/apps/files_external/tests/Controller/UserStoragesControllerTest.php b/apps/files_external/tests/Controller/UserStoragesControllerTest.php new file mode 100644 index 00000000000..ac8d4663740 --- /dev/null +++ b/apps/files_external/tests/Controller/UserStoragesControllerTest.php @@ -0,0 +1,104 @@ + + * @author Robin McCorkell + * @author Vincent Petry + * + * @copyright Copyright (c) 2016, 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 + * + */ +namespace OCA\Files_External\Tests\Controller; + +use \OCA\Files_External\Controller\UserStoragesController; +use \OCP\AppFramework\Http; +use \OCA\Files_External\Service\BackendService; + +class UserStoragesControllerTest extends StoragesControllerTest { + + /** + * @var array + */ + private $oldAllowedBackends; + + public function setUp() { + parent::setUp(); + $this->service = $this->getMockBuilder('\OCA\Files_External\Service\UserStoragesService') + ->disableOriginalConstructor() + ->getMock(); + + $this->service->method('getVisibilityType') + ->willReturn(BackendService::VISIBILITY_PERSONAL); + + $this->controller = new UserStoragesController( + 'files_external', + $this->getMock('\OCP\IRequest'), + $this->getMock('\OCP\IL10N'), + $this->service, + $this->getMock('\OCP\IUserSession'), + $this->getMock('\OCP\ILogger') + ); + } + + public function testAddOrUpdateStorageDisallowedBackend() { + $backend = $this->getBackendMock(); + $backend->method('isVisibleFor') + ->with(BackendService::VISIBILITY_PERSONAL) + ->willReturn(false); + $authMech = $this->getAuthMechMock(); + + $storageConfig = new StorageConfig(1); + $storageConfig->setMountPoint('mount'); + $storageConfig->setBackend($backend); + $storageConfig->setAuthMechanism($authMech); + $storageConfig->setBackendOptions([]); + + $this->service->expects($this->exactly(2)) + ->method('createStorage') + ->will($this->returnValue($storageConfig)); + $this->service->expects($this->never()) + ->method('addStorage'); + $this->service->expects($this->never()) + ->method('updateStorage'); + + $response = $this->controller->create( + 'mount', + '\OCA\Files_External\Lib\Storage\SMB', + '\Auth\Mechanism', + array(), + [], + [], + [], + null + ); + + $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus()); + + $response = $this->controller->update( + 1, + 'mount', + '\OCA\Files_External\Lib\Storage\SMB', + '\Auth\Mechanism', + array(), + [], + [], + [], + null + ); + + $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus()); + } + +} diff --git a/apps/files_external/tests/Service/BackendServiceTest.php b/apps/files_external/tests/Service/BackendServiceTest.php new file mode 100644 index 00000000000..ba9a1f533f2 --- /dev/null +++ b/apps/files_external/tests/Service/BackendServiceTest.php @@ -0,0 +1,211 @@ + + * + * @copyright Copyright (c) 2016, 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 + * + */ +namespace OCA\Files_External\Tests\Service; + +use \OCA\Files_External\Service\BackendService; + +class BackendServiceTest extends \Test\TestCase { + + /** @var \OCP\IConfig */ + protected $config; + + /** @var \OCP\IL10N */ + protected $l10n; + + protected function setUp() { + $this->config = $this->getMock('\OCP\IConfig'); + $this->l10n = $this->getMock('\OCP\IL10N'); + } + + /** + * @param string $class + * + * @return \OCA\Files_External\Lib\Backend\Backend + */ + protected function getBackendMock($class) { + $backend = $this->getMockBuilder('\OCA\Files_External\Lib\Backend\Backend') + ->disableOriginalConstructor() + ->getMock(); + $backend->method('getIdentifier')->will($this->returnValue('identifier:'.$class)); + $backend->method('getIdentifierAliases')->will($this->returnValue(['identifier:'.$class])); + return $backend; + } + + /** + * @param string $class + * + * @return \OCA\Files_External\Lib\Auth\AuthMechanism + */ + protected function getAuthMechanismMock($class) { + $backend = $this->getMockBuilder('\OCA\Files_External\Lib\Auth\AuthMechanism') + ->disableOriginalConstructor() + ->getMock(); + $backend->method('getIdentifier')->will($this->returnValue('identifier:'.$class)); + $backend->method('getIdentifierAliases')->will($this->returnValue(['identifier:'.$class])); + return $backend; + } + + public function testRegisterBackend() { + $service = new BackendService($this->config, $this->l10n); + + $backend = $this->getBackendMock('\Foo\Bar'); + + $backendAlias = $this->getMockBuilder('\OCA\Files_External\Lib\Backend\Backend') + ->disableOriginalConstructor() + ->getMock(); + $backendAlias->method('getIdentifierAliases') + ->willReturn(['identifier_real', 'identifier_alias']); + $backendAlias->method('getIdentifier') + ->willReturn('identifier_real'); + + $service->registerBackend($backend); + $service->registerBackend($backendAlias); + + $this->assertEquals($backend, $service->getBackend('identifier:\Foo\Bar')); + $this->assertEquals($backendAlias, $service->getBackend('identifier_real')); + $this->assertEquals($backendAlias, $service->getBackend('identifier_alias')); + + $backends = $service->getBackends(); + $this->assertCount(2, $backends); + $this->assertArrayHasKey('identifier:\Foo\Bar', $backends); + $this->assertArrayHasKey('identifier_real', $backends); + $this->assertArrayNotHasKey('identifier_alias', $backends); + } + + public function testBackendProvider() { + $service = new BackendService($this->config, $this->l10n); + + $backend1 = $this->getBackendMock('\Foo\Bar'); + $backend2 = $this->getBackendMock('\Bar\Foo'); + + $providerMock = $this->getMock('\OCA\Files_External\Lib\Config\IBackendProvider'); + $providerMock->expects($this->once()) + ->method('getBackends') + ->willReturn([$backend1, $backend2]); + $service->registerBackendProvider($providerMock); + + $this->assertEquals($backend1, $service->getBackend('identifier:\Foo\Bar')); + $this->assertEquals($backend2, $service->getBackend('identifier:\Bar\Foo')); + + $this->assertCount(2, $service->getBackends()); + } + + public function testAuthMechanismProvider() { + $service = new BackendService($this->config, $this->l10n); + + $backend1 = $this->getAuthMechanismMock('\Foo\Bar'); + $backend2 = $this->getAuthMechanismMock('\Bar\Foo'); + + $providerMock = $this->getMock('\OCA\Files_External\Lib\Config\IAuthMechanismProvider'); + $providerMock->expects($this->once()) + ->method('getAuthMechanisms') + ->willReturn([$backend1, $backend2]); + $service->registerAuthMechanismProvider($providerMock); + + $this->assertEquals($backend1, $service->getAuthMechanism('identifier:\Foo\Bar')); + $this->assertEquals($backend2, $service->getAuthMechanism('identifier:\Bar\Foo')); + + $this->assertCount(2, $service->getAuthMechanisms()); + } + + public function testMultipleBackendProviders() { + $service = new BackendService($this->config, $this->l10n); + + $backend1a = $this->getBackendMock('\Foo\Bar'); + $backend1b = $this->getBackendMock('\Bar\Foo'); + + $backend2 = $this->getBackendMock('\Dead\Beef'); + + $provider1Mock = $this->getMock('\OCA\Files_External\Lib\Config\IBackendProvider'); + $provider1Mock->expects($this->once()) + ->method('getBackends') + ->willReturn([$backend1a, $backend1b]); + $service->registerBackendProvider($provider1Mock); + $provider2Mock = $this->getMock('\OCA\Files_External\Lib\Config\IBackendProvider'); + $provider2Mock->expects($this->once()) + ->method('getBackends') + ->willReturn([$backend2]); + $service->registerBackendProvider($provider2Mock); + + $this->assertEquals($backend1a, $service->getBackend('identifier:\Foo\Bar')); + $this->assertEquals($backend1b, $service->getBackend('identifier:\Bar\Foo')); + $this->assertEquals($backend2, $service->getBackend('identifier:\Dead\Beef')); + + $this->assertCount(3, $service->getBackends()); + } + + public function testUserMountingBackends() { + $this->config->expects($this->exactly(2)) + ->method('getAppValue') + ->will($this->returnValueMap([ + ['files_external', 'allow_user_mounting', 'yes', 'yes'], + ['files_external', 'user_mounting_backends', '', 'identifier:\User\Mount\Allowed,identifier_alias'] + ])); + + $service = new BackendService($this->config, $this->l10n); + + $backendAllowed = $this->getBackendMock('\User\Mount\Allowed'); + $backendAllowed->expects($this->never()) + ->method('removeVisibility'); + $backendNotAllowed = $this->getBackendMock('\User\Mount\NotAllowed'); + $backendNotAllowed->expects($this->once()) + ->method('removeVisibility') + ->with(BackendService::VISIBILITY_PERSONAL); + + $backendAlias = $this->getMockBuilder('\OCA\Files_External\Lib\Backend\Backend') + ->disableOriginalConstructor() + ->getMock(); + $backendAlias->method('getIdentifierAliases') + ->willReturn(['identifier_real', 'identifier_alias']); + $backendAlias->expects($this->never()) + ->method('removeVisibility'); + + $service->registerBackend($backendAllowed); + $service->registerBackend($backendNotAllowed); + $service->registerBackend($backendAlias); + } + + public function testGetAvailableBackends() { + $service = new BackendService($this->config, $this->l10n); + + $backendAvailable = $this->getBackendMock('\Backend\Available'); + $backendAvailable->expects($this->once()) + ->method('checkDependencies') + ->will($this->returnValue([])); + $backendNotAvailable = $this->getBackendMock('\Backend\NotAvailable'); + $backendNotAvailable->expects($this->once()) + ->method('checkDependencies') + ->will($this->returnValue([ + $this->getMockBuilder('\OCA\Files_External\Lib\MissingDependency') + ->disableOriginalConstructor() + ->getMock() + ])); + + $service->registerBackend($backendAvailable); + $service->registerBackend($backendNotAvailable); + + $availableBackends = $service->getAvailableBackends(); + $this->assertArrayHasKey('identifier:\Backend\Available', $availableBackends); + $this->assertArrayNotHasKey('identifier:\Backend\NotAvailable', $availableBackends); + } + +} + diff --git a/apps/files_external/tests/Service/DBConfigServiceTest.php b/apps/files_external/tests/Service/DBConfigServiceTest.php new file mode 100644 index 00000000000..b088a7078d1 --- /dev/null +++ b/apps/files_external/tests/Service/DBConfigServiceTest.php @@ -0,0 +1,285 @@ + + * @author Robin McCorkell + * + * @copyright Copyright (c) 2016, 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 + * + */ + +namespace OCA\Files_External\Tests\Service; + + +use OCA\Files_External\Service\DBConfigService; +use OCP\IDBConnection; +use Test\TestCase; + +/** + * @group DB + */ +class DBConfigServiceTest extends TestCase { + /** + * @var DBConfigService + */ + private $dbConfig; + + /** + * @var IDBConnection + */ + private $connection; + + private $mounts = []; + + public function setUp() { + parent::setUp(); + $this->connection = \OC::$server->getDatabaseConnection(); + $this->dbConfig = new DBConfigService($this->connection, \OC::$server->getCrypto()); + } + + public function tearDown() { + foreach ($this->mounts as $mount) { + $this->dbConfig->removeMount($mount); + } + $this->mounts = []; + } + + private function addMount($mountPoint, $storageBackend, $authBackend, $priority, $type) { + $id = $this->dbConfig->addMount($mountPoint, $storageBackend, $authBackend, $priority, $type); + $this->mounts[] = $id; + return $id; + } + + public function testAddSimpleMount() { + $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + + $mount = $this->dbConfig->getMountById($id); + $this->assertEquals('/test', $mount['mount_point']); + $this->assertEquals('foo', $mount['storage_backend']); + $this->assertEquals('bar', $mount['auth_backend']); + $this->assertEquals(100, $mount['priority']); + $this->assertEquals(DBConfigService::MOUNT_TYPE_ADMIN, $mount['type']); + $this->assertEquals([], $mount['applicable']); + $this->assertEquals([], $mount['config']); + $this->assertEquals([], $mount['options']); + } + + public function testAddApplicable() { + $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test'); + + $mount = $this->dbConfig->getMountById($id); + $this->assertEquals([ + ['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id] + ], $mount['applicable']); + + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, 'bar'); + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); + + $mount = $this->dbConfig->getMountById($id); + $this->assertEquals([ + ['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id], + ['type' => DBConfigService::APPLICABLE_TYPE_GROUP, 'value' => 'bar', 'mount_id' => $id], + ['type' => DBConfigService::APPLICABLE_TYPE_GLOBAL, 'value' => null, 'mount_id' => $id] + ], $mount['applicable']); + } + + public function testAddApplicableDouble() { + $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test'); + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test'); + + $mount = $this->dbConfig->getMountById($id); + $this->assertEquals([ + ['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id] + ], $mount['applicable']); + } + + public function testDeleteMount() { + $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + + $this->dbConfig->removeMount($id); + + $mount = $this->dbConfig->getMountById($id); + $this->assertEquals(null, $mount); + } + + public function testRemoveApplicable() { + $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test'); + $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test'); + + $mount = $this->dbConfig->getMountById($id); + $this->assertEquals([], $mount['applicable']); + } + + public function testRemoveApplicableGlobal() { + $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); + $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test'); + + $mount = $this->dbConfig->getMountById($id); + $this->assertEquals([ + ['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id] + ], $mount['applicable']); + } + + public function testSetConfig() { + $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + $this->dbConfig->setConfig($id, 'foo', 'bar'); + + $mount = $this->dbConfig->getMountById($id); + $this->assertEquals(['foo' => 'bar'], $mount['config']); + + $this->dbConfig->setConfig($id, 'foo2', 'bar2'); + + $mount = $this->dbConfig->getMountById($id); + $this->assertEquals(['foo' => 'bar', 'foo2' => 'bar2'], $mount['config']); + } + + public function testSetConfigOverwrite() { + $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + $this->dbConfig->setConfig($id, 'foo', 'bar'); + $this->dbConfig->setConfig($id, 'asd', '1'); + $this->dbConfig->setConfig($id, 'foo', 'qwerty'); + + $mount = $this->dbConfig->getMountById($id); + $this->assertEquals(['foo' => 'qwerty', 'asd' => '1'], $mount['config']); + } + + public function testSetOption() { + $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + $this->dbConfig->setOption($id, 'foo', 'bar'); + + $mount = $this->dbConfig->getMountById($id); + $this->assertEquals(['foo' => 'bar'], $mount['options']); + + $this->dbConfig->setOption($id, 'foo2', 'bar2'); + + $mount = $this->dbConfig->getMountById($id); + $this->assertEquals(['foo' => 'bar', 'foo2' => 'bar2'], $mount['options']); + } + + public function testSetOptionOverwrite() { + $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + $this->dbConfig->setOption($id, 'foo', 'bar'); + $this->dbConfig->setOption($id, 'asd', '1'); + $this->dbConfig->setOption($id, 'foo', 'qwerty'); + + $mount = $this->dbConfig->getMountById($id); + $this->assertEquals(['foo' => 'qwerty', 'asd' => '1'], $mount['options']); + } + + public function testGetMountsFor() { + $mounts = $this->dbConfig->getMountsFor(DBConfigService::APPLICABLE_TYPE_USER, 'test'); + $this->assertEquals([], $mounts); + + $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test'); + + $mounts = $this->dbConfig->getMountsFor(DBConfigService::APPLICABLE_TYPE_USER, 'test'); + $this->assertCount(1, $mounts); + $this->assertEquals($id, $mounts[0]['mount_id']); + $this->assertEquals([['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id]], $mounts[0]['applicable']); + } + + public function testGetAdminMounts() { + $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + $this->addMount('/test2', 'foo2', 'bar2', 100, DBConfigService::MOUNT_TYPE_PERSONAl); + + $mounts = $this->dbConfig->getAdminMounts(); + $this->assertCount(1, $mounts); + $this->assertEquals($id1, $mounts[0]['mount_id']); + } + + public function testGetAdminMountsFor() { + $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + $this->addMount('/test2', 'foo2', 'bar2', 100, DBConfigService::MOUNT_TYPE_ADMIN); + $id3 = $this->addMount('/test3', 'foo3', 'bar3', 100, DBConfigService::MOUNT_TYPE_PERSONAl); + + $this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_USER, 'test'); + $this->dbConfig->addApplicable($id3, DBConfigService::APPLICABLE_TYPE_USER, 'test'); + + $mounts = $this->dbConfig->getAdminMountsFor(DBConfigService::APPLICABLE_TYPE_USER, 'test'); + $this->assertCount(1, $mounts); + $this->assertEquals($id1, $mounts[0]['mount_id']); + $this->assertEquals([['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id1]], $mounts[0]['applicable']); + } + + public function testGetUserMountsFor() { + $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + $this->addMount('/test2', 'foo2', 'bar2', 100, DBConfigService::MOUNT_TYPE_PERSONAl); + $id3 = $this->addMount('/test3', 'foo3', 'bar3', 100, DBConfigService::MOUNT_TYPE_PERSONAl); + + $this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_USER, 'test'); + $this->dbConfig->addApplicable($id3, DBConfigService::APPLICABLE_TYPE_USER, 'test'); + + $mounts = $this->dbConfig->getUserMountsFor(DBConfigService::APPLICABLE_TYPE_USER, 'test'); + $this->assertCount(1, $mounts); + $this->assertEquals($id3, $mounts[0]['mount_id']); + $this->assertEquals([['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id3]], $mounts[0]['applicable']); + } + + public function testGetAdminMountsForGlobal() { + $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + + $this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); + + $mounts = $this->dbConfig->getAdminMountsFor(DBConfigService::APPLICABLE_TYPE_GLOBAL, null); + $this->assertCount(1, $mounts); + $this->assertEquals($id1, $mounts[0]['mount_id']); + $this->assertEquals([['type' => DBConfigService::APPLICABLE_TYPE_GLOBAL, 'value' => null, 'mount_id' => $id1]], $mounts[0]['applicable']); + } + + public function testSetMountPoint() { + $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + $id2 = $this->addMount('/foo', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + + $this->dbConfig->setMountPoint($id1, '/asd'); + + $mount = $this->dbConfig->getMountById($id1); + $this->assertEquals('/asd', $mount['mount_point']); + + // remains unchanged + $mount = $this->dbConfig->getMountById($id2); + $this->assertEquals('/foo', $mount['mount_point']); + } + + public function testSetAuthBackend() { + $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + $id2 = $this->addMount('/foo', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + + $this->dbConfig->setAuthBackend($id1, 'none'); + + $mount = $this->dbConfig->getMountById($id1); + $this->assertEquals('none', $mount['auth_backend']); + + // remains unchanged + $mount = $this->dbConfig->getMountById($id2); + $this->assertEquals('bar', $mount['auth_backend']); + } + + public function testGetMountsForDuplicateByGroup() { + $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); + + $this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_GROUP, 'group1'); + $this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_GROUP, 'group2'); + + $mounts = $this->dbConfig->getAdminMountsForMultiple(DBConfigService::APPLICABLE_TYPE_GROUP, ['group1', 'group2']); + $this->assertCount(1, $mounts); + $this->assertEquals($id1, $mounts[0]['mount_id']); + } +} diff --git a/apps/files_external/tests/Service/GlobalStoragesServiceTest.php b/apps/files_external/tests/Service/GlobalStoragesServiceTest.php new file mode 100644 index 00000000000..b2cc7f59ed4 --- /dev/null +++ b/apps/files_external/tests/Service/GlobalStoragesServiceTest.php @@ -0,0 +1,627 @@ + + * @author Robin McCorkell + * @author Vincent Petry + * + * @copyright Copyright (c) 2016, 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 + * + */ +namespace OCA\Files_External\Tests\Service; + +use \OC\Files\Filesystem; + +use \OCA\Files_External\Service\GlobalStoragesService; + +/** + * @group DB + */ +class GlobalStoragesServiceTest extends StoragesServiceTest { + public function setUp() { + parent::setUp(); + $this->service = new GlobalStoragesService($this->backendService, $this->dbConfig, $this->mountCache); + } + + public function tearDown() { + @unlink($this->dataDir . '/mount.json'); + parent::tearDown(); + } + + protected function makeTestStorageData() { + return $this->makeStorageConfig([ + 'mountPoint' => 'mountpoint', + 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB', + 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism', + 'backendOptions' => [ + 'option1' => 'value1', + 'option2' => 'value2', + 'password' => 'testPassword', + ], + 'applicableUsers' => [], + 'applicableGroups' => [], + 'priority' => 15, + 'mountOptions' => [ + 'preview' => false, + ] + ]); + } + + function storageDataProvider() { + return [ + // all users + [ + [ + 'mountPoint' => 'mountpoint', + 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB', + 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism', + 'backendOptions' => [ + 'option1' => 'value1', + 'option2' => 'value2', + 'password' => 'testPassword', + ], + 'applicableUsers' => [], + 'applicableGroups' => [], + 'priority' => 15, + ], + ], + // some users + [ + [ + 'mountPoint' => 'mountpoint', + 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB', + 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism', + 'backendOptions' => [ + 'option1' => 'value1', + 'option2' => 'value2', + 'password' => 'testPassword', + ], + 'applicableUsers' => ['user1', 'user2'], + 'applicableGroups' => [], + 'priority' => 15, + ], + ], + // some groups + [ + [ + 'mountPoint' => 'mountpoint', + 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB', + 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism', + 'backendOptions' => [ + 'option1' => 'value1', + 'option2' => 'value2', + 'password' => 'testPassword', + ], + 'applicableUsers' => [], + 'applicableGroups' => ['group1', 'group2'], + 'priority' => 15, + ], + ], + // both users and groups + [ + [ + 'mountPoint' => 'mountpoint', + 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB', + 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism', + 'backendOptions' => [ + 'option1' => 'value1', + 'option2' => 'value2', + 'password' => 'testPassword', + ], + 'applicableUsers' => ['user1', 'user2'], + 'applicableGroups' => ['group1', 'group2'], + 'priority' => 15, + ], + ], + ]; + } + + /** + * @dataProvider storageDataProvider + */ + public function testAddStorage($storageParams) { + $storage = $this->makeStorageConfig($storageParams); + $newStorage = $this->service->addStorage($storage); + + $baseId = $newStorage->getId(); + + $newStorage = $this->service->getStorage($baseId); + + $this->assertEquals($storage->getMountPoint(), $newStorage->getMountPoint()); + $this->assertEquals($storage->getBackend(), $newStorage->getBackend()); + $this->assertEquals($storage->getAuthMechanism(), $newStorage->getAuthMechanism()); + $this->assertEquals($storage->getBackendOptions(), $newStorage->getBackendOptions()); + $this->assertEquals($storage->getApplicableUsers(), $newStorage->getApplicableUsers()); + $this->assertEquals($storage->getApplicableGroups(), $newStorage->getApplicableGroups()); + $this->assertEquals($storage->getPriority(), $newStorage->getPriority()); + $this->assertEquals(0, $newStorage->getStatus()); + + $nextStorage = $this->service->addStorage($storage); + $this->assertEquals($baseId + 1, $nextStorage->getId()); + } + + /** + * @dataProvider storageDataProvider + */ + public function testUpdateStorage($updatedStorageParams) { + $updatedStorage = $this->makeStorageConfig($updatedStorageParams); + $storage = $this->makeStorageConfig([ + 'mountPoint' => 'mountpoint', + 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB', + 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism', + 'backendOptions' => [ + 'option1' => 'value1', + 'option2' => 'value2', + 'password' => 'testPassword', + ], + 'applicableUsers' => [], + 'applicableGroups' => [], + 'priority' => 15, + ]); + + $newStorage = $this->service->addStorage($storage); + $id = $newStorage->getId(); + + $updatedStorage->setId($id); + + $this->service->updateStorage($updatedStorage); + $newStorage = $this->service->getStorage($id); + + $this->assertEquals($updatedStorage->getMountPoint(), $newStorage->getMountPoint()); + $this->assertEquals($updatedStorage->getBackendOptions()['password'], $newStorage->getBackendOptions()['password']); + $this->assertEquals($updatedStorage->getApplicableUsers(), $newStorage->getApplicableUsers()); + $this->assertEquals($updatedStorage->getApplicableGroups(), $newStorage->getApplicableGroups()); + $this->assertEquals($updatedStorage->getPriority(), $newStorage->getPriority()); + $this->assertEquals(0, $newStorage->getStatus()); + } + + function hooksAddStorageDataProvider() { + return [ + // applicable all + [ + [], + [], + // expected hook calls + [ + [ + Filesystem::signal_create_mount, + \OC_Mount_Config::MOUNT_TYPE_USER, + 'all' + ], + ], + ], + // single user + [ + ['user1'], + [], + // expected hook calls + [ + [ + Filesystem::signal_create_mount, + \OC_Mount_Config::MOUNT_TYPE_USER, + 'user1', + ], + ], + ], + // single group + [ + [], + ['group1'], + // expected hook calls + [ + [ + Filesystem::signal_create_mount, + \OC_Mount_Config::MOUNT_TYPE_GROUP, + 'group1', + ], + ], + ], + // multiple users + [ + ['user1', 'user2'], + [], + [ + [ + Filesystem::signal_create_mount, + \OC_Mount_Config::MOUNT_TYPE_USER, + 'user1', + ], + [ + Filesystem::signal_create_mount, + \OC_Mount_Config::MOUNT_TYPE_USER, + 'user2', + ], + ], + ], + // multiple groups + [ + [], + ['group1', 'group2'], + // expected hook calls + [ + [ + Filesystem::signal_create_mount, + \OC_Mount_Config::MOUNT_TYPE_GROUP, + 'group1' + ], + [ + Filesystem::signal_create_mount, + \OC_Mount_Config::MOUNT_TYPE_GROUP, + 'group2' + ], + ], + ], + // mixed groups and users + [ + ['user1', 'user2'], + ['group1', 'group2'], + // expected hook calls + [ + [ + Filesystem::signal_create_mount, + \OC_Mount_Config::MOUNT_TYPE_USER, + 'user1', + ], + [ + Filesystem::signal_create_mount, + \OC_Mount_Config::MOUNT_TYPE_USER, + 'user2', + ], + [ + Filesystem::signal_create_mount, + \OC_Mount_Config::MOUNT_TYPE_GROUP, + 'group1' + ], + [ + Filesystem::signal_create_mount, + \OC_Mount_Config::MOUNT_TYPE_GROUP, + 'group2' + ], + ], + ], + ]; + } + + /** + * @dataProvider hooksAddStorageDataProvider + */ + public function testHooksAddStorage($applicableUsers, $applicableGroups, $expectedCalls) { + $storage = $this->makeTestStorageData(); + $storage->setApplicableUsers($applicableUsers); + $storage->setApplicableGroups($applicableGroups); + $this->service->addStorage($storage); + + $this->assertCount(count($expectedCalls), self::$hookCalls); + + foreach ($expectedCalls as $index => $call) { + $this->assertHookCall( + self::$hookCalls[$index], + $call[0], + $storage->getMountPoint(), + $call[1], + $call[2] + ); + } + } + + function hooksUpdateStorageDataProvider() { + return [ + [ + // nothing to multiple users and groups + [], + [], + ['user1', 'user2'], + ['group1', 'group2'], + // expected hook calls + [ + // delete the "all entry" + [ + Filesystem::signal_delete_mount, + \OC_Mount_Config::MOUNT_TYPE_USER, + 'all', + ], + [ + Filesystem::signal_create_mount, + \OC_Mount_Config::MOUNT_TYPE_USER, + 'user1', + ], + [ + Filesystem::signal_create_mount, + \OC_Mount_Config::MOUNT_TYPE_USER, + 'user2', + ], + [ + Filesystem::signal_create_mount, + \OC_Mount_Config::MOUNT_TYPE_GROUP, + 'group1' + ], + [ + Filesystem::signal_create_mount, + \OC_Mount_Config::MOUNT_TYPE_GROUP, + 'group2' + ], + ], + ], + [ + // adding a user and a group + ['user1'], + ['group1'], + ['user1', 'user2'], + ['group1', 'group2'], + // expected hook calls + [ + [ + Filesystem::signal_create_mount, + \OC_Mount_Config::MOUNT_TYPE_USER, + 'user2', + ], + [ + Filesystem::signal_create_mount, + \OC_Mount_Config::MOUNT_TYPE_GROUP, + 'group2' + ], + ], + ], + [ + // removing a user and a group + ['user1', 'user2'], + ['group1', 'group2'], + ['user1'], + ['group1'], + // expected hook calls + [ + [ + Filesystem::signal_delete_mount, + \OC_Mount_Config::MOUNT_TYPE_USER, + 'user2', + ], + [ + Filesystem::signal_delete_mount, + \OC_Mount_Config::MOUNT_TYPE_GROUP, + 'group2' + ], + ], + ], + [ + // removing all + ['user1'], + ['group1'], + [], + [], + // expected hook calls + [ + [ + Filesystem::signal_delete_mount, + \OC_Mount_Config::MOUNT_TYPE_USER, + 'user1', + ], + [ + Filesystem::signal_delete_mount, + \OC_Mount_Config::MOUNT_TYPE_GROUP, + 'group1' + ], + // create the "all" entry + [ + Filesystem::signal_create_mount, + \OC_Mount_Config::MOUNT_TYPE_USER, + 'all' + ], + ], + ], + [ + // no changes + ['user1'], + ['group1'], + ['user1'], + ['group1'], + // no hook calls + [] + ] + ]; + } + + /** + * @dataProvider hooksUpdateStorageDataProvider + */ + public function testHooksUpdateStorage( + $sourceApplicableUsers, + $sourceApplicableGroups, + $updatedApplicableUsers, + $updatedApplicableGroups, + $expectedCalls) { + + $storage = $this->makeTestStorageData(); + $storage->setApplicableUsers($sourceApplicableUsers); + $storage->setApplicableGroups($sourceApplicableGroups); + $storage = $this->service->addStorage($storage); + + $storage->setApplicableUsers($updatedApplicableUsers); + $storage->setApplicableGroups($updatedApplicableGroups); + + // reset calls + self::$hookCalls = []; + + $this->service->updateStorage($storage); + + $this->assertCount(count($expectedCalls), self::$hookCalls); + + foreach ($expectedCalls as $index => $call) { + $this->assertHookCall( + self::$hookCalls[$index], + $call[0], + '/mountpoint', + $call[1], + $call[2] + ); + } + } + + /** + */ + public function testHooksRenameMountPoint() { + $storage = $this->makeTestStorageData(); + $storage->setApplicableUsers(['user1', 'user2']); + $storage->setApplicableGroups(['group1', 'group2']); + $storage = $this->service->addStorage($storage); + + $storage->setMountPoint('renamedMountpoint'); + + // reset calls + self::$hookCalls = []; + + $this->service->updateStorage($storage); + + $expectedCalls = [ + // deletes old mount + [ + Filesystem::signal_delete_mount, + '/mountpoint', + \OC_Mount_Config::MOUNT_TYPE_USER, + 'user1', + ], + [ + Filesystem::signal_delete_mount, + '/mountpoint', + \OC_Mount_Config::MOUNT_TYPE_USER, + 'user2', + ], + [ + Filesystem::signal_delete_mount, + '/mountpoint', + \OC_Mount_Config::MOUNT_TYPE_GROUP, + 'group1', + ], + [ + Filesystem::signal_delete_mount, + '/mountpoint', + \OC_Mount_Config::MOUNT_TYPE_GROUP, + 'group2', + ], + // creates new one + [ + Filesystem::signal_create_mount, + '/renamedMountpoint', + \OC_Mount_Config::MOUNT_TYPE_USER, + 'user1', + ], + [ + Filesystem::signal_create_mount, + '/renamedMountpoint', + \OC_Mount_Config::MOUNT_TYPE_USER, + 'user2', + ], + [ + Filesystem::signal_create_mount, + '/renamedMountpoint', + \OC_Mount_Config::MOUNT_TYPE_GROUP, + 'group1', + ], + [ + Filesystem::signal_create_mount, + '/renamedMountpoint', + \OC_Mount_Config::MOUNT_TYPE_GROUP, + 'group2', + ], + ]; + + $this->assertCount(count($expectedCalls), self::$hookCalls); + + foreach ($expectedCalls as $index => $call) { + $this->assertHookCall( + self::$hookCalls[$index], + $call[0], + $call[1], + $call[2], + $call[3] + ); + } + } + + function hooksDeleteStorageDataProvider() { + return [ + [ + ['user1', 'user2'], + ['group1', 'group2'], + // expected hook calls + [ + [ + Filesystem::signal_delete_mount, + \OC_Mount_Config::MOUNT_TYPE_USER, + 'user1', + ], + [ + Filesystem::signal_delete_mount, + \OC_Mount_Config::MOUNT_TYPE_USER, + 'user2', + ], + [ + Filesystem::signal_delete_mount, + \OC_Mount_Config::MOUNT_TYPE_GROUP, + 'group1' + ], + [ + Filesystem::signal_delete_mount, + \OC_Mount_Config::MOUNT_TYPE_GROUP, + 'group2' + ], + ], + ], + [ + // deleting "all" entry + [], + [], + [ + [ + Filesystem::signal_delete_mount, + \OC_Mount_Config::MOUNT_TYPE_USER, + 'all', + ], + ], + ], + ]; + } + + /** + * @dataProvider hooksDeleteStorageDataProvider + */ + public function testHooksDeleteStorage( + $sourceApplicableUsers, + $sourceApplicableGroups, + $expectedCalls) { + + $storage = $this->makeTestStorageData(); + $storage->setApplicableUsers($sourceApplicableUsers); + $storage->setApplicableGroups($sourceApplicableGroups); + $storage = $this->service->addStorage($storage); + + // reset calls + self::$hookCalls = []; + + $this->service->removeStorage($storage->getId()); + + $this->assertCount(count($expectedCalls), self::$hookCalls); + + foreach ($expectedCalls as $index => $call) { + $this->assertHookCall( + self::$hookCalls[$index], + $call[0], + '/mountpoint', + $call[1], + $call[2] + ); + } + } + +} diff --git a/apps/files_external/tests/Service/StoragesServiceTest.php b/apps/files_external/tests/Service/StoragesServiceTest.php new file mode 100644 index 00000000000..7cc0d8fad93 --- /dev/null +++ b/apps/files_external/tests/Service/StoragesServiceTest.php @@ -0,0 +1,505 @@ + + * @author Robin McCorkell + * @author Vincent Petry + * + * @copyright Copyright (c) 2016, 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 + * + */ +namespace OCA\Files_External\Tests\Service; + +use \OC\Files\Filesystem; + +use \OCA\Files_external\NotFoundException; +use \OCA\Files_external\Lib\StorageConfig; +use OCA\Files_External\Service\BackendService; +use OCA\Files_External\Service\DBConfigService; +use OCA\Files_External\Service\StoragesService; + +class CleaningDBConfig extends DBConfigService { + private $mountIds = []; + + public function addMount($mountPoint, $storageBackend, $authBackend, $priority, $type) { + $id = parent::addMount($mountPoint, $storageBackend, $authBackend, $priority, $type); // TODO: Change the autogenerated stub + $this->mountIds[] = $id; + return $id; + } + + public function clean() { + foreach ($this->mountIds as $id) { + $this->removeMount($id); + } + } +} + +/** + * @group DB + */ +abstract class StoragesServiceTest extends \Test\TestCase { + + /** + * @var StoragesService + */ + protected $service; + + /** @var BackendService */ + protected $backendService; + + /** + * Data directory + * + * @var string + */ + protected $dataDir; + + /** @var CleaningDBConfig */ + protected $dbConfig; + + /** + * Hook calls + * + * @var array + */ + protected static $hookCalls; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject|\OCP\Files\Config\IUserMountCache + */ + protected $mountCache; + + public function setUp() { + parent::setUp(); + $this->dbConfig = new CleaningDBConfig(\OC::$server->getDatabaseConnection(), \OC::$server->getCrypto()); + self::$hookCalls = array(); + $config = \OC::$server->getConfig(); + $this->dataDir = $config->getSystemValue( + 'datadirectory', + \OC::$SERVERROOT . '/data/' + ); + \OC_Mount_Config::$skipTest = true; + + $this->mountCache = $this->getMock('OCP\Files\Config\IUserMountCache'); + + // prepare BackendService mock + $this->backendService = + $this->getMockBuilder('\OCA\Files_External\Service\BackendService') + ->disableOriginalConstructor() + ->getMock(); + + $authMechanisms = [ + 'identifier:\Auth\Mechanism' => $this->getAuthMechMock('null', '\Auth\Mechanism'), + 'identifier:\Other\Auth\Mechanism' => $this->getAuthMechMock('null', '\Other\Auth\Mechanism'), + 'identifier:\OCA\Files_External\Lib\Auth\NullMechanism' => $this->getAuthMechMock(), + ]; + $this->backendService->method('getAuthMechanism') + ->will($this->returnCallback(function ($class) use ($authMechanisms) { + if (isset($authMechanisms[$class])) { + return $authMechanisms[$class]; + } + return null; + })); + $this->backendService->method('getAuthMechanismsByScheme') + ->will($this->returnCallback(function ($schemes) use ($authMechanisms) { + return array_filter($authMechanisms, function ($authMech) use ($schemes) { + return in_array($authMech->getScheme(), $schemes, true); + }); + })); + $this->backendService->method('getAuthMechanisms') + ->will($this->returnValue($authMechanisms)); + + $sftpBackend = $this->getBackendMock('\OCA\Files_External\Lib\Backend\SFTP', '\OCA\Files_External\Lib\Storage\SFTP'); + $backends = [ + 'identifier:\OCA\Files_External\Lib\Backend\SMB' => $this->getBackendMock('\OCA\Files_External\Lib\Backend\SMB', '\OCA\Files_External\Lib\Storage\SMB'), + 'identifier:\OCA\Files_External\Lib\Backend\SFTP' => $sftpBackend, + 'identifier:sftp_alias' => $sftpBackend, + ]; + $backends['identifier:\OCA\Files_External\Lib\Backend\SFTP']->method('getLegacyAuthMechanism') + ->willReturn($authMechanisms['identifier:\Other\Auth\Mechanism']); + $this->backendService->method('getBackend') + ->will($this->returnCallback(function ($backendClass) use ($backends) { + if (isset($backends[$backendClass])) { + return $backends[$backendClass]; + } + return null; + })); + $this->backendService->method('getBackends') + ->will($this->returnValue($backends)); + + \OCP\Util::connectHook( + Filesystem::CLASSNAME, + Filesystem::signal_create_mount, + get_class($this), 'createHookCallback'); + \OCP\Util::connectHook( + Filesystem::CLASSNAME, + Filesystem::signal_delete_mount, + get_class($this), 'deleteHookCallback'); + + $containerMock = $this->getMock('\OCP\AppFramework\IAppContainer'); + $containerMock->method('query') + ->will($this->returnCallback(function ($name) { + if ($name === 'OCA\Files_External\Service\BackendService') { + return $this->backendService; + } + })); + + \OC_Mount_Config::$app = $this->getMockBuilder('\OCA\Files_External\Appinfo\Application') + ->disableOriginalConstructor() + ->getMock(); + \OC_Mount_Config::$app->method('getContainer') + ->willReturn($containerMock); + } + + public function tearDown() { + \OC_Mount_Config::$skipTest = false; + self::$hookCalls = array(); + if ($this->dbConfig) { + $this->dbConfig->clean(); + } + } + + protected function getBackendMock($class = '\OCA\Files_External\Lib\Backend\SMB', $storageClass = '\OCA\Files_External\Lib\Storage\SMB') { + $backend = $this->getMockBuilder('\OCA\Files_External\Lib\Backend\Backend') + ->disableOriginalConstructor() + ->getMock(); + $backend->method('getStorageClass') + ->willReturn($storageClass); + $backend->method('getIdentifier') + ->willReturn('identifier:' . $class); + return $backend; + } + + protected function getAuthMechMock($scheme = 'null', $class = '\OCA\Files_External\Lib\Auth\NullMechanism') { + $authMech = $this->getMockBuilder('\OCA\Files_External\Lib\Auth\AuthMechanism') + ->disableOriginalConstructor() + ->getMock(); + $authMech->method('getScheme') + ->willReturn($scheme); + $authMech->method('getIdentifier') + ->willReturn('identifier:' . $class); + + return $authMech; + } + + /** + * Creates a StorageConfig instance based on array data + * + * @param array data + * + * @return StorageConfig storage config instance + */ + protected function makeStorageConfig($data) { + $storage = new StorageConfig(); + if (isset($data['id'])) { + $storage->setId($data['id']); + } + $storage->setMountPoint($data['mountPoint']); + if (!isset($data['backend'])) { + // data providers are run before $this->backendService is initialised + // so $data['backend'] can be specified directly + $data['backend'] = $this->backendService->getBackend($data['backendIdentifier']); + } + if (!isset($data['backend'])) { + throw new \Exception('oops, no backend'); + } + if (!isset($data['authMechanism'])) { + $data['authMechanism'] = $this->backendService->getAuthMechanism($data['authMechanismIdentifier']); + } + if (!isset($data['authMechanism'])) { + throw new \Exception('oops, no auth mechanism'); + } + $storage->setBackend($data['backend']); + $storage->setAuthMechanism($data['authMechanism']); + $storage->setBackendOptions($data['backendOptions']); + if (isset($data['applicableUsers'])) { + $storage->setApplicableUsers($data['applicableUsers']); + } + if (isset($data['applicableGroups'])) { + $storage->setApplicableGroups($data['applicableGroups']); + } + if (isset($data['priority'])) { + $storage->setPriority($data['priority']); + } + if (isset($data['mountOptions'])) { + $storage->setMountOptions($data['mountOptions']); + } + return $storage; + } + + + /** + * @expectedException \OCA\Files_external\NotFoundException + */ + public function testNonExistingStorage() { + $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB'); + $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism'); + $storage = new StorageConfig(255); + $storage->setMountPoint('mountpoint'); + $storage->setBackend($backend); + $storage->setAuthMechanism($authMechanism); + $this->service->updateStorage($storage); + } + + public function deleteStorageDataProvider() { + return [ + // regular case, can properly delete the oc_storages entry + [ + [ + 'share' => 'share', + 'host' => 'example.com', + 'user' => 'test', + 'password' => 'testPassword', + 'root' => 'someroot', + ], + 'smb::test@example.com//share//someroot/', + 0 + ], + // special case with $user vars, cannot auto-remove the oc_storages entry + [ + [ + 'share' => 'share', + 'host' => 'example.com', + 'user' => '$user', + 'password' => 'testPassword', + 'root' => 'someroot', + ], + 'smb::someone@example.com//share//someroot/', + 1 + ], + ]; + } + + /** + * @dataProvider deleteStorageDataProvider + */ + public function testDeleteStorage($backendOptions, $rustyStorageId, $expectedCountAfterDeletion) { + $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB'); + $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism'); + $storage = new StorageConfig(255); + $storage->setMountPoint('mountpoint'); + $storage->setBackend($backend); + $storage->setAuthMechanism($authMechanism); + $storage->setBackendOptions($backendOptions); + + $newStorage = $this->service->addStorage($storage); + $id = $newStorage->getId(); + + // manually trigger storage entry because normally it happens on first + // access, which isn't possible within this test + $storageCache = new \OC\Files\Cache\Storage($rustyStorageId); + + // get numeric id for later check + $numericId = $storageCache->getNumericId(); + + $this->service->removeStorage($id); + + $caught = false; + try { + $this->service->getStorage(1); + } catch (NotFoundException $e) { + $caught = true; + } + + $this->assertTrue($caught); + + // storage id was removed from oc_storages + $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $storageCheckQuery = $qb->select('*') + ->from('storages') + ->where($qb->expr()->eq('numeric_id', $qb->expr()->literal($numericId))); + $this->assertCount($expectedCountAfterDeletion, $storageCheckQuery->execute()->fetchAll()); + } + + /** + * @expectedException \OCA\Files_external\NotFoundException + */ + public function testDeleteUnexistingStorage() { + $this->service->removeStorage(255); + } + + public function testCreateStorage() { + $mountPoint = 'mount'; + $backendIdentifier = 'identifier:\OCA\Files_External\Lib\Backend\SMB'; + $authMechanismIdentifier = 'identifier:\Auth\Mechanism'; + $backendOptions = ['param' => 'foo', 'param2' => 'bar']; + $mountOptions = ['option' => 'foobar']; + $applicableUsers = ['user1', 'user2']; + $applicableGroups = ['group']; + $priority = 123; + + $backend = $this->backendService->getBackend($backendIdentifier); + $authMechanism = $this->backendService->getAuthMechanism($authMechanismIdentifier); + + $storage = $this->service->createStorage( + $mountPoint, + $backendIdentifier, + $authMechanismIdentifier, + $backendOptions, + $mountOptions, + $applicableUsers, + $applicableGroups, + $priority + ); + + $this->assertEquals('/' . $mountPoint, $storage->getMountPoint()); + $this->assertEquals($backend, $storage->getBackend()); + $this->assertEquals($authMechanism, $storage->getAuthMechanism()); + $this->assertEquals($backendOptions, $storage->getBackendOptions()); + $this->assertEquals($mountOptions, $storage->getMountOptions()); + $this->assertEquals($applicableUsers, $storage->getApplicableUsers()); + $this->assertEquals($applicableGroups, $storage->getApplicableGroups()); + $this->assertEquals($priority, $storage->getPriority()); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testCreateStorageInvalidClass() { + $this->service->createStorage( + 'mount', + 'identifier:\OC\Not\A\Backend', + 'identifier:\Auth\Mechanism', + [] + ); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testCreateStorageInvalidAuthMechanismClass() { + $this->service->createStorage( + 'mount', + 'identifier:\OCA\Files_External\Lib\Backend\SMB', + 'identifier:\Not\An\Auth\Mechanism', + [] + ); + } + + public function testGetStoragesBackendNotVisible() { + $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB'); + $backend->expects($this->once()) + ->method('isVisibleFor') + ->with($this->service->getVisibilityType()) + ->willReturn(false); + $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism'); + $authMechanism->method('isVisibleFor') + ->with($this->service->getVisibilityType()) + ->willReturn(true); + + $storage = new StorageConfig(255); + $storage->setMountPoint('mountpoint'); + $storage->setBackend($backend); + $storage->setAuthMechanism($authMechanism); + $storage->setBackendOptions(['password' => 'testPassword']); + + $newStorage = $this->service->addStorage($storage); + + $this->assertCount(1, $this->service->getAllStorages()); + $this->assertEmpty($this->service->getStorages()); + } + + public function testGetStoragesAuthMechanismNotVisible() { + $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB'); + $backend->method('isVisibleFor') + ->with($this->service->getVisibilityType()) + ->willReturn(true); + $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism'); + $authMechanism->expects($this->once()) + ->method('isVisibleFor') + ->with($this->service->getVisibilityType()) + ->willReturn(false); + + $storage = new StorageConfig(255); + $storage->setMountPoint('mountpoint'); + $storage->setBackend($backend); + $storage->setAuthMechanism($authMechanism); + $storage->setBackendOptions(['password' => 'testPassword']); + + $newStorage = $this->service->addStorage($storage); + + $this->assertCount(1, $this->service->getAllStorages()); + $this->assertEmpty($this->service->getStorages()); + } + + public static function createHookCallback($params) { + self::$hookCalls[] = array( + 'signal' => Filesystem::signal_create_mount, + 'params' => $params + ); + } + + public static function deleteHookCallback($params) { + self::$hookCalls[] = array( + 'signal' => Filesystem::signal_delete_mount, + 'params' => $params + ); + } + + /** + * Asserts hook call + * + * @param array $callData hook call data to check + * @param string $signal signal name + * @param string $mountPath mount path + * @param string $mountType mount type + * @param string $applicable applicable users + */ + protected function assertHookCall($callData, $signal, $mountPath, $mountType, $applicable) { + $this->assertEquals($signal, $callData['signal']); + $params = $callData['params']; + $this->assertEquals( + $mountPath, + $params[Filesystem::signal_param_path] + ); + $this->assertEquals( + $mountType, + $params[Filesystem::signal_param_mount_type] + ); + $this->assertEquals( + $applicable, + $params[Filesystem::signal_param_users] + ); + } + + public function testUpdateStorageMountPoint() { + $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB'); + $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism'); + + $storage = new StorageConfig(); + $storage->setMountPoint('mountpoint'); + $storage->setBackend($backend); + $storage->setAuthMechanism($authMechanism); + $storage->setBackendOptions(['password' => 'testPassword']); + + $savedStorage = $this->service->addStorage($storage); + + $newAuthMechanism = $this->backendService->getAuthMechanism('identifier:\Other\Auth\Mechanism'); + + $updatedStorage = new StorageConfig($savedStorage->getId()); + $updatedStorage->setMountPoint('mountpoint2'); + $updatedStorage->setBackend($backend); + $updatedStorage->setAuthMechanism($newAuthMechanism); + $updatedStorage->setBackendOptions(['password' => 'password2']); + + $this->service->updateStorage($updatedStorage); + + $savedStorage = $this->service->getStorage($updatedStorage->getId()); + + $this->assertEquals('/mountpoint2', $savedStorage->getMountPoint()); + $this->assertEquals($newAuthMechanism, $savedStorage->getAuthMechanism()); + $this->assertEquals('password2', $savedStorage->getBackendOption('password')); + } +} diff --git a/apps/files_external/tests/Service/UserGlobalStoragesServiceTest.php b/apps/files_external/tests/Service/UserGlobalStoragesServiceTest.php new file mode 100644 index 00000000000..812c12392fc --- /dev/null +++ b/apps/files_external/tests/Service/UserGlobalStoragesServiceTest.php @@ -0,0 +1,363 @@ + + * @author Robin McCorkell + * @author Vincent Petry + * + * @copyright Copyright (c) 2016, 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 + * + */ +namespace OCA\Files_External\Tests\Service; + +use OCA\Files_external\NotFoundException; +use OCA\Files_External\Service\StoragesService; +use OCA\Files_External\Service\UserGlobalStoragesService; +use OCP\IUser; +use Test\Traits\UserTrait; + +/** + * @group DB + */ +class UserGlobalStoragesServiceTest extends GlobalStoragesServiceTest { + use UserTrait; + + /** @var \OCP\IGroupManager|\PHPUnit_Framework_MockObject_MockObject groupManager */ + protected $groupManager; + + /** + * @var StoragesService + */ + protected $globalStoragesService; + + /** + * @var UserGlobalStoragesService + */ + protected $service; + + protected $user; + + const USER_ID = 'test_user'; + const GROUP_ID = 'test_group'; + const GROUP_ID2 = 'test_group2'; + + public function setUp() { + parent::setUp(); + + $this->globalStoragesService = $this->service; + + $this->user = new \OC\User\User(self::USER_ID, null); + /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject $userSession */ + $userSession = $this->getMock('\OCP\IUserSession'); + $userSession + ->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + + $this->groupManager = $this->getMock('\OCP\IGroupManager'); + $this->groupManager->method('isInGroup') + ->will($this->returnCallback(function ($userId, $groupId) { + if ($userId === self::USER_ID) { + switch ($groupId) { + case self::GROUP_ID: + case self::GROUP_ID2: + return true; + } + } + return false; + })); + $this->groupManager->method('getUserGroupIds') + ->will($this->returnCallback(function (IUser $user) { + if ($user->getUID() === self::USER_ID) { + return [self::GROUP_ID, self::GROUP_ID2]; + } else { + return []; + } + })); + + $this->service = new UserGlobalStoragesService( + $this->backendService, + $this->dbConfig, + $userSession, + $this->groupManager, + $this->mountCache + ); + } + + public function applicableStorageProvider() { + return [ + [[], [], true], + + // not applicable cases + [['user1'], [], false], + [[], ['group1'], false], + [['user1'], ['group1'], false], + + // applicable cases + [[self::USER_ID], [], true], + [[], [self::GROUP_ID], true], + [[self::USER_ID], ['group1'], true], + [['user1'], [self::GROUP_ID], true], + + // sanity checks + [['user1', 'user2', self::USER_ID, 'user3'], [], true], + ]; + } + + /** + * @dataProvider applicableStorageProvider + */ + public function testGetStorageWithApplicable($applicableUsers, $applicableGroups, $isVisible) { + $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB'); + $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism'); + + $storage = new StorageConfig(); + $storage->setMountPoint('mountpoint'); + $storage->setBackend($backend); + $storage->setAuthMechanism($authMechanism); + $storage->setBackendOptions(['password' => 'testPassword']); + $storage->setApplicableUsers($applicableUsers); + $storage->setApplicableGroups($applicableGroups); + + $newStorage = $this->globalStoragesService->addStorage($storage); + + $storages = $this->service->getAllStorages(); + if ($isVisible) { + $this->assertEquals(1, count($storages)); + $retrievedStorage = $this->service->getStorage($newStorage->getId()); + $this->assertEquals('/mountpoint', $retrievedStorage->getMountPoint()); + } else { + $this->assertEquals(0, count($storages)); + + try { + $this->service->getStorage($newStorage->getId()); + $this->fail('Failed asserting that storage can\'t be accessed by id'); + } catch (NotFoundException $e) { + + } + } + + } + + /** + * @expectedException \DomainException + */ + public function testAddStorage($storageParams = null) { + $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB'); + $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism'); + + $storage = new StorageConfig(255); + $storage->setMountPoint('mountpoint'); + $storage->setBackend($backend); + $storage->setAuthMechanism($authMechanism); + $storage->setBackendOptions(['password' => 'testPassword']); + + $this->service->addStorage($storage); + } + + /** + * @expectedException \DomainException + */ + public function testUpdateStorage($storageParams = null) { + $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB'); + $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism'); + + $storage = new StorageConfig(255); + $storage->setMountPoint('mountpoint'); + $storage->setBackend($backend); + $storage->setAuthMechanism($authMechanism); + $storage->setBackendOptions(['password' => 'testPassword']); + + $newStorage = $this->globalStoragesService->addStorage($storage); + + $retrievedStorage = $this->service->getStorage($newStorage->getId()); + $retrievedStorage->setMountPoint('abc'); + $this->service->updateStorage($retrievedStorage); + } + + /** + * @expectedException \DomainException + */ + public function testNonExistingStorage() { + parent::testNonExistingStorage(); + } + + /** + * @expectedException \DomainException + * @dataProvider deleteStorageDataProvider + */ + public function testDeleteStorage($backendOptions, $rustyStorageId, $expectedCountAfterDeletion) { + $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB'); + $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism'); + + $storage = new StorageConfig(255); + $storage->setMountPoint('mountpoint'); + $storage->setBackend($backend); + $storage->setAuthMechanism($authMechanism); + $storage->setBackendOptions($backendOptions); + + $newStorage = $this->globalStoragesService->addStorage($storage); + $id = $newStorage->getId(); + + $this->service->removeStorage($id); + } + + /** + * @expectedException \DomainException + */ + public function testDeleteUnexistingStorage() { + parent::testDeleteUnexistingStorage(); + } + + public function getUniqueStoragesProvider() { + return [ + // 'all' vs group + [100, [], [], 100, [], [self::GROUP_ID], 2], + [100, [], [self::GROUP_ID], 100, [], [], 1], + + // 'all' vs user + [100, [], [], 100, [self::USER_ID], [], 2], + [100, [self::USER_ID], [], 100, [], [], 1], + + // group vs user + [100, [], [self::GROUP_ID], 100, [self::USER_ID], [], 2], + [100, [self::USER_ID], [], 100, [], [self::GROUP_ID], 1], + + // group+user vs group + [100, [], [self::GROUP_ID2], 100, [self::USER_ID], [self::GROUP_ID], 2], + [100, [self::USER_ID], [self::GROUP_ID], 100, [], [self::GROUP_ID2], 1], + + // user vs 'all' (higher priority) + [200, [], [], 100, [self::USER_ID], [], 2], + [100, [self::USER_ID], [], 200, [], [], 1], + + // group vs group (higher priority) + [100, [], [self::GROUP_ID2], 200, [], [self::GROUP_ID], 2], + [200, [], [self::GROUP_ID], 100, [], [self::GROUP_ID2], 1], + ]; + } + + /** + * @dataProvider getUniqueStoragesProvider + */ + public function testGetUniqueStorages( + $priority1, $applicableUsers1, $applicableGroups1, + $priority2, $applicableUsers2, $applicableGroups2, + $expectedPrecedence + ) { + $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB'); + $backend->method('isVisibleFor') + ->willReturn(true); + $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism'); + $authMechanism->method('isVisibleFor') + ->willReturn(true); + + $storage1 = new StorageConfig(); + $storage1->setMountPoint('mountpoint'); + $storage1->setBackend($backend); + $storage1->setAuthMechanism($authMechanism); + $storage1->setBackendOptions(['password' => 'testPassword']); + $storage1->setPriority($priority1); + $storage1->setApplicableUsers($applicableUsers1); + $storage1->setApplicableGroups($applicableGroups1); + + $storage1 = $this->globalStoragesService->addStorage($storage1); + + $storage2 = new StorageConfig(); + $storage2->setMountPoint('mountpoint'); + $storage2->setBackend($backend); + $storage2->setAuthMechanism($authMechanism); + $storage2->setBackendOptions(['password' => 'testPassword']); + $storage2->setPriority($priority2); + $storage2->setApplicableUsers($applicableUsers2); + $storage2->setApplicableGroups($applicableGroups2); + + $storage2 = $this->globalStoragesService->addStorage($storage2); + + $storages = $this->service->getUniqueStorages(); + $this->assertCount(1, $storages); + + if ($expectedPrecedence === 1) { + $this->assertArrayHasKey($storage1->getID(), $storages); + } elseif ($expectedPrecedence === 2) { + $this->assertArrayHasKey($storage2->getID(), $storages); + } + } + + public function testGetStoragesBackendNotVisible() { + // we don't test this here + $this->assertTrue(true); + } + + public function testGetStoragesAuthMechanismNotVisible() { + // we don't test this here + $this->assertTrue(true); + } + + public function testHooksAddStorage($a = null, $b = null, $c = null) { + // we don't test this here + $this->assertTrue(true); + } + + public function testHooksUpdateStorage($a = null, $b = null, $c = null, $d = null, $e = null) { + // we don't test this here + $this->assertTrue(true); + } + + public function testHooksRenameMountPoint() { + // we don't test this here + $this->assertTrue(true); + } + + public function testHooksDeleteStorage($a = null, $b = null, $c = null) { + // we don't test this here + $this->assertTrue(true); + } + + public function testLegacyConfigConversionApplicableAll() { + // we don't test this here + $this->assertTrue(true); + } + + public function testLegacyConfigConversionApplicableUserAndGroup() { + // we don't test this here + $this->assertTrue(true); + } + + public function testReadLegacyConfigAndGenerateConfigId() { + // we don't test this here + $this->assertTrue(true); + } + + public function testReadLegacyConfigNoAuthMechanism() { + // we don't test this here + $this->assertTrue(true); + } + + public function testReadLegacyConfigClass() { + // we don't test this here + $this->assertTrue(true); + } + + public function testReadEmptyMountPoint() { + // we don't test this here + $this->assertTrue(true); + } + + public function testUpdateStorageMountPoint() { + // we don't test this here + $this->assertTrue(true); + } +} diff --git a/apps/files_external/tests/Service/UserStoragesServiceTest.php b/apps/files_external/tests/Service/UserStoragesServiceTest.php new file mode 100644 index 00000000000..21696878a01 --- /dev/null +++ b/apps/files_external/tests/Service/UserStoragesServiceTest.php @@ -0,0 +1,207 @@ + + * @author Robin McCorkell + * @author Vincent Petry + * + * @copyright Copyright (c) 2016, 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 + * + */ +namespace OCA\Files_External\Tests\Service; + +use \OC\Files\Filesystem; + +use OCA\Files_External\Service\GlobalStoragesService; +use OCA\Files_External\Service\StoragesService; +use OCA\Files_External\Service\UserStoragesService; +use \OCA\Files_external\Lib\StorageConfig; +use Test\Traits\UserTrait; + +/** + * @group DB + */ +class UserStoragesServiceTest extends StoragesServiceTest { + use UserTrait; + + private $user; + + private $userId; + + /** + * @var StoragesService + */ + protected $globalStoragesService; + + public function setUp() { + parent::setUp(); + + $this->globalStoragesService = new GlobalStoragesService($this->backendService, $this->dbConfig, $this->mountCache); + + $this->userId = $this->getUniqueID('user_'); + $this->createUser($this->userId, $this->userId); + $this->user = \OC::$server->getUserManager()->get($this->userId); + + /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject $userSession */ + $userSession = $this->getMock('\OCP\IUserSession'); + $userSession + ->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + + $this->service = new UserStoragesService($this->backendService, $this->dbConfig, $userSession, $this->mountCache); + } + + private function makeTestStorageData() { + return $this->makeStorageConfig([ + 'mountPoint' => 'mountpoint', + 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB', + 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism', + 'backendOptions' => [ + 'option1' => 'value1', + 'option2' => 'value2', + 'password' => 'testPassword', + ], + 'mountOptions' => [ + 'preview' => false, + ] + ]); + } + + public function testAddStorage() { + $storage = $this->makeTestStorageData(); + + $newStorage = $this->service->addStorage($storage); + + $id = $newStorage->getId(); + + $newStorage = $this->service->getStorage($id); + + $this->assertEquals($storage->getMountPoint(), $newStorage->getMountPoint()); + $this->assertEquals($storage->getBackend(), $newStorage->getBackend()); + $this->assertEquals($storage->getAuthMechanism(), $newStorage->getAuthMechanism()); + $this->assertEquals($storage->getBackendOptions(), $newStorage->getBackendOptions()); + $this->assertEquals(0, $newStorage->getStatus()); + + // hook called once for user + $this->assertHookCall( + current(self::$hookCalls), + Filesystem::signal_create_mount, + $storage->getMountPoint(), + \OC_Mount_Config::MOUNT_TYPE_USER, + $this->userId + ); + + $nextStorage = $this->service->addStorage($storage); + $this->assertEquals($id + 1, $nextStorage->getId()); + } + + public function testUpdateStorage() { + $storage = $this->makeStorageConfig([ + 'mountPoint' => 'mountpoint', + 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB', + 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism', + 'backendOptions' => [ + 'option1' => 'value1', + 'option2' => 'value2', + 'password' => 'testPassword', + ], + ]); + + $newStorage = $this->service->addStorage($storage); + + $backendOptions = $newStorage->getBackendOptions(); + $backendOptions['password'] = 'anotherPassword'; + $newStorage->setBackendOptions($backendOptions); + + self::$hookCalls = []; + + $newStorage = $this->service->updateStorage($newStorage); + + $this->assertEquals('anotherPassword', $newStorage->getBackendOptions()['password']); + $this->assertEquals([$this->userId], $newStorage->getApplicableUsers()); + // these attributes are unused for user storages + $this->assertEmpty($newStorage->getApplicableGroups()); + $this->assertEquals(0, $newStorage->getStatus()); + + // no hook calls + $this->assertEmpty(self::$hookCalls); + } + + /** + * @dataProvider deleteStorageDataProvider + */ + public function testDeleteStorage($backendOptions, $rustyStorageId, $expectedCountAfterDeletion) { + parent::testDeleteStorage($backendOptions, $rustyStorageId, $expectedCountAfterDeletion); + + // hook called once for user (first one was during test creation) + $this->assertHookCall( + self::$hookCalls[1], + Filesystem::signal_delete_mount, + '/mountpoint', + \OC_Mount_Config::MOUNT_TYPE_USER, + $this->userId + ); + } + + public function testHooksRenameMountPoint() { + $storage = $this->makeTestStorageData(); + $storage = $this->service->addStorage($storage); + + $storage->setMountPoint('renamedMountpoint'); + + // reset calls + self::$hookCalls = []; + + $this->service->updateStorage($storage); + + // hook called twice + $this->assertHookCall( + self::$hookCalls[0], + Filesystem::signal_delete_mount, + '/mountpoint', + \OC_Mount_Config::MOUNT_TYPE_USER, + $this->userId + ); + $this->assertHookCall( + self::$hookCalls[1], + Filesystem::signal_create_mount, + '/renamedMountpoint', + \OC_Mount_Config::MOUNT_TYPE_USER, + $this->userId + ); + } + + /** + * @expectedException \OCA\Files_external\NotFoundException + */ + public function testGetAdminStorage() { + $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB'); + $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism'); + + $storage = new StorageConfig(); + $storage->setMountPoint('mountpoint'); + $storage->setBackend($backend); + $storage->setAuthMechanism($authMechanism); + $storage->setBackendOptions(['password' => 'testPassword']); + $storage->setApplicableUsers([$this->userId]); + + $newStorage = $this->globalStoragesService->addStorage($storage); + + $this->assertInstanceOf('\OCA\Files_external\Lib\StorageConfig', $this->globalStoragesService->getStorage($newStorage->getId())); + + $this->service->getStorage($newStorage->getId()); + } +} diff --git a/apps/files_external/tests/command/applicabletest.php b/apps/files_external/tests/command/applicabletest.php deleted file mode 100644 index 64d41f6f245..00000000000 --- a/apps/files_external/tests/command/applicabletest.php +++ /dev/null @@ -1,168 +0,0 @@ - - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_External\Tests\Command; - -use OCA\Files_External\Command\Applicable; - -class ApplicableTest extends CommandTest { - private function getInstance($storageService) { - /** @var \OCP\IUserManager|\PHPUnit_Framework_MockObject_MockObject $userManager */ - $userManager = $this->getMock('\OCP\IUserManager'); - /** @var \OCP\IGroupManager|\PHPUnit_Framework_MockObject_MockObject $groupManager */ - $groupManager = $this->getMock('\OCP\IGroupManager'); - - $userManager->expects($this->any()) - ->method('userExists') - ->will($this->returnValue(true)); - - $groupManager->expects($this->any()) - ->method('groupExists') - ->will($this->returnValue(true)); - - return new Applicable($storageService, $userManager, $groupManager); - } - - public function testListEmpty() { - $mount = $this->getMount(1, '', ''); - - $storageService = $this->getGlobalStorageService([$mount]); - $command = $this->getInstance($storageService); - - $input = $this->getInput($command, [ - 'mount_id' => 1 - ], [ - 'output' => 'json' - ]); - - $result = json_decode($this->executeCommand($command, $input), true); - - $this->assertEquals(['users' => [], 'groups' => []], $result); - } - - public function testList() { - $mount = $this->getMount(1, '', '', '', [], [], ['test', 'asd']); - - $storageService = $this->getGlobalStorageService([$mount]); - $command = $this->getInstance($storageService); - - $input = $this->getInput($command, [ - 'mount_id' => 1 - ], [ - 'output' => 'json' - ]); - - $result = json_decode($this->executeCommand($command, $input), true); - - $this->assertEquals(['users' => ['test', 'asd'], 'groups' => []], $result); - } - - public function testAddSingle() { - $mount = $this->getMount(1, '', '', '', [], [], []); - - $storageService = $this->getGlobalStorageService([$mount]); - $command = $this->getInstance($storageService); - - $input = $this->getInput($command, [ - 'mount_id' => 1 - ], [ - 'output' => 'json', - 'add-user' => ['foo'] - ]); - - $this->executeCommand($command, $input); - - $this->assertEquals(['foo'], $mount->getApplicableUsers()); - } - - public function testAddDuplicate() { - $mount = $this->getMount(1, '', '', '', [], [], ['foo']); - - $storageService = $this->getGlobalStorageService([$mount]); - $command = $this->getInstance($storageService); - - $input = $this->getInput($command, [ - 'mount_id' => 1 - ], [ - 'output' => 'json', - 'add-user' => ['foo', 'bar'] - ]); - - $this->executeCommand($command, $input); - - $this->assertEquals(['foo', 'bar'], $mount->getApplicableUsers()); - } - - public function testRemoveSingle() { - $mount = $this->getMount(1, '', '', '', [], [], ['foo', 'bar']); - - $storageService = $this->getGlobalStorageService([$mount]); - $command = $this->getInstance($storageService); - - $input = $this->getInput($command, [ - 'mount_id' => 1 - ], [ - 'output' => 'json', - 'remove-user' => ['bar'] - ]); - - $this->executeCommand($command, $input); - - $this->assertEquals(['foo'], $mount->getApplicableUsers()); - } - - public function testRemoveNonExisting() { - $mount = $this->getMount(1, '', '', '', [], [], ['foo', 'bar']); - - $storageService = $this->getGlobalStorageService([$mount]); - $command = $this->getInstance($storageService); - - $input = $this->getInput($command, [ - 'mount_id' => 1 - ], [ - 'output' => 'json', - 'remove-user' => ['bar', 'asd'] - ]); - - $this->executeCommand($command, $input); - - $this->assertEquals(['foo'], $mount->getApplicableUsers()); - } - - public function testRemoveAddRemove() { - $mount = $this->getMount(1, '', '', '', [], [], ['foo', 'bar']); - - $storageService = $this->getGlobalStorageService([$mount]); - $command = $this->getInstance($storageService); - - $input = $this->getInput($command, [ - 'mount_id' => 1 - ], [ - 'output' => 'json', - 'remove-user' => ['bar', 'asd'], - 'add-user' => ['test'] - ]); - - $this->executeCommand($command, $input); - - $this->assertEquals(['foo', 'test'], $mount->getApplicableUsers()); - } -} diff --git a/apps/files_external/tests/command/commandtest.php b/apps/files_external/tests/command/commandtest.php deleted file mode 100644 index 9a0afbd3681..00000000000 --- a/apps/files_external/tests/command/commandtest.php +++ /dev/null @@ -1,104 +0,0 @@ - - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_External\Tests\Command; - -use OCA\Files_external\Lib\StorageConfig; -use OCA\Files_external\NotFoundException; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\ArrayInput; -use Symfony\Component\Console\Input\Input; -use Symfony\Component\Console\Output\BufferedOutput; -use Test\TestCase; - -abstract class CommandTest extends TestCase { - /** - * @param StorageConfig[] $mounts - * @return \OCA\Files_external\Service\GlobalStoragesService|\PHPUnit_Framework_MockObject_MockObject - */ - protected function getGlobalStorageService(array $mounts = []) { - $mock = $this->getMockBuilder('OCA\Files_external\Service\GlobalStoragesService') - ->disableOriginalConstructor() - ->getMock(); - - $this->bindMounts($mock, $mounts); - - return $mock; - } - - /** - * @param \PHPUnit_Framework_MockObject_MockObject $mock - * @param StorageConfig[] $mounts - */ - protected function bindMounts(\PHPUnit_Framework_MockObject_MockObject $mock, array $mounts) { - $mock->expects($this->any()) - ->method('getStorage') - ->will($this->returnCallback(function ($id) use ($mounts) { - foreach ($mounts as $mount) { - if ($mount->getId() === $id) { - return $mount; - } - } - throw new NotFoundException(); - })); - } - - /** - * @param $id - * @param $mountPoint - * @param $backendClass - * @param string $applicableIdentifier - * @param array $config - * @param array $options - * @param array $users - * @param array $groups - * @return StorageConfig - */ - protected function getMount($id, $mountPoint, $backendClass, $applicableIdentifier = 'password::password', $config = [], $options = [], $users = [], $groups = []) { - $mount = new StorageConfig($id); - - $mount->setMountPoint($mountPoint); - $mount->setBackendOptions($config); - $mount->setMountOptions($options); - $mount->setApplicableUsers($users); - $mount->setApplicableGroups($groups); - - return $mount; - } - - protected function getInput(Command $command, array $arguments = [], array $options = []) { - $input = new ArrayInput([]); - $input->bind($command->getDefinition()); - foreach ($arguments as $key => $value) { - $input->setArgument($key, $value); - } - foreach ($options as $key => $value) { - $input->setOption($key, $value); - } - return $input; - } - - protected function executeCommand(Command $command, Input $input) { - $output = new BufferedOutput(); - $this->invokePrivate($command, 'execute', [$input, $output]); - return $output->fetch(); - } -} diff --git a/apps/files_external/tests/command/listcommandtest.php b/apps/files_external/tests/command/listcommandtest.php deleted file mode 100644 index 3098906bcc6..00000000000 --- a/apps/files_external/tests/command/listcommandtest.php +++ /dev/null @@ -1,70 +0,0 @@ - - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_External\Tests\Command; - -use OCA\Files_External\Command\ListCommand; -use OCA\Files_External\Lib\Auth\NullMechanism; -use OCA\Files_External\Lib\Auth\Password\Password; -use OCA\Files_External\Lib\Auth\Password\SessionCredentials; -use OCA\Files_External\Lib\Backend\Local; -use OCA\Files_external\Lib\StorageConfig; -use Symfony\Component\Console\Output\BufferedOutput; - -class ListCommandTest extends CommandTest { - /** - * @return \OCA\Files_External\Command\ListCommand|\PHPUnit_Framework_MockObject_MockObject - */ - private function getInstance() { - /** @var \OCA\Files_external\Service\GlobalStoragesService|\PHPUnit_Framework_MockObject_MockObject $globalService */ - $globalService = $this->getMock('\OCA\Files_external\Service\GlobalStoragesService', null, [], '', false); - /** @var \OCA\Files_external\Service\UserStoragesService|\PHPUnit_Framework_MockObject_MockObject $userService */ - $userService = $this->getMock('\OCA\Files_external\Service\UserStoragesService', null, [], '', false); - /** @var \OCP\IUserManager|\PHPUnit_Framework_MockObject_MockObject $userManager */ - $userManager = $this->getMock('\OCP\IUserManager'); - /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject $userSession */ - $userSession = $this->getMock('\OCP\IUserSession'); - - return new ListCommand($globalService, $userService, $userSession, $userManager); - } - - public function testListAuthIdentifier() { - $l10n = $this->getMock('\OC_L10N', null, [], '', false); - $session = $this->getMock('\OCP\ISession'); - $crypto = $this->getMock('\OCP\Security\ICrypto'); - $instance = $this->getInstance(); - $mount1 = new StorageConfig(); - $mount1->setAuthMechanism(new Password($l10n)); - $mount1->setBackend(new Local($l10n, new NullMechanism($l10n))); - $mount2 = new StorageConfig(); - $mount2->setAuthMechanism(new SessionCredentials($l10n, $session, $crypto)); - $mount2->setBackend(new Local($l10n, new NullMechanism($l10n))); - $input = $this->getInput($instance, [], [ - 'output' => 'json' - ]); - $output = new BufferedOutput(); - - $instance->listMounts('', [$mount1, $mount2], $input, $output); - $output = json_decode($output->fetch(), true); - - $this->assertNotEquals($output[0]['authentication_type'], $output[1]['authentication_type']); - } -} diff --git a/apps/files_external/tests/controller/globalstoragescontrollertest.php b/apps/files_external/tests/controller/globalstoragescontrollertest.php deleted file mode 100644 index 1021300399a..00000000000 --- a/apps/files_external/tests/controller/globalstoragescontrollertest.php +++ /dev/null @@ -1,49 +0,0 @@ - - * @author Robin McCorkell - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, 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 - * - */ -namespace OCA\Files_external\Tests\Controller; - -use \OCA\Files_external\Controller\GlobalStoragesController; -use \OCA\Files_external\Service\GlobalStoragesService; -use \OCP\AppFramework\Http; -use \OCA\Files_external\NotFoundException; -use \OCA\Files_External\Service\BackendService; - -class GlobalStoragesControllerTest extends StoragesControllerTest { - public function setUp() { - parent::setUp(); - $this->service = $this->getMockBuilder('\OCA\Files_external\Service\GlobalStoragesService') - ->disableOriginalConstructor() - ->getMock(); - - $this->service->method('getVisibilityType') - ->willReturn(BackendService::VISIBILITY_ADMIN); - - $this->controller = new GlobalStoragesController( - 'files_external', - $this->getMock('\OCP\IRequest'), - $this->getMock('\OCP\IL10N'), - $this->service, - $this->getMock('\OCP\ILogger') - ); - } -} diff --git a/apps/files_external/tests/controller/storagescontrollertest.php b/apps/files_external/tests/controller/storagescontrollertest.php deleted file mode 100644 index 4c3c62d5d6f..00000000000 --- a/apps/files_external/tests/controller/storagescontrollertest.php +++ /dev/null @@ -1,394 +0,0 @@ - - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, 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 - * - */ -namespace OCA\Files_external\Tests\Controller; - -use \OCP\AppFramework\Http; - -use \OCA\Files_external\Controller\GlobalStoragesController; -use \OCA\Files_external\Service\GlobalStoragesService; -use \OCA\Files_external\Lib\StorageConfig; -use \OCA\Files_external\NotFoundException; - -abstract class StoragesControllerTest extends \Test\TestCase { - - /** - * @var GlobalStoragesController - */ - protected $controller; - - /** - * @var GlobalStoragesService - */ - protected $service; - - public function setUp() { - \OC_Mount_Config::$skipTest = true; - } - - public function tearDown() { - \OC_Mount_Config::$skipTest = false; - } - - /** - * @return \OCA\Files_External\Lib\Backend\Backend - */ - protected function getBackendMock($class = '\OCA\Files_External\Lib\Backend\SMB', $storageClass = '\OCA\Files_External\Lib\Storage\SMB') { - $backend = $this->getMockBuilder('\OCA\Files_External\Lib\Backend\Backend') - ->disableOriginalConstructor() - ->getMock(); - $backend->method('getStorageClass') - ->willReturn($storageClass); - $backend->method('getIdentifier') - ->willReturn('identifier:'.$class); - return $backend; - } - - /** - * @return \OCA\Files_External\Lib\Auth\AuthMechanism - */ - protected function getAuthMechMock($scheme = 'null', $class = '\OCA\Files_External\Lib\Auth\NullMechanism') { - $authMech = $this->getMockBuilder('\OCA\Files_External\Lib\Auth\AuthMechanism') - ->disableOriginalConstructor() - ->getMock(); - $authMech->method('getScheme') - ->willReturn($scheme); - $authMech->method('getIdentifier') - ->willReturn('identifier:'.$class); - - return $authMech; - } - - public function testAddStorage() { - $authMech = $this->getAuthMechMock(); - $authMech->method('validateStorage') - ->willReturn(true); - $authMech->method('isVisibleFor') - ->willReturn(true); - $backend = $this->getBackendMock(); - $backend->method('validateStorage') - ->willReturn(true); - $backend->method('isVisibleFor') - ->willReturn(true); - - $storageConfig = new StorageConfig(1); - $storageConfig->setMountPoint('mount'); - $storageConfig->setBackend($backend); - $storageConfig->setAuthMechanism($authMech); - $storageConfig->setBackendOptions([]); - - $this->service->expects($this->once()) - ->method('createStorage') - ->will($this->returnValue($storageConfig)); - $this->service->expects($this->once()) - ->method('addStorage') - ->will($this->returnValue($storageConfig)); - - $response = $this->controller->create( - 'mount', - '\OCA\Files_External\Lib\Storage\SMB', - '\OCA\Files_External\Lib\Auth\NullMechanism', - array(), - [], - [], - [], - null - ); - - $data = $response->getData(); - $this->assertEquals(Http::STATUS_CREATED, $response->getStatus()); - $this->assertEquals($storageConfig, $data); - } - - public function testUpdateStorage() { - $authMech = $this->getAuthMechMock(); - $authMech->method('validateStorage') - ->willReturn(true); - $authMech->method('isVisibleFor') - ->willReturn(true); - $backend = $this->getBackendMock(); - $backend->method('validateStorage') - ->willReturn(true); - $backend->method('isVisibleFor') - ->willReturn(true); - - $storageConfig = new StorageConfig(1); - $storageConfig->setMountPoint('mount'); - $storageConfig->setBackend($backend); - $storageConfig->setAuthMechanism($authMech); - $storageConfig->setBackendOptions([]); - - $this->service->expects($this->once()) - ->method('createStorage') - ->will($this->returnValue($storageConfig)); - $this->service->expects($this->once()) - ->method('updateStorage') - ->will($this->returnValue($storageConfig)); - - $response = $this->controller->update( - 1, - 'mount', - '\OCA\Files_External\Lib\Storage\SMB', - '\OCA\Files_External\Lib\Auth\NullMechanism', - array(), - [], - [], - [], - null - ); - - $data = $response->getData(); - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); - $this->assertEquals($storageConfig, $data); - } - - function mountPointNamesProvider() { - return array( - array(''), - array('/'), - array('//'), - ); - } - - /** - * @dataProvider mountPointNamesProvider - */ - public function testAddOrUpdateStorageInvalidMountPoint($mountPoint) { - $storageConfig = new StorageConfig(1); - $storageConfig->setMountPoint($mountPoint); - $storageConfig->setBackend($this->getBackendMock()); - $storageConfig->setAuthMechanism($this->getAuthMechMock()); - $storageConfig->setBackendOptions([]); - - $this->service->expects($this->exactly(2)) - ->method('createStorage') - ->will($this->returnValue($storageConfig)); - $this->service->expects($this->never()) - ->method('addStorage'); - $this->service->expects($this->never()) - ->method('updateStorage'); - - $response = $this->controller->create( - $mountPoint, - '\OCA\Files_External\Lib\Storage\SMB', - '\OCA\Files_External\Lib\Auth\NullMechanism', - array(), - [], - [], - [], - null - ); - - $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus()); - - $response = $this->controller->update( - 1, - $mountPoint, - '\OCA\Files_External\Lib\Storage\SMB', - '\OCA\Files_External\Lib\Auth\NullMechanism', - array(), - [], - [], - [], - null - ); - - $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus()); - } - - public function testAddOrUpdateStorageInvalidBackend() { - $this->service->expects($this->exactly(2)) - ->method('createStorage') - ->will($this->throwException(new \InvalidArgumentException())); - $this->service->expects($this->never()) - ->method('addStorage'); - $this->service->expects($this->never()) - ->method('updateStorage'); - - $response = $this->controller->create( - 'mount', - '\OC\Files\Storage\InvalidStorage', - '\OCA\Files_External\Lib\Auth\NullMechanism', - array(), - [], - [], - [], - null - ); - - $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus()); - - $response = $this->controller->update( - 1, - 'mount', - '\OC\Files\Storage\InvalidStorage', - '\OCA\Files_External\Lib\Auth\NullMechanism', - array(), - [], - [], - [], - null - ); - - $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus()); - } - - public function testUpdateStorageNonExisting() { - $authMech = $this->getAuthMechMock(); - $authMech->method('validateStorage') - ->willReturn(true); - $authMech->method('isVisibleFor') - ->willReturn(true); - $backend = $this->getBackendMock(); - $backend->method('validateStorage') - ->willReturn(true); - $backend->method('isVisibleFor') - ->willReturn(true); - - $storageConfig = new StorageConfig(255); - $storageConfig->setMountPoint('mount'); - $storageConfig->setBackend($backend); - $storageConfig->setAuthMechanism($authMech); - $storageConfig->setBackendOptions([]); - - $this->service->expects($this->once()) - ->method('createStorage') - ->will($this->returnValue($storageConfig)); - $this->service->expects($this->once()) - ->method('updateStorage') - ->will($this->throwException(new NotFoundException())); - - $response = $this->controller->update( - 255, - 'mount', - '\OCA\Files_External\Lib\Storage\SMB', - '\OCA\Files_External\Lib\Auth\NullMechanism', - array(), - [], - [], - [], - null - ); - - $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); - } - - public function testDeleteStorage() { - $this->service->expects($this->once()) - ->method('removeStorage'); - - $response = $this->controller->destroy(1); - $this->assertEquals(Http::STATUS_NO_CONTENT, $response->getStatus()); - } - - public function testDeleteStorageNonExisting() { - $this->service->expects($this->once()) - ->method('removeStorage') - ->will($this->throwException(new NotFoundException())); - - $response = $this->controller->destroy(255); - $this->assertEquals(Http::STATUS_NOT_FOUND, $response->getStatus()); - } - - public function testGetStorage() { - $backend = $this->getBackendMock(); - $authMech = $this->getAuthMechMock(); - $storageConfig = new StorageConfig(1); - $storageConfig->setMountPoint('test'); - $storageConfig->setBackend($backend); - $storageConfig->setAuthMechanism($authMech); - $storageConfig->setBackendOptions(['user' => 'test', 'password', 'password123']); - $storageConfig->setMountOptions(['priority' => false]); - - $this->service->expects($this->once()) - ->method('getStorage') - ->with(1) - ->will($this->returnValue($storageConfig)); - $response = $this->controller->show(1); - - $this->assertEquals(Http::STATUS_OK, $response->getStatus()); - $this->assertEquals($storageConfig, $response->getData()); - } - - public function validateStorageProvider() { - return [ - [true, true, true], - [false, true, false], - [true, false, false], - [false, false, false] - ]; - } - - /** - * @dataProvider validateStorageProvider - */ - public function testValidateStorage($backendValidate, $authMechValidate, $expectSuccess) { - $backend = $this->getBackendMock(); - $backend->method('validateStorage') - ->willReturn($backendValidate); - $backend->method('isVisibleFor') - ->willReturn(true); - - $authMech = $this->getAuthMechMock(); - $authMech->method('validateStorage') - ->will($this->returnValue($authMechValidate)); - $authMech->method('isVisibleFor') - ->willReturn(true); - - $storageConfig = new StorageConfig(); - $storageConfig->setMountPoint('mount'); - $storageConfig->setBackend($backend); - $storageConfig->setAuthMechanism($authMech); - $storageConfig->setBackendOptions([]); - - $this->service->expects($this->once()) - ->method('createStorage') - ->will($this->returnValue($storageConfig)); - - if ($expectSuccess) { - $this->service->expects($this->once()) - ->method('addStorage') - ->with($storageConfig) - ->will($this->returnValue($storageConfig)); - } else { - $this->service->expects($this->never()) - ->method('addStorage'); - } - - $response = $this->controller->create( - 'mount', - '\OCA\Files_External\Lib\Storage\SMB', - '\OCA\Files_External\Lib\Auth\NullMechanism', - array(), - [], - [], - [], - null - ); - - if ($expectSuccess) { - $this->assertEquals(Http::STATUS_CREATED, $response->getStatus()); - } else { - $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus()); - } - } - -} diff --git a/apps/files_external/tests/controller/userstoragescontrollertest.php b/apps/files_external/tests/controller/userstoragescontrollertest.php deleted file mode 100644 index 804b752b6af..00000000000 --- a/apps/files_external/tests/controller/userstoragescontrollertest.php +++ /dev/null @@ -1,107 +0,0 @@ - - * @author Robin McCorkell - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, 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 - * - */ -namespace OCA\Files_external\Tests\Controller; - -use \OCA\Files_external\Controller\UserStoragesController; -use \OCA\Files_external\Service\UserStoragesService; -use \OCP\AppFramework\Http; -use \OCA\Files_external\NotFoundException; -use \OCA\Files_External\Lib\StorageConfig; -use \OCA\Files_External\Service\BackendService; - -class UserStoragesControllerTest extends StoragesControllerTest { - - /** - * @var array - */ - private $oldAllowedBackends; - - public function setUp() { - parent::setUp(); - $this->service = $this->getMockBuilder('\OCA\Files_external\Service\UserStoragesService') - ->disableOriginalConstructor() - ->getMock(); - - $this->service->method('getVisibilityType') - ->willReturn(BackendService::VISIBILITY_PERSONAL); - - $this->controller = new UserStoragesController( - 'files_external', - $this->getMock('\OCP\IRequest'), - $this->getMock('\OCP\IL10N'), - $this->service, - $this->getMock('\OCP\IUserSession'), - $this->getMock('\OCP\ILogger') - ); - } - - public function testAddOrUpdateStorageDisallowedBackend() { - $backend = $this->getBackendMock(); - $backend->method('isVisibleFor') - ->with(BackendService::VISIBILITY_PERSONAL) - ->willReturn(false); - $authMech = $this->getAuthMechMock(); - - $storageConfig = new StorageConfig(1); - $storageConfig->setMountPoint('mount'); - $storageConfig->setBackend($backend); - $storageConfig->setAuthMechanism($authMech); - $storageConfig->setBackendOptions([]); - - $this->service->expects($this->exactly(2)) - ->method('createStorage') - ->will($this->returnValue($storageConfig)); - $this->service->expects($this->never()) - ->method('addStorage'); - $this->service->expects($this->never()) - ->method('updateStorage'); - - $response = $this->controller->create( - 'mount', - '\OCA\Files_External\Lib\Storage\SMB', - '\Auth\Mechanism', - array(), - [], - [], - [], - null - ); - - $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus()); - - $response = $this->controller->update( - 1, - 'mount', - '\OCA\Files_External\Lib\Storage\SMB', - '\Auth\Mechanism', - array(), - [], - [], - [], - null - ); - - $this->assertEquals(Http::STATUS_UNPROCESSABLE_ENTITY, $response->getStatus()); - } - -} diff --git a/apps/files_external/tests/service/backendservicetest.php b/apps/files_external/tests/service/backendservicetest.php deleted file mode 100644 index ba9a1f533f2..00000000000 --- a/apps/files_external/tests/service/backendservicetest.php +++ /dev/null @@ -1,211 +0,0 @@ - - * - * @copyright Copyright (c) 2016, 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 - * - */ -namespace OCA\Files_External\Tests\Service; - -use \OCA\Files_External\Service\BackendService; - -class BackendServiceTest extends \Test\TestCase { - - /** @var \OCP\IConfig */ - protected $config; - - /** @var \OCP\IL10N */ - protected $l10n; - - protected function setUp() { - $this->config = $this->getMock('\OCP\IConfig'); - $this->l10n = $this->getMock('\OCP\IL10N'); - } - - /** - * @param string $class - * - * @return \OCA\Files_External\Lib\Backend\Backend - */ - protected function getBackendMock($class) { - $backend = $this->getMockBuilder('\OCA\Files_External\Lib\Backend\Backend') - ->disableOriginalConstructor() - ->getMock(); - $backend->method('getIdentifier')->will($this->returnValue('identifier:'.$class)); - $backend->method('getIdentifierAliases')->will($this->returnValue(['identifier:'.$class])); - return $backend; - } - - /** - * @param string $class - * - * @return \OCA\Files_External\Lib\Auth\AuthMechanism - */ - protected function getAuthMechanismMock($class) { - $backend = $this->getMockBuilder('\OCA\Files_External\Lib\Auth\AuthMechanism') - ->disableOriginalConstructor() - ->getMock(); - $backend->method('getIdentifier')->will($this->returnValue('identifier:'.$class)); - $backend->method('getIdentifierAliases')->will($this->returnValue(['identifier:'.$class])); - return $backend; - } - - public function testRegisterBackend() { - $service = new BackendService($this->config, $this->l10n); - - $backend = $this->getBackendMock('\Foo\Bar'); - - $backendAlias = $this->getMockBuilder('\OCA\Files_External\Lib\Backend\Backend') - ->disableOriginalConstructor() - ->getMock(); - $backendAlias->method('getIdentifierAliases') - ->willReturn(['identifier_real', 'identifier_alias']); - $backendAlias->method('getIdentifier') - ->willReturn('identifier_real'); - - $service->registerBackend($backend); - $service->registerBackend($backendAlias); - - $this->assertEquals($backend, $service->getBackend('identifier:\Foo\Bar')); - $this->assertEquals($backendAlias, $service->getBackend('identifier_real')); - $this->assertEquals($backendAlias, $service->getBackend('identifier_alias')); - - $backends = $service->getBackends(); - $this->assertCount(2, $backends); - $this->assertArrayHasKey('identifier:\Foo\Bar', $backends); - $this->assertArrayHasKey('identifier_real', $backends); - $this->assertArrayNotHasKey('identifier_alias', $backends); - } - - public function testBackendProvider() { - $service = new BackendService($this->config, $this->l10n); - - $backend1 = $this->getBackendMock('\Foo\Bar'); - $backend2 = $this->getBackendMock('\Bar\Foo'); - - $providerMock = $this->getMock('\OCA\Files_External\Lib\Config\IBackendProvider'); - $providerMock->expects($this->once()) - ->method('getBackends') - ->willReturn([$backend1, $backend2]); - $service->registerBackendProvider($providerMock); - - $this->assertEquals($backend1, $service->getBackend('identifier:\Foo\Bar')); - $this->assertEquals($backend2, $service->getBackend('identifier:\Bar\Foo')); - - $this->assertCount(2, $service->getBackends()); - } - - public function testAuthMechanismProvider() { - $service = new BackendService($this->config, $this->l10n); - - $backend1 = $this->getAuthMechanismMock('\Foo\Bar'); - $backend2 = $this->getAuthMechanismMock('\Bar\Foo'); - - $providerMock = $this->getMock('\OCA\Files_External\Lib\Config\IAuthMechanismProvider'); - $providerMock->expects($this->once()) - ->method('getAuthMechanisms') - ->willReturn([$backend1, $backend2]); - $service->registerAuthMechanismProvider($providerMock); - - $this->assertEquals($backend1, $service->getAuthMechanism('identifier:\Foo\Bar')); - $this->assertEquals($backend2, $service->getAuthMechanism('identifier:\Bar\Foo')); - - $this->assertCount(2, $service->getAuthMechanisms()); - } - - public function testMultipleBackendProviders() { - $service = new BackendService($this->config, $this->l10n); - - $backend1a = $this->getBackendMock('\Foo\Bar'); - $backend1b = $this->getBackendMock('\Bar\Foo'); - - $backend2 = $this->getBackendMock('\Dead\Beef'); - - $provider1Mock = $this->getMock('\OCA\Files_External\Lib\Config\IBackendProvider'); - $provider1Mock->expects($this->once()) - ->method('getBackends') - ->willReturn([$backend1a, $backend1b]); - $service->registerBackendProvider($provider1Mock); - $provider2Mock = $this->getMock('\OCA\Files_External\Lib\Config\IBackendProvider'); - $provider2Mock->expects($this->once()) - ->method('getBackends') - ->willReturn([$backend2]); - $service->registerBackendProvider($provider2Mock); - - $this->assertEquals($backend1a, $service->getBackend('identifier:\Foo\Bar')); - $this->assertEquals($backend1b, $service->getBackend('identifier:\Bar\Foo')); - $this->assertEquals($backend2, $service->getBackend('identifier:\Dead\Beef')); - - $this->assertCount(3, $service->getBackends()); - } - - public function testUserMountingBackends() { - $this->config->expects($this->exactly(2)) - ->method('getAppValue') - ->will($this->returnValueMap([ - ['files_external', 'allow_user_mounting', 'yes', 'yes'], - ['files_external', 'user_mounting_backends', '', 'identifier:\User\Mount\Allowed,identifier_alias'] - ])); - - $service = new BackendService($this->config, $this->l10n); - - $backendAllowed = $this->getBackendMock('\User\Mount\Allowed'); - $backendAllowed->expects($this->never()) - ->method('removeVisibility'); - $backendNotAllowed = $this->getBackendMock('\User\Mount\NotAllowed'); - $backendNotAllowed->expects($this->once()) - ->method('removeVisibility') - ->with(BackendService::VISIBILITY_PERSONAL); - - $backendAlias = $this->getMockBuilder('\OCA\Files_External\Lib\Backend\Backend') - ->disableOriginalConstructor() - ->getMock(); - $backendAlias->method('getIdentifierAliases') - ->willReturn(['identifier_real', 'identifier_alias']); - $backendAlias->expects($this->never()) - ->method('removeVisibility'); - - $service->registerBackend($backendAllowed); - $service->registerBackend($backendNotAllowed); - $service->registerBackend($backendAlias); - } - - public function testGetAvailableBackends() { - $service = new BackendService($this->config, $this->l10n); - - $backendAvailable = $this->getBackendMock('\Backend\Available'); - $backendAvailable->expects($this->once()) - ->method('checkDependencies') - ->will($this->returnValue([])); - $backendNotAvailable = $this->getBackendMock('\Backend\NotAvailable'); - $backendNotAvailable->expects($this->once()) - ->method('checkDependencies') - ->will($this->returnValue([ - $this->getMockBuilder('\OCA\Files_External\Lib\MissingDependency') - ->disableOriginalConstructor() - ->getMock() - ])); - - $service->registerBackend($backendAvailable); - $service->registerBackend($backendNotAvailable); - - $availableBackends = $service->getAvailableBackends(); - $this->assertArrayHasKey('identifier:\Backend\Available', $availableBackends); - $this->assertArrayNotHasKey('identifier:\Backend\NotAvailable', $availableBackends); - } - -} - diff --git a/apps/files_external/tests/service/dbconfigservicetest.php b/apps/files_external/tests/service/dbconfigservicetest.php deleted file mode 100644 index b088a7078d1..00000000000 --- a/apps/files_external/tests/service/dbconfigservicetest.php +++ /dev/null @@ -1,285 +0,0 @@ - - * @author Robin McCorkell - * - * @copyright Copyright (c) 2016, 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 - * - */ - -namespace OCA\Files_External\Tests\Service; - - -use OCA\Files_External\Service\DBConfigService; -use OCP\IDBConnection; -use Test\TestCase; - -/** - * @group DB - */ -class DBConfigServiceTest extends TestCase { - /** - * @var DBConfigService - */ - private $dbConfig; - - /** - * @var IDBConnection - */ - private $connection; - - private $mounts = []; - - public function setUp() { - parent::setUp(); - $this->connection = \OC::$server->getDatabaseConnection(); - $this->dbConfig = new DBConfigService($this->connection, \OC::$server->getCrypto()); - } - - public function tearDown() { - foreach ($this->mounts as $mount) { - $this->dbConfig->removeMount($mount); - } - $this->mounts = []; - } - - private function addMount($mountPoint, $storageBackend, $authBackend, $priority, $type) { - $id = $this->dbConfig->addMount($mountPoint, $storageBackend, $authBackend, $priority, $type); - $this->mounts[] = $id; - return $id; - } - - public function testAddSimpleMount() { - $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); - - $mount = $this->dbConfig->getMountById($id); - $this->assertEquals('/test', $mount['mount_point']); - $this->assertEquals('foo', $mount['storage_backend']); - $this->assertEquals('bar', $mount['auth_backend']); - $this->assertEquals(100, $mount['priority']); - $this->assertEquals(DBConfigService::MOUNT_TYPE_ADMIN, $mount['type']); - $this->assertEquals([], $mount['applicable']); - $this->assertEquals([], $mount['config']); - $this->assertEquals([], $mount['options']); - } - - public function testAddApplicable() { - $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); - $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test'); - - $mount = $this->dbConfig->getMountById($id); - $this->assertEquals([ - ['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id] - ], $mount['applicable']); - - $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, 'bar'); - $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); - - $mount = $this->dbConfig->getMountById($id); - $this->assertEquals([ - ['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id], - ['type' => DBConfigService::APPLICABLE_TYPE_GROUP, 'value' => 'bar', 'mount_id' => $id], - ['type' => DBConfigService::APPLICABLE_TYPE_GLOBAL, 'value' => null, 'mount_id' => $id] - ], $mount['applicable']); - } - - public function testAddApplicableDouble() { - $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); - $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test'); - $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test'); - - $mount = $this->dbConfig->getMountById($id); - $this->assertEquals([ - ['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id] - ], $mount['applicable']); - } - - public function testDeleteMount() { - $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); - - $this->dbConfig->removeMount($id); - - $mount = $this->dbConfig->getMountById($id); - $this->assertEquals(null, $mount); - } - - public function testRemoveApplicable() { - $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); - $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test'); - $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test'); - - $mount = $this->dbConfig->getMountById($id); - $this->assertEquals([], $mount['applicable']); - } - - public function testRemoveApplicableGlobal() { - $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); - $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); - $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); - $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test'); - - $mount = $this->dbConfig->getMountById($id); - $this->assertEquals([ - ['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id] - ], $mount['applicable']); - } - - public function testSetConfig() { - $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); - $this->dbConfig->setConfig($id, 'foo', 'bar'); - - $mount = $this->dbConfig->getMountById($id); - $this->assertEquals(['foo' => 'bar'], $mount['config']); - - $this->dbConfig->setConfig($id, 'foo2', 'bar2'); - - $mount = $this->dbConfig->getMountById($id); - $this->assertEquals(['foo' => 'bar', 'foo2' => 'bar2'], $mount['config']); - } - - public function testSetConfigOverwrite() { - $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); - $this->dbConfig->setConfig($id, 'foo', 'bar'); - $this->dbConfig->setConfig($id, 'asd', '1'); - $this->dbConfig->setConfig($id, 'foo', 'qwerty'); - - $mount = $this->dbConfig->getMountById($id); - $this->assertEquals(['foo' => 'qwerty', 'asd' => '1'], $mount['config']); - } - - public function testSetOption() { - $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); - $this->dbConfig->setOption($id, 'foo', 'bar'); - - $mount = $this->dbConfig->getMountById($id); - $this->assertEquals(['foo' => 'bar'], $mount['options']); - - $this->dbConfig->setOption($id, 'foo2', 'bar2'); - - $mount = $this->dbConfig->getMountById($id); - $this->assertEquals(['foo' => 'bar', 'foo2' => 'bar2'], $mount['options']); - } - - public function testSetOptionOverwrite() { - $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); - $this->dbConfig->setOption($id, 'foo', 'bar'); - $this->dbConfig->setOption($id, 'asd', '1'); - $this->dbConfig->setOption($id, 'foo', 'qwerty'); - - $mount = $this->dbConfig->getMountById($id); - $this->assertEquals(['foo' => 'qwerty', 'asd' => '1'], $mount['options']); - } - - public function testGetMountsFor() { - $mounts = $this->dbConfig->getMountsFor(DBConfigService::APPLICABLE_TYPE_USER, 'test'); - $this->assertEquals([], $mounts); - - $id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); - $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test'); - - $mounts = $this->dbConfig->getMountsFor(DBConfigService::APPLICABLE_TYPE_USER, 'test'); - $this->assertCount(1, $mounts); - $this->assertEquals($id, $mounts[0]['mount_id']); - $this->assertEquals([['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id]], $mounts[0]['applicable']); - } - - public function testGetAdminMounts() { - $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); - $this->addMount('/test2', 'foo2', 'bar2', 100, DBConfigService::MOUNT_TYPE_PERSONAl); - - $mounts = $this->dbConfig->getAdminMounts(); - $this->assertCount(1, $mounts); - $this->assertEquals($id1, $mounts[0]['mount_id']); - } - - public function testGetAdminMountsFor() { - $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); - $this->addMount('/test2', 'foo2', 'bar2', 100, DBConfigService::MOUNT_TYPE_ADMIN); - $id3 = $this->addMount('/test3', 'foo3', 'bar3', 100, DBConfigService::MOUNT_TYPE_PERSONAl); - - $this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_USER, 'test'); - $this->dbConfig->addApplicable($id3, DBConfigService::APPLICABLE_TYPE_USER, 'test'); - - $mounts = $this->dbConfig->getAdminMountsFor(DBConfigService::APPLICABLE_TYPE_USER, 'test'); - $this->assertCount(1, $mounts); - $this->assertEquals($id1, $mounts[0]['mount_id']); - $this->assertEquals([['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id1]], $mounts[0]['applicable']); - } - - public function testGetUserMountsFor() { - $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); - $this->addMount('/test2', 'foo2', 'bar2', 100, DBConfigService::MOUNT_TYPE_PERSONAl); - $id3 = $this->addMount('/test3', 'foo3', 'bar3', 100, DBConfigService::MOUNT_TYPE_PERSONAl); - - $this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_USER, 'test'); - $this->dbConfig->addApplicable($id3, DBConfigService::APPLICABLE_TYPE_USER, 'test'); - - $mounts = $this->dbConfig->getUserMountsFor(DBConfigService::APPLICABLE_TYPE_USER, 'test'); - $this->assertCount(1, $mounts); - $this->assertEquals($id3, $mounts[0]['mount_id']); - $this->assertEquals([['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id3]], $mounts[0]['applicable']); - } - - public function testGetAdminMountsForGlobal() { - $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); - - $this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); - - $mounts = $this->dbConfig->getAdminMountsFor(DBConfigService::APPLICABLE_TYPE_GLOBAL, null); - $this->assertCount(1, $mounts); - $this->assertEquals($id1, $mounts[0]['mount_id']); - $this->assertEquals([['type' => DBConfigService::APPLICABLE_TYPE_GLOBAL, 'value' => null, 'mount_id' => $id1]], $mounts[0]['applicable']); - } - - public function testSetMountPoint() { - $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); - $id2 = $this->addMount('/foo', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); - - $this->dbConfig->setMountPoint($id1, '/asd'); - - $mount = $this->dbConfig->getMountById($id1); - $this->assertEquals('/asd', $mount['mount_point']); - - // remains unchanged - $mount = $this->dbConfig->getMountById($id2); - $this->assertEquals('/foo', $mount['mount_point']); - } - - public function testSetAuthBackend() { - $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); - $id2 = $this->addMount('/foo', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); - - $this->dbConfig->setAuthBackend($id1, 'none'); - - $mount = $this->dbConfig->getMountById($id1); - $this->assertEquals('none', $mount['auth_backend']); - - // remains unchanged - $mount = $this->dbConfig->getMountById($id2); - $this->assertEquals('bar', $mount['auth_backend']); - } - - public function testGetMountsForDuplicateByGroup() { - $id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN); - - $this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_GROUP, 'group1'); - $this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_GROUP, 'group2'); - - $mounts = $this->dbConfig->getAdminMountsForMultiple(DBConfigService::APPLICABLE_TYPE_GROUP, ['group1', 'group2']); - $this->assertCount(1, $mounts); - $this->assertEquals($id1, $mounts[0]['mount_id']); - } -} diff --git a/apps/files_external/tests/service/globalstoragesservicetest.php b/apps/files_external/tests/service/globalstoragesservicetest.php deleted file mode 100644 index 6cdfbef82d4..00000000000 --- a/apps/files_external/tests/service/globalstoragesservicetest.php +++ /dev/null @@ -1,629 +0,0 @@ - - * @author Robin McCorkell - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, 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 - * - */ -namespace OCA\Files_external\Tests\Service; - -use \OC\Files\Filesystem; - -use \OCA\Files_external\Service\GlobalStoragesService; -use \OCA\Files_external\NotFoundException; -use \OCA\Files_external\Lib\StorageConfig; - -/** - * @group DB - */ -class GlobalStoragesServiceTest extends StoragesServiceTest { - public function setUp() { - parent::setUp(); - $this->service = new GlobalStoragesService($this->backendService, $this->dbConfig, $this->mountCache); - } - - public function tearDown() { - @unlink($this->dataDir . '/mount.json'); - parent::tearDown(); - } - - protected function makeTestStorageData() { - return $this->makeStorageConfig([ - 'mountPoint' => 'mountpoint', - 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB', - 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism', - 'backendOptions' => [ - 'option1' => 'value1', - 'option2' => 'value2', - 'password' => 'testPassword', - ], - 'applicableUsers' => [], - 'applicableGroups' => [], - 'priority' => 15, - 'mountOptions' => [ - 'preview' => false, - ] - ]); - } - - function storageDataProvider() { - return [ - // all users - [ - [ - 'mountPoint' => 'mountpoint', - 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB', - 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism', - 'backendOptions' => [ - 'option1' => 'value1', - 'option2' => 'value2', - 'password' => 'testPassword', - ], - 'applicableUsers' => [], - 'applicableGroups' => [], - 'priority' => 15, - ], - ], - // some users - [ - [ - 'mountPoint' => 'mountpoint', - 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB', - 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism', - 'backendOptions' => [ - 'option1' => 'value1', - 'option2' => 'value2', - 'password' => 'testPassword', - ], - 'applicableUsers' => ['user1', 'user2'], - 'applicableGroups' => [], - 'priority' => 15, - ], - ], - // some groups - [ - [ - 'mountPoint' => 'mountpoint', - 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB', - 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism', - 'backendOptions' => [ - 'option1' => 'value1', - 'option2' => 'value2', - 'password' => 'testPassword', - ], - 'applicableUsers' => [], - 'applicableGroups' => ['group1', 'group2'], - 'priority' => 15, - ], - ], - // both users and groups - [ - [ - 'mountPoint' => 'mountpoint', - 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB', - 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism', - 'backendOptions' => [ - 'option1' => 'value1', - 'option2' => 'value2', - 'password' => 'testPassword', - ], - 'applicableUsers' => ['user1', 'user2'], - 'applicableGroups' => ['group1', 'group2'], - 'priority' => 15, - ], - ], - ]; - } - - /** - * @dataProvider storageDataProvider - */ - public function testAddStorage($storageParams) { - $storage = $this->makeStorageConfig($storageParams); - $newStorage = $this->service->addStorage($storage); - - $baseId = $newStorage->getId(); - - $newStorage = $this->service->getStorage($baseId); - - $this->assertEquals($storage->getMountPoint(), $newStorage->getMountPoint()); - $this->assertEquals($storage->getBackend(), $newStorage->getBackend()); - $this->assertEquals($storage->getAuthMechanism(), $newStorage->getAuthMechanism()); - $this->assertEquals($storage->getBackendOptions(), $newStorage->getBackendOptions()); - $this->assertEquals($storage->getApplicableUsers(), $newStorage->getApplicableUsers()); - $this->assertEquals($storage->getApplicableGroups(), $newStorage->getApplicableGroups()); - $this->assertEquals($storage->getPriority(), $newStorage->getPriority()); - $this->assertEquals(0, $newStorage->getStatus()); - - $nextStorage = $this->service->addStorage($storage); - $this->assertEquals($baseId + 1, $nextStorage->getId()); - } - - /** - * @dataProvider storageDataProvider - */ - public function testUpdateStorage($updatedStorageParams) { - $updatedStorage = $this->makeStorageConfig($updatedStorageParams); - $storage = $this->makeStorageConfig([ - 'mountPoint' => 'mountpoint', - 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB', - 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism', - 'backendOptions' => [ - 'option1' => 'value1', - 'option2' => 'value2', - 'password' => 'testPassword', - ], - 'applicableUsers' => [], - 'applicableGroups' => [], - 'priority' => 15, - ]); - - $newStorage = $this->service->addStorage($storage); - $id = $newStorage->getId(); - - $updatedStorage->setId($id); - - $this->service->updateStorage($updatedStorage); - $newStorage = $this->service->getStorage($id); - - $this->assertEquals($updatedStorage->getMountPoint(), $newStorage->getMountPoint()); - $this->assertEquals($updatedStorage->getBackendOptions()['password'], $newStorage->getBackendOptions()['password']); - $this->assertEquals($updatedStorage->getApplicableUsers(), $newStorage->getApplicableUsers()); - $this->assertEquals($updatedStorage->getApplicableGroups(), $newStorage->getApplicableGroups()); - $this->assertEquals($updatedStorage->getPriority(), $newStorage->getPriority()); - $this->assertEquals(0, $newStorage->getStatus()); - } - - function hooksAddStorageDataProvider() { - return [ - // applicable all - [ - [], - [], - // expected hook calls - [ - [ - Filesystem::signal_create_mount, - \OC_Mount_Config::MOUNT_TYPE_USER, - 'all' - ], - ], - ], - // single user - [ - ['user1'], - [], - // expected hook calls - [ - [ - Filesystem::signal_create_mount, - \OC_Mount_Config::MOUNT_TYPE_USER, - 'user1', - ], - ], - ], - // single group - [ - [], - ['group1'], - // expected hook calls - [ - [ - Filesystem::signal_create_mount, - \OC_Mount_Config::MOUNT_TYPE_GROUP, - 'group1', - ], - ], - ], - // multiple users - [ - ['user1', 'user2'], - [], - [ - [ - Filesystem::signal_create_mount, - \OC_Mount_Config::MOUNT_TYPE_USER, - 'user1', - ], - [ - Filesystem::signal_create_mount, - \OC_Mount_Config::MOUNT_TYPE_USER, - 'user2', - ], - ], - ], - // multiple groups - [ - [], - ['group1', 'group2'], - // expected hook calls - [ - [ - Filesystem::signal_create_mount, - \OC_Mount_Config::MOUNT_TYPE_GROUP, - 'group1' - ], - [ - Filesystem::signal_create_mount, - \OC_Mount_Config::MOUNT_TYPE_GROUP, - 'group2' - ], - ], - ], - // mixed groups and users - [ - ['user1', 'user2'], - ['group1', 'group2'], - // expected hook calls - [ - [ - Filesystem::signal_create_mount, - \OC_Mount_Config::MOUNT_TYPE_USER, - 'user1', - ], - [ - Filesystem::signal_create_mount, - \OC_Mount_Config::MOUNT_TYPE_USER, - 'user2', - ], - [ - Filesystem::signal_create_mount, - \OC_Mount_Config::MOUNT_TYPE_GROUP, - 'group1' - ], - [ - Filesystem::signal_create_mount, - \OC_Mount_Config::MOUNT_TYPE_GROUP, - 'group2' - ], - ], - ], - ]; - } - - /** - * @dataProvider hooksAddStorageDataProvider - */ - public function testHooksAddStorage($applicableUsers, $applicableGroups, $expectedCalls) { - $storage = $this->makeTestStorageData(); - $storage->setApplicableUsers($applicableUsers); - $storage->setApplicableGroups($applicableGroups); - $this->service->addStorage($storage); - - $this->assertCount(count($expectedCalls), self::$hookCalls); - - foreach ($expectedCalls as $index => $call) { - $this->assertHookCall( - self::$hookCalls[$index], - $call[0], - $storage->getMountPoint(), - $call[1], - $call[2] - ); - } - } - - function hooksUpdateStorageDataProvider() { - return [ - [ - // nothing to multiple users and groups - [], - [], - ['user1', 'user2'], - ['group1', 'group2'], - // expected hook calls - [ - // delete the "all entry" - [ - Filesystem::signal_delete_mount, - \OC_Mount_Config::MOUNT_TYPE_USER, - 'all', - ], - [ - Filesystem::signal_create_mount, - \OC_Mount_Config::MOUNT_TYPE_USER, - 'user1', - ], - [ - Filesystem::signal_create_mount, - \OC_Mount_Config::MOUNT_TYPE_USER, - 'user2', - ], - [ - Filesystem::signal_create_mount, - \OC_Mount_Config::MOUNT_TYPE_GROUP, - 'group1' - ], - [ - Filesystem::signal_create_mount, - \OC_Mount_Config::MOUNT_TYPE_GROUP, - 'group2' - ], - ], - ], - [ - // adding a user and a group - ['user1'], - ['group1'], - ['user1', 'user2'], - ['group1', 'group2'], - // expected hook calls - [ - [ - Filesystem::signal_create_mount, - \OC_Mount_Config::MOUNT_TYPE_USER, - 'user2', - ], - [ - Filesystem::signal_create_mount, - \OC_Mount_Config::MOUNT_TYPE_GROUP, - 'group2' - ], - ], - ], - [ - // removing a user and a group - ['user1', 'user2'], - ['group1', 'group2'], - ['user1'], - ['group1'], - // expected hook calls - [ - [ - Filesystem::signal_delete_mount, - \OC_Mount_Config::MOUNT_TYPE_USER, - 'user2', - ], - [ - Filesystem::signal_delete_mount, - \OC_Mount_Config::MOUNT_TYPE_GROUP, - 'group2' - ], - ], - ], - [ - // removing all - ['user1'], - ['group1'], - [], - [], - // expected hook calls - [ - [ - Filesystem::signal_delete_mount, - \OC_Mount_Config::MOUNT_TYPE_USER, - 'user1', - ], - [ - Filesystem::signal_delete_mount, - \OC_Mount_Config::MOUNT_TYPE_GROUP, - 'group1' - ], - // create the "all" entry - [ - Filesystem::signal_create_mount, - \OC_Mount_Config::MOUNT_TYPE_USER, - 'all' - ], - ], - ], - [ - // no changes - ['user1'], - ['group1'], - ['user1'], - ['group1'], - // no hook calls - [] - ] - ]; - } - - /** - * @dataProvider hooksUpdateStorageDataProvider - */ - public function testHooksUpdateStorage( - $sourceApplicableUsers, - $sourceApplicableGroups, - $updatedApplicableUsers, - $updatedApplicableGroups, - $expectedCalls) { - - $storage = $this->makeTestStorageData(); - $storage->setApplicableUsers($sourceApplicableUsers); - $storage->setApplicableGroups($sourceApplicableGroups); - $storage = $this->service->addStorage($storage); - - $storage->setapplicableUsers($updatedApplicableUsers); - $storage->setapplicableGroups($updatedApplicableGroups); - - // reset calls - self::$hookCalls = []; - - $this->service->updateStorage($storage); - - $this->assertCount(count($expectedCalls), self::$hookCalls); - - foreach ($expectedCalls as $index => $call) { - $this->assertHookCall( - self::$hookCalls[$index], - $call[0], - '/mountpoint', - $call[1], - $call[2] - ); - } - } - - /** - */ - public function testHooksRenameMountPoint() { - $storage = $this->makeTestStorageData(); - $storage->setApplicableUsers(['user1', 'user2']); - $storage->setApplicableGroups(['group1', 'group2']); - $storage = $this->service->addStorage($storage); - - $storage->setMountPoint('renamedMountpoint'); - - // reset calls - self::$hookCalls = []; - - $this->service->updateStorage($storage); - - $expectedCalls = [ - // deletes old mount - [ - Filesystem::signal_delete_mount, - '/mountpoint', - \OC_Mount_Config::MOUNT_TYPE_USER, - 'user1', - ], - [ - Filesystem::signal_delete_mount, - '/mountpoint', - \OC_Mount_Config::MOUNT_TYPE_USER, - 'user2', - ], - [ - Filesystem::signal_delete_mount, - '/mountpoint', - \OC_Mount_Config::MOUNT_TYPE_GROUP, - 'group1', - ], - [ - Filesystem::signal_delete_mount, - '/mountpoint', - \OC_Mount_Config::MOUNT_TYPE_GROUP, - 'group2', - ], - // creates new one - [ - Filesystem::signal_create_mount, - '/renamedMountpoint', - \OC_Mount_Config::MOUNT_TYPE_USER, - 'user1', - ], - [ - Filesystem::signal_create_mount, - '/renamedMountpoint', - \OC_Mount_Config::MOUNT_TYPE_USER, - 'user2', - ], - [ - Filesystem::signal_create_mount, - '/renamedMountpoint', - \OC_Mount_Config::MOUNT_TYPE_GROUP, - 'group1', - ], - [ - Filesystem::signal_create_mount, - '/renamedMountpoint', - \OC_Mount_Config::MOUNT_TYPE_GROUP, - 'group2', - ], - ]; - - $this->assertCount(count($expectedCalls), self::$hookCalls); - - foreach ($expectedCalls as $index => $call) { - $this->assertHookCall( - self::$hookCalls[$index], - $call[0], - $call[1], - $call[2], - $call[3] - ); - } - } - - function hooksDeleteStorageDataProvider() { - return [ - [ - ['user1', 'user2'], - ['group1', 'group2'], - // expected hook calls - [ - [ - Filesystem::signal_delete_mount, - \OC_Mount_Config::MOUNT_TYPE_USER, - 'user1', - ], - [ - Filesystem::signal_delete_mount, - \OC_Mount_Config::MOUNT_TYPE_USER, - 'user2', - ], - [ - Filesystem::signal_delete_mount, - \OC_Mount_Config::MOUNT_TYPE_GROUP, - 'group1' - ], - [ - Filesystem::signal_delete_mount, - \OC_Mount_Config::MOUNT_TYPE_GROUP, - 'group2' - ], - ], - ], - [ - // deleting "all" entry - [], - [], - [ - [ - Filesystem::signal_delete_mount, - \OC_Mount_Config::MOUNT_TYPE_USER, - 'all', - ], - ], - ], - ]; - } - - /** - * @dataProvider hooksDeleteStorageDataProvider - */ - public function testHooksDeleteStorage( - $sourceApplicableUsers, - $sourceApplicableGroups, - $expectedCalls) { - - $storage = $this->makeTestStorageData(); - $storage->setApplicableUsers($sourceApplicableUsers); - $storage->setApplicableGroups($sourceApplicableGroups); - $storage = $this->service->addStorage($storage); - - // reset calls - self::$hookCalls = []; - - $this->service->removeStorage($storage->getId()); - - $this->assertCount(count($expectedCalls), self::$hookCalls); - - foreach ($expectedCalls as $index => $call) { - $this->assertHookCall( - self::$hookCalls[$index], - $call[0], - '/mountpoint', - $call[1], - $call[2] - ); - } - } - -} diff --git a/apps/files_external/tests/service/storagesservicetest.php b/apps/files_external/tests/service/storagesservicetest.php deleted file mode 100644 index f93c0134814..00000000000 --- a/apps/files_external/tests/service/storagesservicetest.php +++ /dev/null @@ -1,505 +0,0 @@ - - * @author Robin McCorkell - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, 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 - * - */ -namespace OCA\Files_external\Tests\Service; - -use \OC\Files\Filesystem; - -use \OCA\Files_external\NotFoundException; -use \OCA\Files_external\Lib\StorageConfig; -use OCA\Files_External\Service\BackendService; -use OCA\Files_External\Service\DBConfigService; -use OCA\Files_external\Service\StoragesService; - -class CleaningDBConfig extends DBConfigService { - private $mountIds = []; - - public function addMount($mountPoint, $storageBackend, $authBackend, $priority, $type) { - $id = parent::addMount($mountPoint, $storageBackend, $authBackend, $priority, $type); // TODO: Change the autogenerated stub - $this->mountIds[] = $id; - return $id; - } - - public function clean() { - foreach ($this->mountIds as $id) { - $this->removeMount($id); - } - } -} - -/** - * @group DB - */ -abstract class StoragesServiceTest extends \Test\TestCase { - - /** - * @var StoragesService - */ - protected $service; - - /** @var BackendService */ - protected $backendService; - - /** - * Data directory - * - * @var string - */ - protected $dataDir; - - /** @var CleaningDBConfig */ - protected $dbConfig; - - /** - * Hook calls - * - * @var array - */ - protected static $hookCalls; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject|\OCP\Files\Config\IUserMountCache - */ - protected $mountCache; - - public function setUp() { - parent::setUp(); - $this->dbConfig = new CleaningDBConfig(\OC::$server->getDatabaseConnection(), \OC::$server->getCrypto()); - self::$hookCalls = array(); - $config = \OC::$server->getConfig(); - $this->dataDir = $config->getSystemValue( - 'datadirectory', - \OC::$SERVERROOT . '/data/' - ); - \OC_Mount_Config::$skipTest = true; - - $this->mountCache = $this->getMock('OCP\Files\Config\IUserMountCache'); - - // prepare BackendService mock - $this->backendService = - $this->getMockBuilder('\OCA\Files_External\Service\BackendService') - ->disableOriginalConstructor() - ->getMock(); - - $authMechanisms = [ - 'identifier:\Auth\Mechanism' => $this->getAuthMechMock('null', '\Auth\Mechanism'), - 'identifier:\Other\Auth\Mechanism' => $this->getAuthMechMock('null', '\Other\Auth\Mechanism'), - 'identifier:\OCA\Files_External\Lib\Auth\NullMechanism' => $this->getAuthMechMock(), - ]; - $this->backendService->method('getAuthMechanism') - ->will($this->returnCallback(function ($class) use ($authMechanisms) { - if (isset($authMechanisms[$class])) { - return $authMechanisms[$class]; - } - return null; - })); - $this->backendService->method('getAuthMechanismsByScheme') - ->will($this->returnCallback(function ($schemes) use ($authMechanisms) { - return array_filter($authMechanisms, function ($authMech) use ($schemes) { - return in_array($authMech->getScheme(), $schemes, true); - }); - })); - $this->backendService->method('getAuthMechanisms') - ->will($this->returnValue($authMechanisms)); - - $sftpBackend = $this->getBackendMock('\OCA\Files_External\Lib\Backend\SFTP', '\OCA\Files_External\Lib\Storage\SFTP'); - $backends = [ - 'identifier:\OCA\Files_External\Lib\Backend\SMB' => $this->getBackendMock('\OCA\Files_External\Lib\Backend\SMB', '\OCA\Files_External\Lib\Storage\SMB'), - 'identifier:\OCA\Files_External\Lib\Backend\SFTP' => $sftpBackend, - 'identifier:sftp_alias' => $sftpBackend, - ]; - $backends['identifier:\OCA\Files_External\Lib\Backend\SFTP']->method('getLegacyAuthMechanism') - ->willReturn($authMechanisms['identifier:\Other\Auth\Mechanism']); - $this->backendService->method('getBackend') - ->will($this->returnCallback(function ($backendClass) use ($backends) { - if (isset($backends[$backendClass])) { - return $backends[$backendClass]; - } - return null; - })); - $this->backendService->method('getBackends') - ->will($this->returnValue($backends)); - - \OCP\Util::connectHook( - Filesystem::CLASSNAME, - Filesystem::signal_create_mount, - get_class($this), 'createHookCallback'); - \OCP\Util::connectHook( - Filesystem::CLASSNAME, - Filesystem::signal_delete_mount, - get_class($this), 'deleteHookCallback'); - - $containerMock = $this->getMock('\OCP\AppFramework\IAppContainer'); - $containerMock->method('query') - ->will($this->returnCallback(function ($name) { - if ($name === 'OCA\Files_External\Service\BackendService') { - return $this->backendService; - } - })); - - \OC_Mount_Config::$app = $this->getMockBuilder('\OCA\Files_External\Appinfo\Application') - ->disableOriginalConstructor() - ->getMock(); - \OC_Mount_Config::$app->method('getContainer') - ->willReturn($containerMock); - } - - public function tearDown() { - \OC_Mount_Config::$skipTest = false; - self::$hookCalls = array(); - if ($this->dbConfig) { - $this->dbConfig->clean(); - } - } - - protected function getBackendMock($class = '\OCA\Files_External\Lib\Backend\SMB', $storageClass = '\OCA\Files_External\Lib\Storage\SMB') { - $backend = $this->getMockBuilder('\OCA\Files_External\Lib\Backend\Backend') - ->disableOriginalConstructor() - ->getMock(); - $backend->method('getStorageClass') - ->willReturn($storageClass); - $backend->method('getIdentifier') - ->willReturn('identifier:' . $class); - return $backend; - } - - protected function getAuthMechMock($scheme = 'null', $class = '\OCA\Files_External\Lib\Auth\NullMechanism') { - $authMech = $this->getMockBuilder('\OCA\Files_External\Lib\Auth\AuthMechanism') - ->disableOriginalConstructor() - ->getMock(); - $authMech->method('getScheme') - ->willReturn($scheme); - $authMech->method('getIdentifier') - ->willReturn('identifier:' . $class); - - return $authMech; - } - - /** - * Creates a StorageConfig instance based on array data - * - * @param array data - * - * @return StorageConfig storage config instance - */ - protected function makeStorageConfig($data) { - $storage = new StorageConfig(); - if (isset($data['id'])) { - $storage->setId($data['id']); - } - $storage->setMountPoint($data['mountPoint']); - if (!isset($data['backend'])) { - // data providers are run before $this->backendService is initialised - // so $data['backend'] can be specified directly - $data['backend'] = $this->backendService->getBackend($data['backendIdentifier']); - } - if (!isset($data['backend'])) { - throw new \Exception('oops, no backend'); - } - if (!isset($data['authMechanism'])) { - $data['authMechanism'] = $this->backendService->getAuthMechanism($data['authMechanismIdentifier']); - } - if (!isset($data['authMechanism'])) { - throw new \Exception('oops, no auth mechanism'); - } - $storage->setBackend($data['backend']); - $storage->setAuthMechanism($data['authMechanism']); - $storage->setBackendOptions($data['backendOptions']); - if (isset($data['applicableUsers'])) { - $storage->setApplicableUsers($data['applicableUsers']); - } - if (isset($data['applicableGroups'])) { - $storage->setApplicableGroups($data['applicableGroups']); - } - if (isset($data['priority'])) { - $storage->setPriority($data['priority']); - } - if (isset($data['mountOptions'])) { - $storage->setMountOptions($data['mountOptions']); - } - return $storage; - } - - - /** - * @expectedException \OCA\Files_external\NotFoundException - */ - public function testNonExistingStorage() { - $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB'); - $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism'); - $storage = new StorageConfig(255); - $storage->setMountPoint('mountpoint'); - $storage->setBackend($backend); - $storage->setAuthMechanism($authMechanism); - $this->service->updateStorage($storage); - } - - public function deleteStorageDataProvider() { - return [ - // regular case, can properly delete the oc_storages entry - [ - [ - 'share' => 'share', - 'host' => 'example.com', - 'user' => 'test', - 'password' => 'testPassword', - 'root' => 'someroot', - ], - 'smb::test@example.com//share//someroot/', - 0 - ], - // special case with $user vars, cannot auto-remove the oc_storages entry - [ - [ - 'share' => 'share', - 'host' => 'example.com', - 'user' => '$user', - 'password' => 'testPassword', - 'root' => 'someroot', - ], - 'smb::someone@example.com//share//someroot/', - 1 - ], - ]; - } - - /** - * @dataProvider deleteStorageDataProvider - */ - public function testDeleteStorage($backendOptions, $rustyStorageId, $expectedCountAfterDeletion) { - $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB'); - $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism'); - $storage = new StorageConfig(255); - $storage->setMountPoint('mountpoint'); - $storage->setBackend($backend); - $storage->setAuthMechanism($authMechanism); - $storage->setBackendOptions($backendOptions); - - $newStorage = $this->service->addStorage($storage); - $id = $newStorage->getId(); - - // manually trigger storage entry because normally it happens on first - // access, which isn't possible within this test - $storageCache = new \OC\Files\Cache\Storage($rustyStorageId); - - // get numeric id for later check - $numericId = $storageCache->getNumericId(); - - $this->service->removeStorage($id); - - $caught = false; - try { - $this->service->getStorage(1); - } catch (NotFoundException $e) { - $caught = true; - } - - $this->assertTrue($caught); - - // storage id was removed from oc_storages - $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder(); - $storageCheckQuery = $qb->select('*') - ->from('storages') - ->where($qb->expr()->eq('numeric_id', $qb->expr()->literal($numericId))); - $this->assertCount($expectedCountAfterDeletion, $storageCheckQuery->execute()->fetchAll()); - } - - /** - * @expectedException \OCA\Files_external\NotFoundException - */ - public function testDeleteUnexistingStorage() { - $this->service->removeStorage(255); - } - - public function testCreateStorage() { - $mountPoint = 'mount'; - $backendIdentifier = 'identifier:\OCA\Files_External\Lib\Backend\SMB'; - $authMechanismIdentifier = 'identifier:\Auth\Mechanism'; - $backendOptions = ['param' => 'foo', 'param2' => 'bar']; - $mountOptions = ['option' => 'foobar']; - $applicableUsers = ['user1', 'user2']; - $applicableGroups = ['group']; - $priority = 123; - - $backend = $this->backendService->getBackend($backendIdentifier); - $authMechanism = $this->backendService->getAuthMechanism($authMechanismIdentifier); - - $storage = $this->service->createStorage( - $mountPoint, - $backendIdentifier, - $authMechanismIdentifier, - $backendOptions, - $mountOptions, - $applicableUsers, - $applicableGroups, - $priority - ); - - $this->assertEquals('/' . $mountPoint, $storage->getMountPoint()); - $this->assertEquals($backend, $storage->getBackend()); - $this->assertEquals($authMechanism, $storage->getAuthMechanism()); - $this->assertEquals($backendOptions, $storage->getBackendOptions()); - $this->assertEquals($mountOptions, $storage->getMountOptions()); - $this->assertEquals($applicableUsers, $storage->getApplicableUsers()); - $this->assertEquals($applicableGroups, $storage->getApplicableGroups()); - $this->assertEquals($priority, $storage->getPriority()); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testCreateStorageInvalidClass() { - $this->service->createStorage( - 'mount', - 'identifier:\OC\Not\A\Backend', - 'identifier:\Auth\Mechanism', - [] - ); - } - - /** - * @expectedException \InvalidArgumentException - */ - public function testCreateStorageInvalidAuthMechanismClass() { - $this->service->createStorage( - 'mount', - 'identifier:\OCA\Files_External\Lib\Backend\SMB', - 'identifier:\Not\An\Auth\Mechanism', - [] - ); - } - - public function testGetStoragesBackendNotVisible() { - $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB'); - $backend->expects($this->once()) - ->method('isVisibleFor') - ->with($this->service->getVisibilityType()) - ->willReturn(false); - $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism'); - $authMechanism->method('isVisibleFor') - ->with($this->service->getVisibilityType()) - ->willReturn(true); - - $storage = new StorageConfig(255); - $storage->setMountPoint('mountpoint'); - $storage->setBackend($backend); - $storage->setAuthMechanism($authMechanism); - $storage->setBackendOptions(['password' => 'testPassword']); - - $newStorage = $this->service->addStorage($storage); - - $this->assertCount(1, $this->service->getAllStorages()); - $this->assertEmpty($this->service->getStorages()); - } - - public function testGetStoragesAuthMechanismNotVisible() { - $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB'); - $backend->method('isVisibleFor') - ->with($this->service->getVisibilityType()) - ->willReturn(true); - $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism'); - $authMechanism->expects($this->once()) - ->method('isVisibleFor') - ->with($this->service->getVisibilityType()) - ->willReturn(false); - - $storage = new StorageConfig(255); - $storage->setMountPoint('mountpoint'); - $storage->setBackend($backend); - $storage->setAuthMechanism($authMechanism); - $storage->setBackendOptions(['password' => 'testPassword']); - - $newStorage = $this->service->addStorage($storage); - - $this->assertCount(1, $this->service->getAllStorages()); - $this->assertEmpty($this->service->getStorages()); - } - - public static function createHookCallback($params) { - self::$hookCalls[] = array( - 'signal' => Filesystem::signal_create_mount, - 'params' => $params - ); - } - - public static function deleteHookCallback($params) { - self::$hookCalls[] = array( - 'signal' => Filesystem::signal_delete_mount, - 'params' => $params - ); - } - - /** - * Asserts hook call - * - * @param array $callData hook call data to check - * @param string $signal signal name - * @param string $mountPath mount path - * @param string $mountType mount type - * @param string $applicable applicable users - */ - protected function assertHookCall($callData, $signal, $mountPath, $mountType, $applicable) { - $this->assertEquals($signal, $callData['signal']); - $params = $callData['params']; - $this->assertEquals( - $mountPath, - $params[Filesystem::signal_param_path] - ); - $this->assertEquals( - $mountType, - $params[Filesystem::signal_param_mount_type] - ); - $this->assertEquals( - $applicable, - $params[Filesystem::signal_param_users] - ); - } - - public function testUpdateStorageMountPoint() { - $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB'); - $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism'); - - $storage = new StorageConfig(); - $storage->setMountPoint('mountpoint'); - $storage->setBackend($backend); - $storage->setAuthMechanism($authMechanism); - $storage->setBackendOptions(['password' => 'testPassword']); - - $savedStorage = $this->service->addStorage($storage); - - $newAuthMechanism = $this->backendService->getAuthMechanism('identifier:\Other\Auth\Mechanism'); - - $updatedStorage = new StorageConfig($savedStorage->getId()); - $updatedStorage->setMountPoint('mountpoint2'); - $updatedStorage->setBackend($backend); - $updatedStorage->setAuthMechanism($newAuthMechanism); - $updatedStorage->setBackendOptions(['password' => 'password2']); - - $this->service->updateStorage($updatedStorage); - - $savedStorage = $this->service->getStorage($updatedStorage->getId()); - - $this->assertEquals('/mountpoint2', $savedStorage->getMountPoint()); - $this->assertEquals($newAuthMechanism, $savedStorage->getAuthMechanism()); - $this->assertEquals('password2', $savedStorage->getBackendOption('password')); - } -} diff --git a/apps/files_external/tests/service/userglobalstoragesservicetest.php b/apps/files_external/tests/service/userglobalstoragesservicetest.php deleted file mode 100644 index baecf143c66..00000000000 --- a/apps/files_external/tests/service/userglobalstoragesservicetest.php +++ /dev/null @@ -1,366 +0,0 @@ - - * @author Robin McCorkell - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, 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 - * - */ -namespace OCA\Files_External\Tests\Service; - -use OCA\Files_external\NotFoundException; -use OCA\Files_external\Service\StoragesService; -use \OCA\Files_External\Service\UserGlobalStoragesService; -use \OCP\IGroupManager; - -use \OCA\Files_External\Lib\StorageConfig; -use OCP\IUser; -use Test\Traits\UserTrait; - -/** - * @group DB - */ -class UserGlobalStoragesServiceTest extends GlobalStoragesServiceTest { - use UserTrait; - - /** @var \OCP\IGroupManager|\PHPUnit_Framework_MockObject_MockObject groupManager */ - protected $groupManager; - - /** - * @var StoragesService - */ - protected $globalStoragesService; - - /** - * @var UserGlobalStoragesService - */ - protected $service; - - protected $user; - - const USER_ID = 'test_user'; - const GROUP_ID = 'test_group'; - const GROUP_ID2 = 'test_group2'; - - public function setUp() { - parent::setUp(); - - $this->globalStoragesService = $this->service; - - $this->user = new \OC\User\User(self::USER_ID, null); - /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject $userSession */ - $userSession = $this->getMock('\OCP\IUserSession'); - $userSession - ->expects($this->any()) - ->method('getUser') - ->will($this->returnValue($this->user)); - - $this->groupManager = $this->getMock('\OCP\IGroupManager'); - $this->groupManager->method('isInGroup') - ->will($this->returnCallback(function ($userId, $groupId) { - if ($userId === self::USER_ID) { - switch ($groupId) { - case self::GROUP_ID: - case self::GROUP_ID2: - return true; - } - } - return false; - })); - $this->groupManager->method('getUserGroupIds') - ->will($this->returnCallback(function (IUser $user) { - if ($user->getUID() === self::USER_ID) { - return [self::GROUP_ID, self::GROUP_ID2]; - } else { - return []; - } - })); - - $this->service = new UserGlobalStoragesService( - $this->backendService, - $this->dbConfig, - $userSession, - $this->groupManager, - $this->mountCache - ); - } - - public function applicableStorageProvider() { - return [ - [[], [], true], - - // not applicable cases - [['user1'], [], false], - [[], ['group1'], false], - [['user1'], ['group1'], false], - - // applicable cases - [[self::USER_ID], [], true], - [[], [self::GROUP_ID], true], - [[self::USER_ID], ['group1'], true], - [['user1'], [self::GROUP_ID], true], - - // sanity checks - [['user1', 'user2', self::USER_ID, 'user3'], [], true], - ]; - } - - /** - * @dataProvider applicableStorageProvider - */ - public function testGetStorageWithApplicable($applicableUsers, $applicableGroups, $isVisible) { - $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB'); - $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism'); - - $storage = new StorageConfig(); - $storage->setMountPoint('mountpoint'); - $storage->setBackend($backend); - $storage->setAuthMechanism($authMechanism); - $storage->setBackendOptions(['password' => 'testPassword']); - $storage->setApplicableUsers($applicableUsers); - $storage->setApplicableGroups($applicableGroups); - - $newStorage = $this->globalStoragesService->addStorage($storage); - - $storages = $this->service->getAllStorages(); - if ($isVisible) { - $this->assertEquals(1, count($storages)); - $retrievedStorage = $this->service->getStorage($newStorage->getId()); - $this->assertEquals('/mountpoint', $retrievedStorage->getMountPoint()); - } else { - $this->assertEquals(0, count($storages)); - - try { - $this->service->getStorage($newStorage->getId()); - $this->fail('Failed asserting that storage can\'t be accessed by id'); - } catch (NotFoundException $e) { - - } - } - - } - - /** - * @expectedException \DomainException - */ - public function testAddStorage($storageParams = null) { - $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB'); - $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism'); - - $storage = new StorageConfig(255); - $storage->setMountPoint('mountpoint'); - $storage->setBackend($backend); - $storage->setAuthMechanism($authMechanism); - $storage->setBackendOptions(['password' => 'testPassword']); - - $this->service->addStorage($storage); - } - - /** - * @expectedException \DomainException - */ - public function testUpdateStorage($storageParams = null) { - $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB'); - $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism'); - - $storage = new StorageConfig(255); - $storage->setMountPoint('mountpoint'); - $storage->setBackend($backend); - $storage->setAuthMechanism($authMechanism); - $storage->setBackendOptions(['password' => 'testPassword']); - - $newStorage = $this->globalStoragesService->addStorage($storage); - - $retrievedStorage = $this->service->getStorage($newStorage->getId()); - $retrievedStorage->setMountPoint('abc'); - $this->service->updateStorage($retrievedStorage); - } - - /** - * @expectedException \DomainException - */ - public function testNonExistingStorage() { - parent::testNonExistingStorage(); - } - - /** - * @expectedException \DomainException - * @dataProvider deleteStorageDataProvider - */ - public function testDeleteStorage($backendOptions, $rustyStorageId, $expectedCountAfterDeletion) { - $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB'); - $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism'); - - $storage = new StorageConfig(255); - $storage->setMountPoint('mountpoint'); - $storage->setBackend($backend); - $storage->setAuthMechanism($authMechanism); - $storage->setBackendOptions($backendOptions); - - $newStorage = $this->globalStoragesService->addStorage($storage); - $id = $newStorage->getId(); - - $this->service->removeStorage($id); - } - - /** - * @expectedException \DomainException - */ - public function testDeleteUnexistingStorage() { - parent::testDeleteUnexistingStorage(); - } - - public function getUniqueStoragesProvider() { - return [ - // 'all' vs group - [100, [], [], 100, [], [self::GROUP_ID], 2], - [100, [], [self::GROUP_ID], 100, [], [], 1], - - // 'all' vs user - [100, [], [], 100, [self::USER_ID], [], 2], - [100, [self::USER_ID], [], 100, [], [], 1], - - // group vs user - [100, [], [self::GROUP_ID], 100, [self::USER_ID], [], 2], - [100, [self::USER_ID], [], 100, [], [self::GROUP_ID], 1], - - // group+user vs group - [100, [], [self::GROUP_ID2], 100, [self::USER_ID], [self::GROUP_ID], 2], - [100, [self::USER_ID], [self::GROUP_ID], 100, [], [self::GROUP_ID2], 1], - - // user vs 'all' (higher priority) - [200, [], [], 100, [self::USER_ID], [], 2], - [100, [self::USER_ID], [], 200, [], [], 1], - - // group vs group (higher priority) - [100, [], [self::GROUP_ID2], 200, [], [self::GROUP_ID], 2], - [200, [], [self::GROUP_ID], 100, [], [self::GROUP_ID2], 1], - ]; - } - - /** - * @dataProvider getUniqueStoragesProvider - */ - public function testGetUniqueStorages( - $priority1, $applicableUsers1, $applicableGroups1, - $priority2, $applicableUsers2, $applicableGroups2, - $expectedPrecedence - ) { - $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB'); - $backend->method('isVisibleFor') - ->willReturn(true); - $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism'); - $authMechanism->method('isVisibleFor') - ->willReturn(true); - - $storage1 = new StorageConfig(); - $storage1->setMountPoint('mountpoint'); - $storage1->setBackend($backend); - $storage1->setAuthMechanism($authMechanism); - $storage1->setBackendOptions(['password' => 'testPassword']); - $storage1->setPriority($priority1); - $storage1->setApplicableUsers($applicableUsers1); - $storage1->setApplicableGroups($applicableGroups1); - - $storage1 = $this->globalStoragesService->addStorage($storage1); - - $storage2 = new StorageConfig(); - $storage2->setMountPoint('mountpoint'); - $storage2->setBackend($backend); - $storage2->setAuthMechanism($authMechanism); - $storage2->setBackendOptions(['password' => 'testPassword']); - $storage2->setPriority($priority2); - $storage2->setApplicableUsers($applicableUsers2); - $storage2->setApplicableGroups($applicableGroups2); - - $storage2 = $this->globalStoragesService->addStorage($storage2); - - $storages = $this->service->getUniqueStorages(); - $this->assertCount(1, $storages); - - if ($expectedPrecedence === 1) { - $this->assertArrayHasKey($storage1->getID(), $storages); - } elseif ($expectedPrecedence === 2) { - $this->assertArrayHasKey($storage2->getID(), $storages); - } - } - - public function testGetStoragesBackendNotVisible() { - // we don't test this here - $this->assertTrue(true); - } - - public function testGetStoragesAuthMechanismNotVisible() { - // we don't test this here - $this->assertTrue(true); - } - - public function testHooksAddStorage($a = null, $b = null, $c = null) { - // we don't test this here - $this->assertTrue(true); - } - - public function testHooksUpdateStorage($a = null, $b = null, $c = null, $d = null, $e = null) { - // we don't test this here - $this->assertTrue(true); - } - - public function testHooksRenameMountPoint() { - // we don't test this here - $this->assertTrue(true); - } - - public function testHooksDeleteStorage($a = null, $b = null, $c = null) { - // we don't test this here - $this->assertTrue(true); - } - - public function testLegacyConfigConversionApplicableAll() { - // we don't test this here - $this->assertTrue(true); - } - - public function testLegacyConfigConversionApplicableUserAndGroup() { - // we don't test this here - $this->assertTrue(true); - } - - public function testReadLegacyConfigAndGenerateConfigId() { - // we don't test this here - $this->assertTrue(true); - } - - public function testReadLegacyConfigNoAuthMechanism() { - // we don't test this here - $this->assertTrue(true); - } - - public function testReadLegacyConfigClass() { - // we don't test this here - $this->assertTrue(true); - } - - public function testReadEmptyMountPoint() { - // we don't test this here - $this->assertTrue(true); - } - - public function testUpdateStorageMountPoint() { - // we don't test this here - $this->assertTrue(true); - } -} diff --git a/apps/files_external/tests/service/userstoragesservicetest.php b/apps/files_external/tests/service/userstoragesservicetest.php deleted file mode 100644 index 37423cb8d74..00000000000 --- a/apps/files_external/tests/service/userstoragesservicetest.php +++ /dev/null @@ -1,208 +0,0 @@ - - * @author Robin McCorkell - * @author Vincent Petry - * - * @copyright Copyright (c) 2016, 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 - * - */ -namespace OCA\Files_external\Tests\Service; - -use \OC\Files\Filesystem; - -use OCA\Files_external\Service\GlobalStoragesService; -use OCA\Files_external\Service\StoragesService; -use \OCA\Files_external\Service\UserStoragesService; -use \OCA\Files_external\NotFoundException; -use \OCA\Files_external\Lib\StorageConfig; -use Test\Traits\UserTrait; - -/** - * @group DB - */ -class UserStoragesServiceTest extends StoragesServiceTest { - use UserTrait; - - private $user; - - private $userId; - - /** - * @var StoragesService - */ - protected $globalStoragesService; - - public function setUp() { - parent::setUp(); - - $this->globalStoragesService = new GlobalStoragesService($this->backendService, $this->dbConfig, $this->mountCache); - - $this->userId = $this->getUniqueID('user_'); - $this->createUser($this->userId, $this->userId); - $this->user = \OC::$server->getUserManager()->get($this->userId); - - /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject $userSession */ - $userSession = $this->getMock('\OCP\IUserSession'); - $userSession - ->expects($this->any()) - ->method('getUser') - ->will($this->returnValue($this->user)); - - $this->service = new UserStoragesService($this->backendService, $this->dbConfig, $userSession, $this->mountCache); - } - - private function makeTestStorageData() { - return $this->makeStorageConfig([ - 'mountPoint' => 'mountpoint', - 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB', - 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism', - 'backendOptions' => [ - 'option1' => 'value1', - 'option2' => 'value2', - 'password' => 'testPassword', - ], - 'mountOptions' => [ - 'preview' => false, - ] - ]); - } - - public function testAddStorage() { - $storage = $this->makeTestStorageData(); - - $newStorage = $this->service->addStorage($storage); - - $id = $newStorage->getId(); - - $newStorage = $this->service->getStorage($id); - - $this->assertEquals($storage->getMountPoint(), $newStorage->getMountPoint()); - $this->assertEquals($storage->getBackend(), $newStorage->getBackend()); - $this->assertEquals($storage->getAuthMechanism(), $newStorage->getAuthMechanism()); - $this->assertEquals($storage->getBackendOptions(), $newStorage->getBackendOptions()); - $this->assertEquals(0, $newStorage->getStatus()); - - // hook called once for user - $this->assertHookCall( - current(self::$hookCalls), - Filesystem::signal_create_mount, - $storage->getMountPoint(), - \OC_Mount_Config::MOUNT_TYPE_USER, - $this->userId - ); - - $nextStorage = $this->service->addStorage($storage); - $this->assertEquals($id + 1, $nextStorage->getId()); - } - - public function testUpdateStorage() { - $storage = $this->makeStorageConfig([ - 'mountPoint' => 'mountpoint', - 'backendIdentifier' => 'identifier:\OCA\Files_External\Lib\Backend\SMB', - 'authMechanismIdentifier' => 'identifier:\Auth\Mechanism', - 'backendOptions' => [ - 'option1' => 'value1', - 'option2' => 'value2', - 'password' => 'testPassword', - ], - ]); - - $newStorage = $this->service->addStorage($storage); - - $backendOptions = $newStorage->getBackendOptions(); - $backendOptions['password'] = 'anotherPassword'; - $newStorage->setBackendOptions($backendOptions); - - self::$hookCalls = []; - - $newStorage = $this->service->updateStorage($newStorage); - - $this->assertEquals('anotherPassword', $newStorage->getBackendOptions()['password']); - $this->assertEquals([$this->userId], $newStorage->getApplicableUsers()); - // these attributes are unused for user storages - $this->assertEmpty($newStorage->getApplicableGroups()); - $this->assertEquals(0, $newStorage->getStatus()); - - // no hook calls - $this->assertEmpty(self::$hookCalls); - } - - /** - * @dataProvider deleteStorageDataProvider - */ - public function testDeleteStorage($backendOptions, $rustyStorageId, $expectedCountAfterDeletion) { - parent::testDeleteStorage($backendOptions, $rustyStorageId, $expectedCountAfterDeletion); - - // hook called once for user (first one was during test creation) - $this->assertHookCall( - self::$hookCalls[1], - Filesystem::signal_delete_mount, - '/mountpoint', - \OC_Mount_Config::MOUNT_TYPE_USER, - $this->userId - ); - } - - public function testHooksRenameMountPoint() { - $storage = $this->makeTestStorageData(); - $storage = $this->service->addStorage($storage); - - $storage->setMountPoint('renamedMountpoint'); - - // reset calls - self::$hookCalls = []; - - $this->service->updateStorage($storage); - - // hook called twice - $this->assertHookCall( - self::$hookCalls[0], - Filesystem::signal_delete_mount, - '/mountpoint', - \OC_Mount_Config::MOUNT_TYPE_USER, - $this->userId - ); - $this->assertHookCall( - self::$hookCalls[1], - Filesystem::signal_create_mount, - '/renamedMountpoint', - \OC_Mount_Config::MOUNT_TYPE_USER, - $this->userId - ); - } - - /** - * @expectedException \OCA\Files_external\NotFoundException - */ - public function testGetAdminStorage() { - $backend = $this->backendService->getBackend('identifier:\OCA\Files_External\Lib\Backend\SMB'); - $authMechanism = $this->backendService->getAuthMechanism('identifier:\Auth\Mechanism'); - - $storage = new StorageConfig(); - $storage->setMountPoint('mountpoint'); - $storage->setBackend($backend); - $storage->setAuthMechanism($authMechanism); - $storage->setBackendOptions(['password' => 'testPassword']); - $storage->setApplicableUsers([$this->userId]); - - $newStorage = $this->globalStoragesService->addStorage($storage); - - $this->assertInstanceOf('\OCA\Files_external\Lib\StorageConfig', $this->globalStoragesService->getStorage($newStorage->getId())); - - $this->service->getStorage($newStorage->getId()); - } -} -- cgit v1.2.3