|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Carla Schroder <[email protected]> |
|
6
|
|
|
* @author Joas Schilling <[email protected]> |
|
7
|
|
|
* @author Thomas Müller <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* @license AGPL-3.0 |
|
10
|
|
|
* |
|
11
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
12
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
13
|
|
|
* as published by the Free Software Foundation. |
|
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, version 3, |
|
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace OCA\Files\Command; |
|
26
|
|
|
|
|
27
|
|
|
use OC\Files\Filesystem; |
|
28
|
|
|
use OC\Files\View; |
|
29
|
|
|
use OCP\Files\FileInfo; |
|
30
|
|
|
use OCP\Files\Mount\IMountManager; |
|
31
|
|
|
use OCP\IUser; |
|
32
|
|
|
use OCP\IUserManager; |
|
33
|
|
|
use OCP\Share\IManager; |
|
34
|
|
|
use OCP\Share\IShare; |
|
35
|
|
|
use Symfony\Component\Console\Command\Command; |
|
36
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
|
37
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
38
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
39
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
40
|
|
|
|
|
41
|
|
|
class TransferOwnership extends Command { |
|
42
|
|
|
|
|
43
|
|
|
/** @var IUserManager $userManager */ |
|
44
|
|
|
private $userManager; |
|
45
|
|
|
|
|
46
|
|
|
/** @var IManager */ |
|
47
|
|
|
private $shareManager; |
|
48
|
|
|
|
|
49
|
|
|
/** @var IMountManager */ |
|
50
|
|
|
private $mountManager; |
|
51
|
|
|
|
|
52
|
|
|
/** @var FileInfo[] */ |
|
53
|
|
|
private $allFiles = []; |
|
54
|
|
|
|
|
55
|
|
|
/** @var FileInfo[] */ |
|
56
|
|
|
private $encryptedFiles = []; |
|
57
|
|
|
|
|
58
|
|
|
/** @var IShare[] */ |
|
59
|
|
|
private $shares = []; |
|
60
|
|
|
|
|
61
|
|
|
/** @var string */ |
|
62
|
|
|
private $sourceUser; |
|
63
|
|
|
|
|
64
|
|
|
/** @var string */ |
|
65
|
|
|
private $destinationUser; |
|
66
|
|
|
|
|
67
|
|
|
/** @var string */ |
|
68
|
|
|
private $finalTarget; |
|
69
|
|
|
|
|
70
|
|
|
public function __construct(IUserManager $userManager, IManager $shareManager, IMountManager $mountManager) { |
|
71
|
|
|
$this->userManager = $userManager; |
|
72
|
|
|
$this->shareManager = $shareManager; |
|
73
|
|
|
$this->mountManager = $mountManager; |
|
74
|
|
|
parent::__construct(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
protected function configure() { |
|
78
|
|
|
$this |
|
79
|
|
|
->setName('files:transfer-ownership') |
|
80
|
|
|
->setDescription('All files and folders are moved to another user - shares are moved as well.') |
|
81
|
|
|
->addArgument( |
|
82
|
|
|
'source-user', |
|
83
|
|
|
InputArgument::REQUIRED, |
|
84
|
|
|
'owner of files which shall be moved' |
|
85
|
|
|
) |
|
86
|
|
|
->addArgument( |
|
87
|
|
|
'destination-user', |
|
88
|
|
|
InputArgument::REQUIRED, |
|
89
|
|
|
'user who will be the new owner of the files' |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) { |
|
94
|
|
|
$sourceUserObject = $this->userManager->get($input->getArgument('source-user')); |
|
95
|
|
|
$destinationUserObject = $this->userManager->get($input->getArgument('destination-user')); |
|
96
|
|
|
|
|
97
|
|
|
if (!$sourceUserObject instanceof IUser) { |
|
98
|
|
|
$output->writeln("<error>Unknown source user $this->sourceUser</error>"); |
|
99
|
|
|
return 1; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if (!$destinationUserObject instanceof IUser) { |
|
103
|
|
|
$output->writeln("<error>Unknown destination user $this->destinationUser</error>"); |
|
104
|
|
|
return 1; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$this->sourceUser = $sourceUserObject->getUID(); |
|
108
|
|
|
$this->destinationUser = $destinationUserObject->getUID(); |
|
109
|
|
|
|
|
110
|
|
|
// target user has to be ready |
|
111
|
|
|
if (!\OC::$server->getEncryptionManager()->isReadyForUser($this->destinationUser)) { |
|
112
|
|
|
$output->writeln("<error>The target user is not ready to accept files. The user has at least to be logged in once.</error>"); |
|
113
|
|
|
return 2; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$date = date('c'); |
|
117
|
|
|
$this->finalTarget = "$this->destinationUser/files/transferred from $this->sourceUser on $date"; |
|
118
|
|
|
|
|
119
|
|
|
// setup filesystem |
|
120
|
|
|
Filesystem::initMountPoints($this->sourceUser); |
|
121
|
|
|
Filesystem::initMountPoints($this->destinationUser); |
|
122
|
|
|
|
|
123
|
|
|
// analyse source folder |
|
124
|
|
|
$this->analyse($output); |
|
125
|
|
|
|
|
126
|
|
|
// collect all the shares |
|
127
|
|
|
$this->collectUsersShares($output); |
|
128
|
|
|
|
|
129
|
|
|
// transfer the files |
|
130
|
|
|
$this->transfer($output); |
|
131
|
|
|
|
|
132
|
|
|
// restore the shares |
|
133
|
|
|
$this->restoreShares($output); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
private function walkFiles(View $view, $path, \Closure $callBack) { |
|
137
|
|
|
foreach ($view->getDirectoryContent($path) as $fileInfo) { |
|
138
|
|
|
if (!$callBack($fileInfo)) { |
|
139
|
|
|
return; |
|
140
|
|
|
} |
|
141
|
|
|
if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) { |
|
142
|
|
|
$this->walkFiles($view, $fileInfo->getPath(), $callBack); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param OutputInterface $output |
|
149
|
|
|
* @throws \Exception |
|
150
|
|
|
*/ |
|
151
|
|
|
protected function analyse(OutputInterface $output) { |
|
152
|
|
|
$view = new View(); |
|
153
|
|
|
$output->writeln("Analysing files of $this->sourceUser ..."); |
|
154
|
|
|
$progress = new ProgressBar($output); |
|
155
|
|
|
$progress->start(); |
|
156
|
|
|
$self = $this; |
|
157
|
|
|
$this->walkFiles($view, "$this->sourceUser/files", |
|
158
|
|
|
function (FileInfo $fileInfo) use ($progress, $self) { |
|
159
|
|
|
if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) { |
|
160
|
|
|
return true; |
|
161
|
|
|
} |
|
162
|
|
|
$progress->advance(); |
|
163
|
|
|
$this->allFiles[] = $fileInfo; |
|
164
|
|
|
if ($fileInfo->isEncrypted()) { |
|
165
|
|
|
$this->encryptedFiles[] = $fileInfo; |
|
166
|
|
|
} |
|
167
|
|
|
return true; |
|
168
|
|
|
}); |
|
169
|
|
|
$progress->finish(); |
|
170
|
|
|
$output->writeln(''); |
|
171
|
|
|
|
|
172
|
|
|
// no file is allowed to be encrypted |
|
173
|
|
|
if (!empty($this->encryptedFiles)) { |
|
174
|
|
|
$output->writeln("<error>Some files are encrypted - please decrypt them first</error>"); |
|
175
|
|
|
foreach($this->encryptedFiles as $encryptedFile) { |
|
176
|
|
|
/** @var FileInfo $encryptedFile */ |
|
177
|
|
|
$output->writeln(" " . $encryptedFile->getPath()); |
|
178
|
|
|
} |
|
179
|
|
|
throw new \Exception('Execution terminated.'); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @param OutputInterface $output |
|
186
|
|
|
*/ |
|
187
|
|
|
private function collectUsersShares(OutputInterface $output) { |
|
188
|
|
|
$output->writeln("Collecting all share information for files and folder of $this->sourceUser ..."); |
|
189
|
|
|
|
|
190
|
|
|
$progress = new ProgressBar($output, count($this->shares)); |
|
191
|
|
|
foreach([\OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_LINK, \OCP\Share::SHARE_TYPE_REMOTE] as $shareType) { |
|
192
|
|
|
$offset = 0; |
|
193
|
|
|
while (true) { |
|
194
|
|
|
$sharePage = $this->shareManager->getSharesBy($this->sourceUser, $shareType, null, true, 50, $offset); |
|
195
|
|
|
$progress->advance(count($sharePage)); |
|
196
|
|
|
if (empty($sharePage)) { |
|
197
|
|
|
break; |
|
198
|
|
|
} |
|
199
|
|
|
$this->shares = array_merge($this->shares, $sharePage); |
|
200
|
|
|
$offset += 50; |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
$progress->finish(); |
|
205
|
|
|
$output->writeln(''); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @param OutputInterface $output |
|
210
|
|
|
*/ |
|
211
|
|
|
protected function transfer(OutputInterface $output) { |
|
212
|
|
|
$view = new View(); |
|
213
|
|
|
$output->writeln("Transferring files to $this->finalTarget ..."); |
|
214
|
|
|
$view->rename("$this->sourceUser/files", $this->finalTarget); |
|
215
|
|
|
// because the files folder is moved away we need to recreate it |
|
216
|
|
|
$view->mkdir("$this->sourceUser/files"); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @param OutputInterface $output |
|
221
|
|
|
*/ |
|
222
|
|
|
private function restoreShares(OutputInterface $output) { |
|
223
|
|
|
$output->writeln("Restoring shares ..."); |
|
224
|
|
|
$progress = new ProgressBar($output, count($this->shares)); |
|
225
|
|
|
|
|
226
|
|
|
foreach($this->shares as $share) { |
|
227
|
|
|
try { |
|
228
|
|
|
if ($share->getSharedWith() === $this->destinationUser) { |
|
229
|
|
|
// Unmount the shares before deleting, so we don't try to get the storage later on. |
|
230
|
|
|
$shareMountPoint = $this->mountManager->find('/' . $this->destinationUser . '/files' . $share->getTarget()); |
|
231
|
|
|
if ($shareMountPoint) { |
|
232
|
|
|
$this->mountManager->removeMount($shareMountPoint->getMountPoint()); |
|
233
|
|
|
} |
|
234
|
|
|
$this->shareManager->deleteShare($share); |
|
235
|
|
|
} else { |
|
236
|
|
|
if ($share->getShareOwner() === $this->sourceUser) { |
|
237
|
|
|
$share->setShareOwner($this->destinationUser); |
|
238
|
|
|
} |
|
239
|
|
|
if ($share->getSharedBy() === $this->sourceUser) { |
|
240
|
|
|
$share->setSharedBy($this->destinationUser); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
$this->shareManager->updateShare($share); |
|
244
|
|
|
} |
|
245
|
|
|
} catch (\OCP\Files\NotFoundException $e) { |
|
246
|
|
|
$output->writeln('<error>Share with id ' . $share->getId() . ' points at deleted file, skipping</error>'); |
|
247
|
|
|
} catch (\Exception $e) { |
|
248
|
|
|
$output->writeln('<error>Could not restore share with id ' . $share->getId() . ':' . $e->getTraceAsString() . '</error>'); |
|
249
|
|
|
} |
|
250
|
|
|
$progress->advance(); |
|
251
|
|
|
} |
|
252
|
|
|
$progress->finish(); |
|
253
|
|
|
$output->writeln(''); |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|