You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TransferOwnership.php 9.0KB

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