]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add option to transfer-ownership to move data
authorTobia De Koninck <LEDfan@users.noreply.github.com>
Fri, 3 Jan 2020 07:43:39 +0000 (08:43 +0100)
committerRoeland Jago Douma <roeland@famdouma.nl>
Mon, 3 Feb 2020 10:15:56 +0000 (11:15 +0100)
This will move the home folder of own user to another user. Only allowed
if that other user's home folder is empty.
Can be used as workaround to rename users.

Signed-off-by: Tobia De Koninck <LEDfan@users.noreply.github.com>
apps/files/lib/Command/TransferOwnership.php
apps/files/lib/Service/OwnershipTransferService.php

index cad1b1b46cbe9546647f978698ca748b005cdf3c..2675a7dd1bb8b2b515235f01856499abd492c4a9 100644 (file)
@@ -77,7 +77,12 @@ class TransferOwnership extends Command {
                                InputOption::VALUE_REQUIRED,
                                'selectively provide the path to transfer. For example --path="folder_name"',
                                ''
-                       );
+                       )->addOption(
+                               'move',
+                               null,
+                               InputOption::VALUE_NONE,
+                               'move data from source user to root directory of destination user, which must be empty'
+               );
        }
 
        protected function execute(InputInterface $input, OutputInterface $output) {
@@ -99,7 +104,8 @@ class TransferOwnership extends Command {
                                $sourceUserObject,
                                $destinationUserObject,
                                ltrim($input->getOption('path'), '/'),
-                               $output
+                               $output,
+                               $input->getOption('move') === true
                        );
                } catch (TransferOwnershipException $e) {
                        $output->writeln("<error>" . $e->getMessage() . "</error>");
index 8530edd17b17fca7af44e1edae13d611897064ce..8af894a01673a79537239e8acf6ab5e959876dfa 100644 (file)
@@ -71,12 +71,16 @@ class OwnershipTransferService {
         * @param IUser $destinationUser
         * @param string $path
         *
+        * @param OutputInterface|null $output
+        * @param bool $move
         * @throws TransferOwnershipException
+        * @throws \OC\User\NoUserException
         */
        public function transfer(IUser $sourceUser,
                                                         IUser $destinationUser,
                                                         string $path,
-                                                        ?OutputInterface $output = null): void {
+                                                        ?OutputInterface $output = null,
+                                                        bool $move = false): void {
                $output = $output ?? new NullOutput();
                $sourceUid = $sourceUser->getUID();
                $destinationUid = $destinationUser->getUID();
@@ -87,8 +91,12 @@ class OwnershipTransferService {
                        throw new TransferOwnershipException("The target user is not ready to accept files. The user has at least to have logged in once.", 2);
                }
 
-               $date = date('Y-m-d H-i-s');
-               $finalTarget = "$destinationUid/files/transferred from $sourceUid on $date";
+               if ($move) {
+                       $finalTarget = "$destinationUid/files/";
+               } else {
+                       $date = date('Y-m-d H-i-s');
+                       $finalTarget = "$destinationUid/files/transferred from $sourceUid on $date";
+               }
 
                // setup filesystem
                Filesystem::initMountPoints($sourceUid);
@@ -99,6 +107,11 @@ class OwnershipTransferService {
                        throw new TransferOwnershipException("Unknown path provided: $path", 1);
                }
 
+               if ($move && (!$view->is_dir($finalTarget) || count($view->getDirectoryContent($finalTarget)) > 0)) {
+                       throw new TransferOwnershipException("Destination path does not exists or is not empty", 1);
+               }
+
+
                // analyse source folder
                $this->analyse(
                        $sourceUid,