Skip to content

Commit dfe85ae

Browse files
authored
Merge pull request #18025 from nextcloud/refactor/extract-ownership-transfer-service
Extract transfer ownership logic into a reusable service
2 parents 7015501 + 07c5f37 commit dfe85ae

File tree

5 files changed

+333
-220
lines changed

5 files changed

+333
-220
lines changed

apps/files/composer/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
'OCA\\Files\\Controller\\ViewController' => $baseDir . '/../lib/Controller/ViewController.php',
3535
'OCA\\Files\\Event\\LoadAdditionalScriptsEvent' => $baseDir . '/../lib/Event/LoadAdditionalScriptsEvent.php',
3636
'OCA\\Files\\Event\\LoadSidebar' => $baseDir . '/../lib/Event/LoadSidebar.php',
37+
'OCA\\Files\\Exception\\TransferOwnershipException' => $baseDir . '/../lib/Exception/TransferOwnershipException.php',
3738
'OCA\\Files\\Helper' => $baseDir . '/../lib/Helper.php',
3839
'OCA\\Files\\Listener\\LegacyLoadAdditionalScriptsAdapter' => $baseDir . '/../lib/Listener/LegacyLoadAdditionalScriptsAdapter.php',
40+
'OCA\\Files\\Service\\OwnershipTransferService' => $baseDir . '/../lib/Service/OwnershipTransferService.php',
3941
'OCA\\Files\\Service\\TagService' => $baseDir . '/../lib/Service/TagService.php',
4042
);

apps/files/composer/composer/autoload_static.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ class ComposerStaticInitFiles
4949
'OCA\\Files\\Controller\\ViewController' => __DIR__ . '/..' . '/../lib/Controller/ViewController.php',
5050
'OCA\\Files\\Event\\LoadAdditionalScriptsEvent' => __DIR__ . '/..' . '/../lib/Event/LoadAdditionalScriptsEvent.php',
5151
'OCA\\Files\\Event\\LoadSidebar' => __DIR__ . '/..' . '/../lib/Event/LoadSidebar.php',
52+
'OCA\\Files\\Exception\\TransferOwnershipException' => __DIR__ . '/..' . '/../lib/Exception/TransferOwnershipException.php',
5253
'OCA\\Files\\Helper' => __DIR__ . '/..' . '/../lib/Helper.php',
5354
'OCA\\Files\\Listener\\LegacyLoadAdditionalScriptsAdapter' => __DIR__ . '/..' . '/../lib/Listener/LegacyLoadAdditionalScriptsAdapter.php',
55+
'OCA\\Files\\Service\\OwnershipTransferService' => __DIR__ . '/..' . '/../lib/Service/OwnershipTransferService.php',
5456
'OCA\\Files\\Service\\TagService' => __DIR__ . '/..' . '/../lib/Service/TagService.php',
5557
);
5658

Lines changed: 23 additions & 220 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<?php
1+
<?php declare(strict_types=1);
2+
23
/**
34
* @copyright Copyright (c) 2016, ownCloud, Inc.
45
*
@@ -28,63 +29,29 @@
2829

2930
namespace OCA\Files\Command;
3031

31-
use OC\Files\Filesystem;
32-
use OC\Files\View;
33-
use OCP\Files\FileInfo;
34-
use OCP\Files\IHomeStorage;
35-
use OCP\Files\IRootFolder;
36-
use OCP\Files\Mount\IMountManager;
32+
use OCA\Files\Exception\TransferOwnershipException;
33+
use OCA\Files\Service\OwnershipTransferService;
3734
use OCP\IUser;
3835
use OCP\IUserManager;
39-
use OCP\Share\IManager;
40-
use OCP\Share\IShare;
4136
use Symfony\Component\Console\Command\Command;
42-
use Symfony\Component\Console\Helper\ProgressBar;
4337
use Symfony\Component\Console\Input\InputArgument;
4438
use Symfony\Component\Console\Input\InputInterface;
4539
use Symfony\Component\Console\Input\InputOption;
4640
use Symfony\Component\Console\Output\OutputInterface;
4741

4842
class TransferOwnership extends Command {
4943

50-
/** @var IUserManager $userManager */
44+
/** @var IUserManager */
5145
private $userManager;
5246

53-
/** @var IManager */
54-
private $shareManager;
55-
56-
/** @var IMountManager */
57-
private $mountManager;
58-
59-
/** @var FileInfo[] */
60-
private $allFiles = [];
61-
62-
/** @var FileInfo[] */
63-
private $encryptedFiles = [];
64-
65-
/** @var IShare[] */
66-
private $shares = [];
67-
68-
/** @var string */
69-
private $sourceUser;
70-
71-
/** @var string */
72-
private $destinationUser;
73-
74-
/** @var string */
75-
private $sourcePath;
76-
77-
/** @var string */
78-
private $finalTarget;
47+
/** @var OwnershipTransferService */
48+
private $transferService;
7949

8050
public function __construct(IUserManager $userManager,
81-
IManager $shareManager,
82-
IMountManager $mountManager,
83-
IRootFolder $rootFolder) {
84-
$this->userManager = $userManager;
85-
$this->shareManager = $shareManager;
86-
$this->mountManager = $mountManager;
51+
OwnershipTransferService $transferService) {
8752
parent::__construct();
53+
$this->userManager = $userManager;
54+
$this->transferService = $transferService;
8855
}
8956

9057
protected function configure() {
@@ -115,192 +82,28 @@ protected function execute(InputInterface $input, OutputInterface $output) {
11582
$destinationUserObject = $this->userManager->get($input->getArgument('destination-user'));
11683

11784
if (!$sourceUserObject instanceof IUser) {
118-
$output->writeln("<error>Unknown source user $this->sourceUser</error>");
85+
$output->writeln("<error>Unknown source user " . $input->getArgument('source-user') . "</error>");
11986
return 1;
12087
}
12188

12289
if (!$destinationUserObject instanceof IUser) {
123-
$output->writeln("<error>Unknown destination user $this->destinationUser</error>");
90+
$output->writeln("<error>Unknown destination user " . $input->getArgument('destination-user') . "</error>");
12491
return 1;
12592
}
12693

127-
$this->sourceUser = $sourceUserObject->getUID();
128-
$this->destinationUser = $destinationUserObject->getUID();
129-
$sourcePathOption = ltrim($input->getOption('path'), '/');
130-
$this->sourcePath = rtrim($this->sourceUser . '/files/' . $sourcePathOption, '/');
131-
132-
// target user has to be ready
133-
if (!\OC::$server->getEncryptionManager()->isReadyForUser($this->destinationUser)) {
134-
$output->writeln("<error>The target user is not ready to accept files. The user has at least to be logged in once.</error>");
135-
return 2;
136-
}
137-
138-
$date = date('Y-m-d H-i-s');
139-
$this->finalTarget = "$this->destinationUser/files/transferred from $this->sourceUser on $date";
140-
141-
// setup filesystem
142-
Filesystem::initMountPoints($this->sourceUser);
143-
Filesystem::initMountPoints($this->destinationUser);
144-
145-
$view = new View();
146-
if (!$view->is_dir($this->sourcePath)) {
147-
$output->writeln("<error>Unknown path provided: $sourcePathOption</error>");
148-
return 1;
149-
}
150-
151-
// analyse source folder
152-
$this->analyse($output);
153-
154-
// collect all the shares
155-
$this->collectUsersShares($output);
156-
157-
// transfer the files
158-
$this->transfer($output);
159-
160-
// restore the shares
161-
$this->restoreShares($output);
162-
}
163-
164-
private function walkFiles(View $view, $path, \Closure $callBack) {
165-
foreach ($view->getDirectoryContent($path) as $fileInfo) {
166-
if (!$callBack($fileInfo)) {
167-
return;
168-
}
169-
if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) {
170-
$this->walkFiles($view, $fileInfo->getPath(), $callBack);
171-
}
172-
}
173-
}
174-
175-
/**
176-
* @param OutputInterface $output
177-
* @throws \Exception
178-
*/
179-
protected function analyse(OutputInterface $output) {
180-
$view = new View();
181-
182-
$output->writeln('Validating quota');
183-
$size = $view->getFileInfo($this->sourcePath, false)->getSize(false);
184-
$freeSpace = $view->free_space($this->destinationUser . '/files/');
185-
if ($size > $freeSpace) {
186-
$output->writeln('<error>Target user does not have enough free space available</error>');
187-
throw new \Exception('Execution terminated');
188-
}
189-
190-
$output->writeln("Analysing files of $this->sourceUser ...");
191-
$progress = new ProgressBar($output);
192-
$progress->start();
193-
$self = $this;
194-
195-
$this->walkFiles($view, $this->sourcePath,
196-
function (FileInfo $fileInfo) use ($progress, $self) {
197-
if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) {
198-
// only analyze into folders from main storage,
199-
if (!$fileInfo->getStorage()->instanceOfStorage(IHomeStorage::class)) {
200-
return false;
201-
}
202-
return true;
203-
}
204-
$progress->advance();
205-
$this->allFiles[] = $fileInfo;
206-
if ($fileInfo->isEncrypted()) {
207-
$this->encryptedFiles[] = $fileInfo;
208-
}
209-
return true;
210-
});
211-
$progress->finish();
212-
$output->writeln('');
213-
214-
// no file is allowed to be encrypted
215-
if (!empty($this->encryptedFiles)) {
216-
$output->writeln("<error>Some files are encrypted - please decrypt them first</error>");
217-
foreach($this->encryptedFiles as $encryptedFile) {
218-
/** @var FileInfo $encryptedFile */
219-
$output->writeln(" " . $encryptedFile->getPath());
220-
}
221-
throw new \Exception('Execution terminated.');
222-
}
223-
}
224-
225-
/**
226-
* @param OutputInterface $output
227-
*/
228-
private function collectUsersShares(OutputInterface $output) {
229-
$output->writeln("Collecting all share information for files and folder of $this->sourceUser ...");
230-
231-
$progress = new ProgressBar($output, count($this->shares));
232-
foreach([\OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_LINK, \OCP\Share::SHARE_TYPE_REMOTE, \OCP\Share::SHARE_TYPE_ROOM] as $shareType) {
233-
$offset = 0;
234-
while (true) {
235-
$sharePage = $this->shareManager->getSharesBy($this->sourceUser, $shareType, null, true, 50, $offset);
236-
$progress->advance(count($sharePage));
237-
if (empty($sharePage)) {
238-
break;
239-
}
240-
$this->shares = array_merge($this->shares, $sharePage);
241-
$offset += 50;
242-
}
94+
try {
95+
$this->transferService->transfer(
96+
$sourceUserObject,
97+
$destinationUserObject,
98+
ltrim($input->getOption('path'), '/'),
99+
$output
100+
);
101+
} catch (TransferOwnershipException $e) {
102+
$output->writeln("<error>" . $e->getMessage() . "</error>");
103+
return $e->getCode() !== 0 ? $e->getCode() : 1;
243104
}
244105

245-
$progress->finish();
246-
$output->writeln('');
106+
return 0;
247107
}
248108

249-
/**
250-
* @param OutputInterface $output
251-
*/
252-
protected function transfer(OutputInterface $output) {
253-
$view = new View();
254-
$output->writeln("Transferring files to $this->finalTarget ...");
255-
256-
// This change will help user to transfer the folder specified using --path option.
257-
// Else only the content inside folder is transferred which is not correct.
258-
if($this->sourcePath !== "$this->sourceUser/files") {
259-
$view->mkdir($this->finalTarget);
260-
$this->finalTarget = $this->finalTarget . '/' . basename($this->sourcePath);
261-
}
262-
$view->rename($this->sourcePath, $this->finalTarget);
263-
if (!is_dir("$this->sourceUser/files")) {
264-
// because the files folder is moved away we need to recreate it
265-
$view->mkdir("$this->sourceUser/files");
266-
}
267-
}
268-
269-
/**
270-
* @param OutputInterface $output
271-
*/
272-
private function restoreShares(OutputInterface $output) {
273-
$output->writeln("Restoring shares ...");
274-
$progress = new ProgressBar($output, count($this->shares));
275-
276-
foreach($this->shares as $share) {
277-
try {
278-
if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
279-
$share->getSharedWith() === $this->destinationUser) {
280-
// Unmount the shares before deleting, so we don't try to get the storage later on.
281-
$shareMountPoint = $this->mountManager->find('/' . $this->destinationUser . '/files' . $share->getTarget());
282-
if ($shareMountPoint) {
283-
$this->mountManager->removeMount($shareMountPoint->getMountPoint());
284-
}
285-
$this->shareManager->deleteShare($share);
286-
} else {
287-
if ($share->getShareOwner() === $this->sourceUser) {
288-
$share->setShareOwner($this->destinationUser);
289-
}
290-
if ($share->getSharedBy() === $this->sourceUser) {
291-
$share->setSharedBy($this->destinationUser);
292-
}
293-
294-
$this->shareManager->updateShare($share);
295-
}
296-
} catch (\OCP\Files\NotFoundException $e) {
297-
$output->writeln('<error>Share with id ' . $share->getId() . ' points at deleted file, skipping</error>');
298-
} catch (\Exception $e) {
299-
$output->writeln('<error>Could not restore share with id ' . $share->getId() . ':' . $e->getTraceAsString() . '</error>');
300-
}
301-
$progress->advance();
302-
}
303-
$progress->finish();
304-
$output->writeln('');
305-
}
306109
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php declare(strict_types=1);
2+
3+
/**
4+
* @copyright 2019 Christoph Wurst <[email protected]>
5+
*
6+
* @author 2019 Christoph Wurst <[email protected]>
7+
*
8+
* @license GNU AGPL version 3 or any later version
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU Affero General Public License as
12+
* published by the Free Software Foundation, either version 3 of the
13+
* License, or (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Affero General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Affero General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
*/
23+
24+
namespace OCA\Files\Exception;
25+
26+
use Exception;
27+
28+
class TransferOwnershipException extends Exception {
29+
30+
}

0 commit comments

Comments
 (0)