aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files_external/lib/Command/Import.php
diff options
context:
space:
mode:
Diffstat (limited to 'apps/files_external/lib/Command/Import.php')
-rw-r--r--apps/files_external/lib/Command/Import.php100
1 files changed, 36 insertions, 64 deletions
diff --git a/apps/files_external/lib/Command/Import.php b/apps/files_external/lib/Command/Import.php
index eab7dd9a6be..a9ed76fbe40 100644
--- a/apps/files_external/lib/Command/Import.php
+++ b/apps/files_external/lib/Command/Import.php
@@ -1,26 +1,9 @@
<?php
+
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Robin Appelman <robin@icewind.nl>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License, version 3,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
+ * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\Files_External\Command;
@@ -30,6 +13,7 @@ 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\StoragesService;
use OCA\Files_External\Service\UserStoragesService;
use OCP\IUserManager;
use OCP\IUserSession;
@@ -40,27 +24,15 @@ use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class Import extends Base {
- private GlobalStoragesService $globalService;
- private UserStoragesService $userService;
- private IUserSession $userSession;
- private IUserManager $userManager;
- private ImportLegacyStoragesService $importLegacyStorageService;
- private BackendService $backendService;
-
- public function __construct(GlobalStoragesService $globalService,
- UserStoragesService $userService,
- IUserSession $userSession,
- IUserManager $userManager,
- ImportLegacyStoragesService $importLegacyStorageService,
- BackendService $backendService
+ public function __construct(
+ private GlobalStoragesService $globalService,
+ private UserStoragesService $userService,
+ private IUserSession $userSession,
+ private IUserManager $userManager,
+ private ImportLegacyStoragesService $importLegacyStorageService,
+ private 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(): void {
@@ -88,25 +60,25 @@ class Import extends Base {
}
protected function execute(InputInterface $input, OutputInterface $output): int {
- $user = (string) $input->getOption('user');
+ $user = (string)$input->getOption('user');
$path = $input->getArgument('path');
if ($path === '-') {
$json = file_get_contents('php://stdin');
} else {
if (!file_exists($path)) {
$output->writeln('<error>File not found: ' . $path . '</error>');
- return 1;
+ return self::FAILURE;
}
$json = file_get_contents($path);
}
if (!is_string($json) || strlen($json) < 2) {
$output->writeln('<error>Error while reading json</error>');
- return 1;
+ return self::FAILURE;
}
$data = json_decode($json, true);
if (!is_array($data)) {
$output->writeln('<error>Error while parsing json</error>');
- return 1;
+ return self::FAILURE;
}
$isLegacy = isset($data['user']) || isset($data['group']);
@@ -116,7 +88,7 @@ class Import extends Base {
foreach ($mounts as $mount) {
if ($mount->getBackendOption('password') === false) {
$output->writeln('<error>Failed to decrypt password</error>');
- return 1;
+ return self::FAILURE;
}
}
} else {
@@ -141,13 +113,13 @@ class Import extends Base {
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()
+ $existingMount->getMountPoint() === $mount->getMountPoint()
+ && $existingMount->getApplicableGroups() === $mount->getApplicableGroups()
+ && $existingMount->getApplicableUsers() === $mount->getApplicableUsers()
+ && $existingMount->getBackendOptions() === $mount->getBackendOptions()
) {
- $output->writeln("<error>Duplicate mount (" . $mount->getMountPoint() . ")</error>");
- return 1;
+ $output->writeln('<error>Duplicate mount (' . $mount->getMountPoint() . ')</error>');
+ return self::FAILURE;
}
}
}
@@ -155,7 +127,7 @@ class Import extends Base {
if ($input->getOption('dry')) {
if (count($mounts) === 0) {
$output->writeln('<error>No mounts to be imported</error>');
- return 1;
+ return self::FAILURE;
}
$listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager);
$listInput = new ArrayInput([], $listCommand->getDefinition());
@@ -167,7 +139,7 @@ class Import extends Base {
$storageService->addStorage($mount);
}
}
- return 0;
+ return self::SUCCESS;
}
private function parseData(array $data): StorageConfig {
@@ -178,8 +150,8 @@ class Import extends Base {
$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'] : []);
+ $mount->setApplicableUsers($data['applicable_users'] ?? []);
+ $mount->setApplicableGroups($data['applicable_groups'] ?? []);
return $mount;
}
@@ -192,16 +164,16 @@ class Import extends Base {
}
}
- 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 {
+ protected function getStorageService(string $userId): StoragesService {
+ if (empty($userId)) {
return $this->globalService;
}
+
+ $user = $this->userManager->get($userId);
+ if (is_null($user)) {
+ throw new NoUserException("user $userId not found");
+ }
+ $this->userSession->setUser($user);
+ return $this->userService;
}
}