Passed
Push — master ( 701550...dfe85a )
by Roeland
16:47 queued 13s
created
apps/files/lib/Command/TransferOwnership.php 1 patch
Indentation   +56 added lines, -56 removed lines patch added patch discarded remove patch
@@ -41,69 +41,69 @@
 block discarded – undo
41 41
 
42 42
 class TransferOwnership extends Command {
43 43
 
44
-	/** @var IUserManager */
45
-	private $userManager;
44
+    /** @var IUserManager */
45
+    private $userManager;
46 46
 
47
-	/** @var OwnershipTransferService */
48
-	private $transferService;
47
+    /** @var OwnershipTransferService */
48
+    private $transferService;
49 49
 
50
-	public function __construct(IUserManager $userManager,
51
-								OwnershipTransferService $transferService) {
52
-		parent::__construct();
53
-		$this->userManager = $userManager;
54
-		$this->transferService = $transferService;
55
-	}
50
+    public function __construct(IUserManager $userManager,
51
+                                OwnershipTransferService $transferService) {
52
+        parent::__construct();
53
+        $this->userManager = $userManager;
54
+        $this->transferService = $transferService;
55
+    }
56 56
 
57
-	protected function configure() {
58
-		$this
59
-			->setName('files:transfer-ownership')
60
-			->setDescription('All files and folders are moved to another user - shares are moved as well.')
61
-			->addArgument(
62
-				'source-user',
63
-				InputArgument::REQUIRED,
64
-				'owner of files which shall be moved'
65
-			)
66
-			->addArgument(
67
-				'destination-user',
68
-				InputArgument::REQUIRED,
69
-				'user who will be the new owner of the files'
70
-			)
71
-			->addOption(
72
-				'path',
73
-				null,
74
-				InputOption::VALUE_REQUIRED,
75
-				'selectively provide the path to transfer. For example --path="folder_name"',
76
-				''
77
-			);
78
-	}
57
+    protected function configure() {
58
+        $this
59
+            ->setName('files:transfer-ownership')
60
+            ->setDescription('All files and folders are moved to another user - shares are moved as well.')
61
+            ->addArgument(
62
+                'source-user',
63
+                InputArgument::REQUIRED,
64
+                'owner of files which shall be moved'
65
+            )
66
+            ->addArgument(
67
+                'destination-user',
68
+                InputArgument::REQUIRED,
69
+                'user who will be the new owner of the files'
70
+            )
71
+            ->addOption(
72
+                'path',
73
+                null,
74
+                InputOption::VALUE_REQUIRED,
75
+                'selectively provide the path to transfer. For example --path="folder_name"',
76
+                ''
77
+            );
78
+    }
79 79
 
80
-	protected function execute(InputInterface $input, OutputInterface $output) {
81
-		$sourceUserObject = $this->userManager->get($input->getArgument('source-user'));
82
-		$destinationUserObject = $this->userManager->get($input->getArgument('destination-user'));
80
+    protected function execute(InputInterface $input, OutputInterface $output) {
81
+        $sourceUserObject = $this->userManager->get($input->getArgument('source-user'));
82
+        $destinationUserObject = $this->userManager->get($input->getArgument('destination-user'));
83 83
 
84
-		if (!$sourceUserObject instanceof IUser) {
85
-			$output->writeln("<error>Unknown source user " . $input->getArgument('source-user') . "</error>");
86
-			return 1;
87
-		}
84
+        if (!$sourceUserObject instanceof IUser) {
85
+            $output->writeln("<error>Unknown source user " . $input->getArgument('source-user') . "</error>");
86
+            return 1;
87
+        }
88 88
 
89
-		if (!$destinationUserObject instanceof IUser) {
90
-			$output->writeln("<error>Unknown destination user " . $input->getArgument('destination-user') . "</error>");
91
-			return 1;
92
-		}
89
+        if (!$destinationUserObject instanceof IUser) {
90
+            $output->writeln("<error>Unknown destination user " . $input->getArgument('destination-user') . "</error>");
91
+            return 1;
92
+        }
93 93
 
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;
104
-		}
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;
104
+        }
105 105
 
106
-		return 0;
107
-	}
106
+        return 0;
107
+    }
108 108
 
109 109
 }
Please login to merge, or discard this patch.
apps/files/lib/Service/OwnershipTransferService.php 1 patch
Indentation   +202 added lines, -202 removed lines patch added patch discarded remove patch
@@ -45,232 +45,232 @@
 block discarded – undo
45 45
 
46 46
 class OwnershipTransferService {
47 47
 
48
-	/** @var IEncryptionManager */
49
-	private $encryptionManager;
48
+    /** @var IEncryptionManager */
49
+    private $encryptionManager;
50 50
 
51
-	/** @var IShareManager */
52
-	private $shareManager;
51
+    /** @var IShareManager */
52
+    private $shareManager;
53 53
 
54
-	/** @var IMountManager */
55
-	private $mountManager;
54
+    /** @var IMountManager */
55
+    private $mountManager;
56 56
 
57
-	public function __construct(IEncryptionManager $manager,
58
-								IShareManager $shareManager,
59
-								IMountManager $mountManager) {
60
-		$this->encryptionManager = $manager;
61
-		$this->shareManager = $shareManager;
62
-		$this->mountManager = $mountManager;
63
-	}
57
+    public function __construct(IEncryptionManager $manager,
58
+                                IShareManager $shareManager,
59
+                                IMountManager $mountManager) {
60
+        $this->encryptionManager = $manager;
61
+        $this->shareManager = $shareManager;
62
+        $this->mountManager = $mountManager;
63
+    }
64 64
 
65
-	/**
66
-	 * @param IUser $sourceUser
67
-	 * @param IUser $destinationUser
68
-	 * @param string $path
69
-	 *
70
-	 * @throws TransferOwnershipException
71
-	 */
72
-	public function transfer(IUser $sourceUser,
73
-							 IUser $destinationUser,
74
-							 string $path,
75
-							 ?OutputInterface $output = null): void {
76
-		$output = $output ?? new NullOutput();
77
-		$sourceUid = $sourceUser->getUID();
78
-		$destinationUid = $destinationUser->getUID();
79
-		$sourcePath = rtrim($sourceUid . '/files/' . $path, '/');
65
+    /**
66
+     * @param IUser $sourceUser
67
+     * @param IUser $destinationUser
68
+     * @param string $path
69
+     *
70
+     * @throws TransferOwnershipException
71
+     */
72
+    public function transfer(IUser $sourceUser,
73
+                                IUser $destinationUser,
74
+                                string $path,
75
+                             ?OutputInterface $output = null): void {
76
+        $output = $output ?? new NullOutput();
77
+        $sourceUid = $sourceUser->getUID();
78
+        $destinationUid = $destinationUser->getUID();
79
+        $sourcePath = rtrim($sourceUid . '/files/' . $path, '/');
80 80
 
81
-		// target user has to be ready
82
-		if (!$this->encryptionManager->isReadyForUser($destinationUid)) {
83
-			throw new TransferOwnershipException("The target user is not ready to accept files. The user has at least to be logged in once.", 2);
84
-		}
81
+        // target user has to be ready
82
+        if (!$this->encryptionManager->isReadyForUser($destinationUid)) {
83
+            throw new TransferOwnershipException("The target user is not ready to accept files. The user has at least to be logged in once.", 2);
84
+        }
85 85
 
86
-		$date = date('Y-m-d H-i-s');
87
-		$finalTarget = "$destinationUid/files/transferred from $sourceUid on $date";
86
+        $date = date('Y-m-d H-i-s');
87
+        $finalTarget = "$destinationUid/files/transferred from $sourceUid on $date";
88 88
 
89
-		// setup filesystem
90
-		Filesystem::initMountPoints($sourceUid);
91
-		Filesystem::initMountPoints($destinationUid);
89
+        // setup filesystem
90
+        Filesystem::initMountPoints($sourceUid);
91
+        Filesystem::initMountPoints($destinationUid);
92 92
 
93
-		$view = new View();
94
-		if (!$view->is_dir($sourcePath)) {
95
-			throw new TransferOwnershipException("Unknown path provided: $path", 1);
96
-		}
93
+        $view = new View();
94
+        if (!$view->is_dir($sourcePath)) {
95
+            throw new TransferOwnershipException("Unknown path provided: $path", 1);
96
+        }
97 97
 
98
-		// analyse source folder
99
-		$this->analyse(
100
-			$sourceUid,
101
-			$destinationUid,
102
-			$sourcePath,
103
-			$view,
104
-			$output
105
-		);
98
+        // analyse source folder
99
+        $this->analyse(
100
+            $sourceUid,
101
+            $destinationUid,
102
+            $sourcePath,
103
+            $view,
104
+            $output
105
+        );
106 106
 
107
-		// collect all the shares
108
-		$shares = $this->collectUsersShares(
109
-			$sourceUid,
110
-			$output
111
-		);
107
+        // collect all the shares
108
+        $shares = $this->collectUsersShares(
109
+            $sourceUid,
110
+            $output
111
+        );
112 112
 
113
-		// transfer the files
114
-		$this->transferFiles(
115
-			$sourceUid,
116
-			$sourcePath,
117
-			$finalTarget,
118
-			$view,
119
-			$output
120
-		);
113
+        // transfer the files
114
+        $this->transferFiles(
115
+            $sourceUid,
116
+            $sourcePath,
117
+            $finalTarget,
118
+            $view,
119
+            $output
120
+        );
121 121
 
122
-		// restore the shares
123
-		$this->restoreShares(
124
-			$sourceUid,
125
-			$destinationUid,
126
-			$shares,
127
-			$output
128
-		);
129
-	}
122
+        // restore the shares
123
+        $this->restoreShares(
124
+            $sourceUid,
125
+            $destinationUid,
126
+            $shares,
127
+            $output
128
+        );
129
+    }
130 130
 
131
-	private function walkFiles(View $view, $path, Closure $callBack) {
132
-		foreach ($view->getDirectoryContent($path) as $fileInfo) {
133
-			if (!$callBack($fileInfo)) {
134
-				return;
135
-			}
136
-			if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) {
137
-				$this->walkFiles($view, $fileInfo->getPath(), $callBack);
138
-			}
139
-		}
140
-	}
131
+    private function walkFiles(View $view, $path, Closure $callBack) {
132
+        foreach ($view->getDirectoryContent($path) as $fileInfo) {
133
+            if (!$callBack($fileInfo)) {
134
+                return;
135
+            }
136
+            if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) {
137
+                $this->walkFiles($view, $fileInfo->getPath(), $callBack);
138
+            }
139
+        }
140
+    }
141 141
 
142
-	/**
143
-	 * @param OutputInterface $output
144
-	 *
145
-	 * @throws \Exception
146
-	 */
147
-	protected function analyse(string $sourceUid,
148
-							   string $destinationUid,
149
-							   string $sourcePath,
150
-							   View $view,
151
-							   OutputInterface $output): void {
152
-		$output->writeln('Validating quota');
153
-		$size = $view->getFileInfo($sourcePath, false)->getSize(false);
154
-		$freeSpace = $view->free_space($destinationUid . '/files/');
155
-		if ($size > $freeSpace) {
156
-			$output->writeln('<error>Target user does not have enough free space available</error>');
157
-			throw new \Exception('Execution terminated');
158
-		}
142
+    /**
143
+     * @param OutputInterface $output
144
+     *
145
+     * @throws \Exception
146
+     */
147
+    protected function analyse(string $sourceUid,
148
+                                string $destinationUid,
149
+                                string $sourcePath,
150
+                                View $view,
151
+                                OutputInterface $output): void {
152
+        $output->writeln('Validating quota');
153
+        $size = $view->getFileInfo($sourcePath, false)->getSize(false);
154
+        $freeSpace = $view->free_space($destinationUid . '/files/');
155
+        if ($size > $freeSpace) {
156
+            $output->writeln('<error>Target user does not have enough free space available</error>');
157
+            throw new \Exception('Execution terminated');
158
+        }
159 159
 
160
-		$output->writeln("Analysing files of $sourceUid ...");
161
-		$progress = new ProgressBar($output);
162
-		$progress->start();
160
+        $output->writeln("Analysing files of $sourceUid ...");
161
+        $progress = new ProgressBar($output);
162
+        $progress->start();
163 163
 
164
-		$encryptedFiles = [];
165
-		$this->walkFiles($view, $sourcePath,
166
-			function (FileInfo $fileInfo) use ($progress) {
167
-				if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) {
168
-					// only analyze into folders from main storage,
169
-					if (!$fileInfo->getStorage()->instanceOfStorage(IHomeStorage::class)) {
170
-						return false;
171
-					}
172
-					return true;
173
-				}
174
-				$progress->advance();
175
-				if ($fileInfo->isEncrypted()) {
176
-					$encryptedFiles[] = $fileInfo;
177
-				}
178
-				return true;
179
-			});
180
-		$progress->finish();
181
-		$output->writeln('');
164
+        $encryptedFiles = [];
165
+        $this->walkFiles($view, $sourcePath,
166
+            function (FileInfo $fileInfo) use ($progress) {
167
+                if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) {
168
+                    // only analyze into folders from main storage,
169
+                    if (!$fileInfo->getStorage()->instanceOfStorage(IHomeStorage::class)) {
170
+                        return false;
171
+                    }
172
+                    return true;
173
+                }
174
+                $progress->advance();
175
+                if ($fileInfo->isEncrypted()) {
176
+                    $encryptedFiles[] = $fileInfo;
177
+                }
178
+                return true;
179
+            });
180
+        $progress->finish();
181
+        $output->writeln('');
182 182
 
183
-		// no file is allowed to be encrypted
184
-		if (!empty($encryptedFiles)) {
185
-			$output->writeln("<error>Some files are encrypted - please decrypt them first</error>");
186
-			foreach ($encryptedFiles as $encryptedFile) {
187
-				/** @var FileInfo $encryptedFile */
188
-				$output->writeln("  " . $encryptedFile->getPath());
189
-			}
190
-			throw new \Exception('Execution terminated.');
191
-		}
192
-	}
183
+        // no file is allowed to be encrypted
184
+        if (!empty($encryptedFiles)) {
185
+            $output->writeln("<error>Some files are encrypted - please decrypt them first</error>");
186
+            foreach ($encryptedFiles as $encryptedFile) {
187
+                /** @var FileInfo $encryptedFile */
188
+                $output->writeln("  " . $encryptedFile->getPath());
189
+            }
190
+            throw new \Exception('Execution terminated.');
191
+        }
192
+    }
193 193
 
194
-	private function collectUsersShares(string $sourceUid,
195
-										OutputInterface $output): array {
196
-		$output->writeln("Collecting all share information for files and folder of $sourceUid ...");
194
+    private function collectUsersShares(string $sourceUid,
195
+                                        OutputInterface $output): array {
196
+        $output->writeln("Collecting all share information for files and folder of $sourceUid ...");
197 197
 
198
-		$shares = [];
199
-		$progress = new ProgressBar($output);
200
-		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) {
201
-			$offset = 0;
202
-			while (true) {
203
-				$sharePage = $this->shareManager->getSharesBy($sourceUid, $shareType, null, true, 50, $offset);
204
-				$progress->advance(count($sharePage));
205
-				if (empty($sharePage)) {
206
-					break;
207
-				}
208
-				$shares = array_merge($shares, $sharePage);
209
-				$offset += 50;
210
-			}
211
-		}
198
+        $shares = [];
199
+        $progress = new ProgressBar($output);
200
+        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) {
201
+            $offset = 0;
202
+            while (true) {
203
+                $sharePage = $this->shareManager->getSharesBy($sourceUid, $shareType, null, true, 50, $offset);
204
+                $progress->advance(count($sharePage));
205
+                if (empty($sharePage)) {
206
+                    break;
207
+                }
208
+                $shares = array_merge($shares, $sharePage);
209
+                $offset += 50;
210
+            }
211
+        }
212 212
 
213
-		$progress->finish();
214
-		$output->writeln('');
215
-		return $shares;
216
-	}
213
+        $progress->finish();
214
+        $output->writeln('');
215
+        return $shares;
216
+    }
217 217
 
218
-	protected function transferFiles(string $sourceUid,
219
-									 string $sourcePath,
220
-									 string $finalTarget,
221
-									 View $view,
222
-									 OutputInterface $output): void {
223
-		$output->writeln("Transferring files to $finalTarget ...");
218
+    protected function transferFiles(string $sourceUid,
219
+                                        string $sourcePath,
220
+                                        string $finalTarget,
221
+                                        View $view,
222
+                                        OutputInterface $output): void {
223
+        $output->writeln("Transferring files to $finalTarget ...");
224 224
 
225
-		// This change will help user to transfer the folder specified using --path option.
226
-		// Else only the content inside folder is transferred which is not correct.
227
-		if ($sourcePath !== "$sourceUid/files") {
228
-			$view->mkdir($finalTarget);
229
-			$finalTarget = $finalTarget . '/' . basename($sourcePath);
230
-		}
231
-		$view->rename($sourcePath, $finalTarget);
232
-		if (!is_dir("$sourceUid/files")) {
233
-			// because the files folder is moved away we need to recreate it
234
-			$view->mkdir("$sourceUid/files");
235
-		}
236
-	}
225
+        // This change will help user to transfer the folder specified using --path option.
226
+        // Else only the content inside folder is transferred which is not correct.
227
+        if ($sourcePath !== "$sourceUid/files") {
228
+            $view->mkdir($finalTarget);
229
+            $finalTarget = $finalTarget . '/' . basename($sourcePath);
230
+        }
231
+        $view->rename($sourcePath, $finalTarget);
232
+        if (!is_dir("$sourceUid/files")) {
233
+            // because the files folder is moved away we need to recreate it
234
+            $view->mkdir("$sourceUid/files");
235
+        }
236
+    }
237 237
 
238
-	private function restoreShares(string $sourceUid,
239
-								   string $destinationUid,
240
-								   array $shares,
241
-								   OutputInterface $output) {
242
-		$output->writeln("Restoring shares ...");
243
-		$progress = new ProgressBar($output, count($shares));
238
+    private function restoreShares(string $sourceUid,
239
+                                    string $destinationUid,
240
+                                    array $shares,
241
+                                    OutputInterface $output) {
242
+        $output->writeln("Restoring shares ...");
243
+        $progress = new ProgressBar($output, count($shares));
244 244
 
245
-		foreach ($shares as $share) {
246
-			try {
247
-				if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
248
-					$share->getSharedWith() === $destinationUid) {
249
-					// Unmount the shares before deleting, so we don't try to get the storage later on.
250
-					$shareMountPoint = $this->mountManager->find('/' . $destinationUid . '/files' . $share->getTarget());
251
-					if ($shareMountPoint) {
252
-						$this->mountManager->removeMount($shareMountPoint->getMountPoint());
253
-					}
254
-					$this->shareManager->deleteShare($share);
255
-				} else {
256
-					if ($share->getShareOwner() === $sourceUid) {
257
-						$share->setShareOwner($destinationUid);
258
-					}
259
-					if ($share->getSharedBy() === $sourceUid) {
260
-						$share->setSharedBy($destinationUid);
261
-					}
245
+        foreach ($shares as $share) {
246
+            try {
247
+                if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER &&
248
+                    $share->getSharedWith() === $destinationUid) {
249
+                    // Unmount the shares before deleting, so we don't try to get the storage later on.
250
+                    $shareMountPoint = $this->mountManager->find('/' . $destinationUid . '/files' . $share->getTarget());
251
+                    if ($shareMountPoint) {
252
+                        $this->mountManager->removeMount($shareMountPoint->getMountPoint());
253
+                    }
254
+                    $this->shareManager->deleteShare($share);
255
+                } else {
256
+                    if ($share->getShareOwner() === $sourceUid) {
257
+                        $share->setShareOwner($destinationUid);
258
+                    }
259
+                    if ($share->getSharedBy() === $sourceUid) {
260
+                        $share->setSharedBy($destinationUid);
261
+                    }
262 262
 
263
-					$this->shareManager->updateShare($share);
264
-				}
265
-			} catch (\OCP\Files\NotFoundException $e) {
266
-				$output->writeln('<error>Share with id ' . $share->getId() . ' points at deleted file, skipping</error>');
267
-			} catch (\Exception $e) {
268
-				$output->writeln('<error>Could not restore share with id ' . $share->getId() . ':' . $e->getTraceAsString() . '</error>');
269
-			}
270
-			$progress->advance();
271
-		}
272
-		$progress->finish();
273
-		$output->writeln('');
274
-	}
263
+                    $this->shareManager->updateShare($share);
264
+                }
265
+            } catch (\OCP\Files\NotFoundException $e) {
266
+                $output->writeln('<error>Share with id ' . $share->getId() . ' points at deleted file, skipping</error>');
267
+            } catch (\Exception $e) {
268
+                $output->writeln('<error>Could not restore share with id ' . $share->getId() . ':' . $e->getTraceAsString() . '</error>');
269
+            }
270
+            $progress->advance();
271
+        }
272
+        $progress->finish();
273
+        $output->writeln('');
274
+    }
275 275
 
276 276
 }
Please login to merge, or discard this patch.