From 5df5b9c8b13770142eab82e889eed5e50da5fab3 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Thu, 3 Nov 2016 12:14:44 +0100 Subject: [PATCH 1/5] Handle invalid ext storage backend to keep mount point visible Keep mount point visible and also ext storage config visible when dealing with configs relating to storage backends or auth mechanisms that were provided by an app that is currently disabled. Signed-off-by: Morris Jobke --- apps/files_external/js/settings.js | 14 +++++ .../lib/Lib/Auth/InvalidAuth.php | 44 ++++++++++++++ .../lib/Lib/Backend/InvalidBackend.php | 57 +++++++++++++++++++ .../lib/Lib/Storage/InvalidStorage.php | 30 ++++++++++ .../lib/Service/StoragesService.php | 10 +++- 5 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 apps/files_external/lib/Lib/Auth/InvalidAuth.php create mode 100644 apps/files_external/lib/Lib/Backend/InvalidBackend.php create mode 100644 apps/files_external/lib/Lib/Storage/InvalidStorage.php diff --git a/apps/files_external/js/settings.js b/apps/files_external/js/settings.js index 112676b8c2..30074ab179 100644 --- a/apps/files_external/js/settings.js +++ b/apps/files_external/js/settings.js @@ -805,6 +805,13 @@ MountConfigListView.prototype = _.extend({ var mountPoint = storageConfig.mountPoint; var backend = this._allBackends[storageConfig.backend]; + if (!backend) { + backend = { + name: 'Unknown: ' + storageConfig.backend, + invalid: true + }; + } + // FIXME: Replace with a proper Handlebar template var $tr = this.$el.find('tr#addMountPoint'); this.$el.find('tbody').append($tr.clone()); @@ -829,6 +836,13 @@ MountConfigListView.prototype = _.extend({ $tr.addClass(backend.identifier); $tr.find('.backend').data('identifier', backend.identifier); + if (backend.invalid) { + $tr.find('[name=mountPoint]').prop('disabled', true); + $tr.find('.applicable,.mountOptionsToggle').empty(); + this.updateStatus($tr, false, 'Unknown backend: ' + backend.name); + return $tr; + } + var selectAuthMechanism = $(''); var neededVisibility = (this._isPersonal) ? StorageConfig.Visibility.PERSONAL : StorageConfig.Visibility.ADMIN; $.each(this._allAuthMechanisms, function(authIdentifier, authMechanism) { diff --git a/apps/files_external/lib/Lib/Auth/InvalidAuth.php b/apps/files_external/lib/Lib/Auth/InvalidAuth.php new file mode 100644 index 0000000000..c99eaa73d1 --- /dev/null +++ b/apps/files_external/lib/Lib/Auth/InvalidAuth.php @@ -0,0 +1,44 @@ + + * + * @copyright Copyright (c) 2016, ownCloud GmbH. + * @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\Lib\Auth; + +/** + * Invalid authentication representing an auth mechanism + * that could not be resolved0 + */ +class InvalidAuth extends AuthMechanism { + + /** + * Constructs a new InvalidAuth with the id of the invalid auth + * for display purposes + * + * @param string $invalidId invalid id + */ + public function __construct($invalidId) { + $this + ->setIdentifier($invalidId) + ->setScheme(self::SCHEME_NULL) + ->setText('Unknown auth mechanism backend ' . $invalidId) + ; + } + +} diff --git a/apps/files_external/lib/Lib/Backend/InvalidBackend.php b/apps/files_external/lib/Lib/Backend/InvalidBackend.php new file mode 100644 index 0000000000..1a426c4bbb --- /dev/null +++ b/apps/files_external/lib/Lib/Backend/InvalidBackend.php @@ -0,0 +1,57 @@ + + * + * @copyright Copyright (c) 2016, ownCloud GmbH. + * @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\Lib\Backend; + +/** + * Invalid storage backend representing a backend + * that could not be resolved + */ +class InvalidBackend extends Backend { + + /** @var string Invalid backend id */ + private $invalidId; + + /** + * Constructs a new InvalidBackend with the id of the invalid backend + * for display purposes + * + * @param string $invalidId id of the backend that did not exist + */ + function __construct($invalidId) { + $this->invalidId = $invalidId; + $this + ->setIdentifier($invalidId) + ->setStorageClass('\OCA\Files_External\Lib\Storage\InvalidStorage') + ->setText('Unknown storage backend ' . $invalidId) + ; + } + + /** + * Returns the invalid backend id + * + * @return string invalid backend id + */ + public function getInvalidId() { + return $this->invalidId; + } +} + diff --git a/apps/files_external/lib/Lib/Storage/InvalidStorage.php b/apps/files_external/lib/Lib/Storage/InvalidStorage.php new file mode 100644 index 0000000000..bc4ecee836 --- /dev/null +++ b/apps/files_external/lib/Lib/Storage/InvalidStorage.php @@ -0,0 +1,30 @@ + + * + * @copyright Copyright (c) 2016, ownCloud GmbH. + * @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\Lib\Storage; + +use OCP\Files\StorageNotAvailableException; + +class InvalidStorage { + public function __construct($params) { + throw new StorageNotAvailableException(); + } +} diff --git a/apps/files_external/lib/Service/StoragesService.php b/apps/files_external/lib/Service/StoragesService.php index 4e38ea9625..d52bf41046 100644 --- a/apps/files_external/lib/Service/StoragesService.php +++ b/apps/files_external/lib/Service/StoragesService.php @@ -29,6 +29,8 @@ namespace OCA\Files_External\Service; use \OC\Files\Filesystem; +use OCA\Files_External\Lib\Auth\InvalidAuth; +use OCA\Files_External\Lib\Backend\InvalidBackend; use OCA\Files_External\Lib\StorageConfig; use OCA\Files_External\NotFoundException; use \OCA\Files_External\Lib\Backend\Backend; @@ -295,11 +297,11 @@ abstract class StoragesService { ) { $backend = $this->backendService->getBackend($backendIdentifier); if (!$backend) { - throw new \InvalidArgumentException('Unable to get backend for ' . $backendIdentifier); + $backend = new InvalidBackend($backendIdentifier); } $authMechanism = $this->backendService->getAuthMechanism($authMechanismIdentifier); if (!$authMechanism) { - throw new \InvalidArgumentException('Unable to get authentication mechanism for ' . $authMechanismIdentifier); + $authMechanism = new InvalidAuth($authMechanismIdentifier); } $newStorage = new StorageConfig(); $newStorage->setMountPoint($mountPoint); @@ -382,6 +384,10 @@ abstract class StoragesService { $oldStorage = $this->getStorageConfigFromDBMount($existingMount); + if ($oldStorage->getBackend() instanceof InvalidBackend) { + throw new NotFoundException('Storage with id "' . $id . '" cannot be edited due to missing backend'); + } + $removedUsers = array_diff($oldStorage->getApplicableUsers(), $updatedStorage->getApplicableUsers()); $removedGroups = array_diff($oldStorage->getApplicableGroups(), $updatedStorage->getApplicableGroups()); $addedUsers = array_diff($updatedStorage->getApplicableUsers(), $oldStorage->getApplicableUsers()); From 442d4ed24a713cf5f9fc582ca284b0b740940c6b Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Tue, 29 Aug 2017 13:37:13 +0200 Subject: [PATCH 2/5] Use the FailedStorage instead Signed-off-by: Morris Jobke --- .../lib/Lib/Backend/InvalidBackend.php | 2 +- .../lib/Lib/Storage/InvalidStorage.php | 30 ------------------- 2 files changed, 1 insertion(+), 31 deletions(-) delete mode 100644 apps/files_external/lib/Lib/Storage/InvalidStorage.php diff --git a/apps/files_external/lib/Lib/Backend/InvalidBackend.php b/apps/files_external/lib/Lib/Backend/InvalidBackend.php index 1a426c4bbb..c3c5ca0cac 100644 --- a/apps/files_external/lib/Lib/Backend/InvalidBackend.php +++ b/apps/files_external/lib/Lib/Backend/InvalidBackend.php @@ -40,7 +40,7 @@ class InvalidBackend extends Backend { $this->invalidId = $invalidId; $this ->setIdentifier($invalidId) - ->setStorageClass('\OCA\Files_External\Lib\Storage\InvalidStorage') + ->setStorageClass('\OC\Files\Storage\FailedStorage') ->setText('Unknown storage backend ' . $invalidId) ; } diff --git a/apps/files_external/lib/Lib/Storage/InvalidStorage.php b/apps/files_external/lib/Lib/Storage/InvalidStorage.php deleted file mode 100644 index bc4ecee836..0000000000 --- a/apps/files_external/lib/Lib/Storage/InvalidStorage.php +++ /dev/null @@ -1,30 +0,0 @@ - - * - * @copyright Copyright (c) 2016, ownCloud GmbH. - * @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\Lib\Storage; - -use OCP\Files\StorageNotAvailableException; - -class InvalidStorage { - public function __construct($params) { - throw new StorageNotAvailableException(); - } -} From 68ee79b5957d31d6b6e1dd32dea757c8e08024f1 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 1 Sep 2017 15:34:32 +0200 Subject: [PATCH 3/5] set the exception for failedstorage Signed-off-by: Robin Appelman --- .../files_external/lib/Lib/Backend/InvalidBackend.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/apps/files_external/lib/Lib/Backend/InvalidBackend.php b/apps/files_external/lib/Lib/Backend/InvalidBackend.php index c3c5ca0cac..0d51669b3c 100644 --- a/apps/files_external/lib/Lib/Backend/InvalidBackend.php +++ b/apps/files_external/lib/Lib/Backend/InvalidBackend.php @@ -21,6 +21,10 @@ namespace OCA\Files_External\Lib\Backend; +use OCA\Files_External\Lib\Storage\InvalidStorage; +use OCA\Files_External\Lib\StorageConfig; +use OCP\IUser; + /** * Invalid storage backend representing a backend * that could not be resolved @@ -41,8 +45,7 @@ class InvalidBackend extends Backend { $this ->setIdentifier($invalidId) ->setStorageClass('\OC\Files\Storage\FailedStorage') - ->setText('Unknown storage backend ' . $invalidId) - ; + ->setText('Unknown storage backend ' . $invalidId); } /** @@ -53,5 +56,9 @@ class InvalidBackend extends Backend { public function getInvalidId() { return $this->invalidId; } + + public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = null) { + $storage->setBackendOption('exception', new \Exception('Unknown storage backend ' . $this->invalidId)); + } } From e9a58f857903512ba7d0d2a1078551706be906fb Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 1 Sep 2017 15:52:17 +0200 Subject: [PATCH 4/5] show storage as unavailable in the file list Signed-off-by: Robin Appelman --- apps/files_external/lib/Lib/Backend/InvalidBackend.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/files_external/lib/Lib/Backend/InvalidBackend.php b/apps/files_external/lib/Lib/Backend/InvalidBackend.php index 0d51669b3c..6c8eda22d0 100644 --- a/apps/files_external/lib/Lib/Backend/InvalidBackend.php +++ b/apps/files_external/lib/Lib/Backend/InvalidBackend.php @@ -23,6 +23,7 @@ namespace OCA\Files_External\Lib\Backend; use OCA\Files_External\Lib\Storage\InvalidStorage; use OCA\Files_External\Lib\StorageConfig; +use OCP\Files\StorageNotAvailableException; use OCP\IUser; /** @@ -58,7 +59,7 @@ class InvalidBackend extends Backend { } public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = null) { - $storage->setBackendOption('exception', new \Exception('Unknown storage backend ' . $this->invalidId)); + $storage->setBackendOption('exception', new \Exception('Unknown storage backend "' . $this->invalidId . '"', StorageNotAvailableException::STATUS_ERROR)); } } From 0326c2c54fe26dc742ebe00c66bd0363e083712d Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Mon, 4 Sep 2017 14:17:03 +0200 Subject: [PATCH 5/5] Fix broken tests Signed-off-by: Morris Jobke --- core/Controller/LoginController.php | 1 + tests/Core/Controller/LoginControllerTest.php | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/core/Controller/LoginController.php b/core/Controller/LoginController.php index 1243157125..34c80f6b8b 100644 --- a/core/Controller/LoginController.php +++ b/core/Controller/LoginController.php @@ -74,6 +74,7 @@ class LoginController extends Controller { * @param IURLGenerator $urlGenerator * @param ILogger $logger * @param Manager $twoFactorManager + * @param Defaults $defaults */ public function __construct($appName, IRequest $request, diff --git a/tests/Core/Controller/LoginControllerTest.php b/tests/Core/Controller/LoginControllerTest.php index 169beff2b9..e02b8403a2 100644 --- a/tests/Core/Controller/LoginControllerTest.php +++ b/tests/Core/Controller/LoginControllerTest.php @@ -26,6 +26,7 @@ use OC\Core\Controller\LoginController; use OC\User\Session; use OCP\AppFramework\Http\RedirectResponse; use OCP\AppFramework\Http\TemplateResponse; +use OCP\Defaults; use OCP\IConfig; use OCP\ILogger; use OCP\IRequest; @@ -54,6 +55,8 @@ class LoginControllerTest extends TestCase { private $logger; /** @var Manager|\PHPUnit_Framework_MockObject_MockObject */ private $twoFactorManager; + /** @var Defaults|\PHPUnit_Framework_MockObject_MockObject */ + private $defaults; public function setUp() { parent::setUp(); @@ -65,6 +68,7 @@ class LoginControllerTest extends TestCase { $this->urlGenerator = $this->createMock(IURLGenerator::class); $this->logger = $this->createMock(ILogger::class); $this->twoFactorManager = $this->createMock(Manager::class); + $this->defaults = $this->createMock(Defaults::class); $this->loginController = new LoginController( 'core', @@ -75,7 +79,8 @@ class LoginControllerTest extends TestCase { $this->userSession, $this->urlGenerator, $this->logger, - $this->twoFactorManager + $this->twoFactorManager, + $this->defaults ); }