@@ -45,84 +45,84 @@ |
||
| 45 | 45 | |
| 46 | 46 | class TransferOwnership extends Command { |
| 47 | 47 | |
| 48 | - /** @var IUserManager */ |
|
| 49 | - private $userManager; |
|
| 50 | - |
|
| 51 | - /** @var OwnershipTransferService */ |
|
| 52 | - private $transferService; |
|
| 53 | - |
|
| 54 | - public function __construct(IUserManager $userManager, |
|
| 55 | - OwnershipTransferService $transferService) { |
|
| 56 | - parent::__construct(); |
|
| 57 | - $this->userManager = $userManager; |
|
| 58 | - $this->transferService = $transferService; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - protected function configure() { |
|
| 62 | - $this |
|
| 63 | - ->setName('files:transfer-ownership') |
|
| 64 | - ->setDescription('All files and folders are moved to another user - shares are moved as well.') |
|
| 65 | - ->addArgument( |
|
| 66 | - 'source-user', |
|
| 67 | - InputArgument::REQUIRED, |
|
| 68 | - 'owner of files which shall be moved' |
|
| 69 | - ) |
|
| 70 | - ->addArgument( |
|
| 71 | - 'destination-user', |
|
| 72 | - InputArgument::REQUIRED, |
|
| 73 | - 'user who will be the new owner of the files' |
|
| 74 | - ) |
|
| 75 | - ->addOption( |
|
| 76 | - 'path', |
|
| 77 | - null, |
|
| 78 | - InputOption::VALUE_REQUIRED, |
|
| 79 | - 'selectively provide the path to transfer. For example --path="folder_name"', |
|
| 80 | - '' |
|
| 81 | - )->addOption( |
|
| 82 | - 'move', |
|
| 83 | - null, |
|
| 84 | - InputOption::VALUE_NONE, |
|
| 85 | - 'move data from source user to root directory of destination user, which must be empty' |
|
| 86 | - ); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - protected function execute(InputInterface $input, OutputInterface $output): int { |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Check if source and destination users are same. If they are same then just ignore the transfer. |
|
| 93 | - */ |
|
| 94 | - |
|
| 95 | - if ($input->getArgument(('source-user')) === $input->getArgument('destination-user')) { |
|
| 96 | - $output->writeln("<error>Ownership can't be transferred when Source and Destination users are the same user. Please check your input.</error>"); |
|
| 97 | - return 1; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - $sourceUserObject = $this->userManager->get($input->getArgument('source-user')); |
|
| 101 | - $destinationUserObject = $this->userManager->get($input->getArgument('destination-user')); |
|
| 102 | - |
|
| 103 | - if (!$sourceUserObject instanceof IUser) { |
|
| 104 | - $output->writeln("<error>Unknown source user " . $input->getArgument('source-user') . "</error>"); |
|
| 105 | - return 1; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - if (!$destinationUserObject instanceof IUser) { |
|
| 109 | - $output->writeln("<error>Unknown destination user " . $input->getArgument('destination-user') . "</error>"); |
|
| 110 | - return 1; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - try { |
|
| 114 | - $this->transferService->transfer( |
|
| 115 | - $sourceUserObject, |
|
| 116 | - $destinationUserObject, |
|
| 117 | - ltrim($input->getOption('path'), '/'), |
|
| 118 | - $output, |
|
| 119 | - $input->getOption('move') === true |
|
| 120 | - ); |
|
| 121 | - } catch (TransferOwnershipException $e) { |
|
| 122 | - $output->writeln("<error>" . $e->getMessage() . "</error>"); |
|
| 123 | - return $e->getCode() !== 0 ? $e->getCode() : 1; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - return 0; |
|
| 127 | - } |
|
| 48 | + /** @var IUserManager */ |
|
| 49 | + private $userManager; |
|
| 50 | + |
|
| 51 | + /** @var OwnershipTransferService */ |
|
| 52 | + private $transferService; |
|
| 53 | + |
|
| 54 | + public function __construct(IUserManager $userManager, |
|
| 55 | + OwnershipTransferService $transferService) { |
|
| 56 | + parent::__construct(); |
|
| 57 | + $this->userManager = $userManager; |
|
| 58 | + $this->transferService = $transferService; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + protected function configure() { |
|
| 62 | + $this |
|
| 63 | + ->setName('files:transfer-ownership') |
|
| 64 | + ->setDescription('All files and folders are moved to another user - shares are moved as well.') |
|
| 65 | + ->addArgument( |
|
| 66 | + 'source-user', |
|
| 67 | + InputArgument::REQUIRED, |
|
| 68 | + 'owner of files which shall be moved' |
|
| 69 | + ) |
|
| 70 | + ->addArgument( |
|
| 71 | + 'destination-user', |
|
| 72 | + InputArgument::REQUIRED, |
|
| 73 | + 'user who will be the new owner of the files' |
|
| 74 | + ) |
|
| 75 | + ->addOption( |
|
| 76 | + 'path', |
|
| 77 | + null, |
|
| 78 | + InputOption::VALUE_REQUIRED, |
|
| 79 | + 'selectively provide the path to transfer. For example --path="folder_name"', |
|
| 80 | + '' |
|
| 81 | + )->addOption( |
|
| 82 | + 'move', |
|
| 83 | + null, |
|
| 84 | + InputOption::VALUE_NONE, |
|
| 85 | + 'move data from source user to root directory of destination user, which must be empty' |
|
| 86 | + ); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Check if source and destination users are same. If they are same then just ignore the transfer. |
|
| 93 | + */ |
|
| 94 | + |
|
| 95 | + if ($input->getArgument(('source-user')) === $input->getArgument('destination-user')) { |
|
| 96 | + $output->writeln("<error>Ownership can't be transferred when Source and Destination users are the same user. Please check your input.</error>"); |
|
| 97 | + return 1; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + $sourceUserObject = $this->userManager->get($input->getArgument('source-user')); |
|
| 101 | + $destinationUserObject = $this->userManager->get($input->getArgument('destination-user')); |
|
| 102 | + |
|
| 103 | + if (!$sourceUserObject instanceof IUser) { |
|
| 104 | + $output->writeln("<error>Unknown source user " . $input->getArgument('source-user') . "</error>"); |
|
| 105 | + return 1; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + if (!$destinationUserObject instanceof IUser) { |
|
| 109 | + $output->writeln("<error>Unknown destination user " . $input->getArgument('destination-user') . "</error>"); |
|
| 110 | + return 1; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + try { |
|
| 114 | + $this->transferService->transfer( |
|
| 115 | + $sourceUserObject, |
|
| 116 | + $destinationUserObject, |
|
| 117 | + ltrim($input->getOption('path'), '/'), |
|
| 118 | + $output, |
|
| 119 | + $input->getOption('move') === true |
|
| 120 | + ); |
|
| 121 | + } catch (TransferOwnershipException $e) { |
|
| 122 | + $output->writeln("<error>" . $e->getMessage() . "</error>"); |
|
| 123 | + return $e->getCode() !== 0 ? $e->getCode() : 1; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + return 0; |
|
| 127 | + } |
|
| 128 | 128 | } |