X Tutup
Skip to content

Commit 90a094a

Browse files
author
Vincent Petry
authored
Merge pull request #27343 from sharidas/occ-transfer-ownership-with-path
Providing --path option to transfer-ownership
2 parents 6be3671 + ed2be64 commit 90a094a

File tree

3 files changed

+173
-4
lines changed

3 files changed

+173
-4
lines changed

apps/files/lib/Command/TransferOwnership.php

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use OCP\Share\IManager;
3333
use OCP\Share\IShare;
3434
use Symfony\Component\Console\Command\Command;
35+
use Symfony\Component\Console\Input\InputOption;
3536
use Symfony\Component\Console\Helper\ProgressBar;
3637
use Symfony\Component\Console\Input\InputArgument;
3738
use Symfony\Component\Console\Input\InputInterface;
@@ -63,6 +64,9 @@ class TransferOwnership extends Command {
6364
/** @var string */
6465
private $destinationUser;
6566

67+
/** @var string */
68+
private $inputPath;
69+
6670
/** @var string */
6771
private $finalTarget;
6872

@@ -86,6 +90,12 @@ protected function configure() {
8690
'destination-user',
8791
InputArgument::REQUIRED,
8892
'user who will be the new owner of the files'
93+
)
94+
->addOption(
95+
'path',
96+
null,
97+
InputOption::VALUE_REQUIRED,
98+
'selectively provide the path to transfer. For example --path="folder_name"'
8999
);
90100
}
91101

@@ -103,6 +113,8 @@ protected function execute(InputInterface $input, OutputInterface $output) {
103113

104114
$this->sourceUser = $sourceUserObject->getUID();
105115
$this->destinationUser = $destinationUserObject->getUID();
116+
$this->inputPath = $input->getOption('path');
117+
$this->inputPath = ltrim($this->inputPath, '/');
106118

107119
// target user has to be ready
108120
if (!\OC::$server->getEncryptionManager()->isReadyForUser($this->destinationUser)) {
@@ -118,6 +130,16 @@ protected function execute(InputInterface $input, OutputInterface $output) {
118130
Filesystem::initMountPoints($this->sourceUser);
119131
Filesystem::initMountPoints($this->destinationUser);
120132

133+
if (strlen($this->inputPath) >= 1) {
134+
$view = new View();
135+
$unknownDir = $this->inputPath;
136+
$this->inputPath = $this->sourceUser . "/files/" . $this->inputPath;
137+
if (!$view->is_dir($this->inputPath)) {
138+
$output->writeln("<error>Unknown path provided: $unknownDir</error>");
139+
return 1;
140+
}
141+
}
142+
121143
// analyse source folder
122144
$this->analyse($output);
123145

@@ -152,7 +174,14 @@ protected function analyse(OutputInterface $output) {
152174
$progress = new ProgressBar($output);
153175
$progress->start();
154176
$self = $this;
155-
$this->walkFiles($view, "$this->sourceUser/files",
177+
$walkPath = "$this->sourceUser/files";
178+
if ( strlen($this->inputPath) > 0) {
179+
if ($this->inputPath !== "$this->sourceUser/files") {
180+
$walkPath = $this->inputPath;
181+
}
182+
}
183+
184+
$this->walkFiles($view, $walkPath,
156185
function (FileInfo $fileInfo) use ($progress, $self) {
157186
if ($fileInfo->getType() === FileInfo::TYPE_FOLDER) {
158187
// only analyze into folders from main storage,
@@ -214,9 +243,20 @@ private function collectUsersShares(OutputInterface $output) {
214243
protected function transfer(OutputInterface $output) {
215244
$view = new View();
216245
$output->writeln("Transferring files to $this->finalTarget ...");
217-
$view->rename("$this->sourceUser/files", $this->finalTarget);
218-
// because the files folder is moved away we need to recreate it
219-
$view->mkdir("$this->sourceUser/files");
246+
$sourcePath = (strlen($this->inputPath) > 0) ? $this->inputPath : "$this->sourceUser/files";
247+
// This change will help user to transfer the folder specified using --path option.
248+
// Else only the content inside folder is transferred which is not correct.
249+
if (strlen($this->inputPath) > 0) {
250+
if($this->inputPath !== ltrim("$this->sourceUser/files", '/')) {
251+
$view->mkdir($this->finalTarget);
252+
$this->finalTarget = $this->finalTarget . '/' . basename($sourcePath);
253+
}
254+
}
255+
$view->rename($sourcePath, $this->finalTarget);
256+
if (!is_dir("$this->sourceUser/files")) {
257+
// because the files folder is moved away we need to recreate it
258+
$view->mkdir("$this->sourceUser/files");
259+
}
220260
}
221261

222262
/**

tests/integration/features/bootstrap/CommandLineContext.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@ public function transferingOwnership($user1, $user2) {
8888
}
8989
}
9090

91+
/**
92+
* @When /^transfering ownership of path "([^"]+)" from "([^"]+)" to "([^"]+)"/
93+
*/
94+
public function transferingOwnershipPath($path, $user1, $user2) {
95+
$path = '--path=' . $path;
96+
if($this->runOcc(['files:transfer-ownership', $path, $user1, $user2]) === 0) {
97+
$this->lastTransferPath = $this->findLastTransferFolderForUser($user1, $user2);
98+
} else {
99+
// failure
100+
$this->lastTransferPath = null;
101+
}
102+
}
103+
104+
91105
/**
92106
* @When /^using received transfer folder of "([^"]+)" as dav path$/
93107
*/

tests/integration/features/transfer-ownership.feature

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,118 @@ Feature: transfer-ownership
126126
Then the command error output contains the text "Unknown target user"
127127
And the command failed with exit code 1
128128

129+
@no_encryption
130+
Scenario: transfering ownership of a folder
131+
Given user "user0" exists
132+
And user "user1" exists
133+
And User "user0" created a folder "/test"
134+
And User "user0" uploads file "data/textfile.txt" to "/test/somefile.txt"
135+
When transfering ownership of path "test" from "user0" to "user1"
136+
And the command was successful
137+
And As an "user1"
138+
And using received transfer folder of "user1" as dav path
139+
Then Downloaded content when downloading file "/test/somefile.txt" with range "bytes=0-6" should be "This is"
140+
141+
@no_encryption
142+
Scenario: transfering ownership of file shares
143+
Given user "user0" exists
144+
And user "user1" exists
145+
And user "user2" exists
146+
And User "user0" created a folder "/test"
147+
And User "user0" uploads file "data/textfile.txt" to "/test/somefile.txt"
148+
And file "/test/somefile.txt" of user "user0" is shared with user "user2" with permissions 19
149+
When transfering ownership of path "test" from "user0" to "user1"
150+
And the command was successful
151+
And As an "user2"
152+
Then Downloaded content when downloading file "/somefile.txt" with range "bytes=0-6" should be "This is"
153+
154+
@no_encryption
155+
Scenario: transfering ownership of folder shared with third user
156+
Given user "user0" exists
157+
And user "user1" exists
158+
And user "user2" exists
159+
And User "user0" created a folder "/test"
160+
And User "user0" uploads file "data/textfile.txt" to "/test/somefile.txt"
161+
And folder "/test" of user "user0" is shared with user "user2" with permissions 31
162+
When transfering ownership of path "test" from "user0" to "user1"
163+
And the command was successful
164+
And As an "user2"
165+
Then Downloaded content when downloading file "/test/somefile.txt" with range "bytes=0-6" should be "This is"
166+
167+
@no_encryption
168+
Scenario: transfering ownership of folder shared with transfer recipient
169+
Given user "user0" exists
170+
And user "user1" exists
171+
And User "user0" created a folder "/test"
172+
And User "user0" uploads file "data/textfile.txt" to "/test/somefile.txt"
173+
And folder "/test" of user "user0" is shared with user "user1" with permissions 31
174+
When transfering ownership of path "test" from "user0" to "user1"
175+
And the command was successful
176+
And As an "user1"
177+
Then as "user1" the folder "/test" does not exist
178+
And using received transfer folder of "user1" as dav path
179+
And Downloaded content when downloading file "/test/somefile.txt" with range "bytes=0-6" should be "This is"
180+
181+
@no_encryption
182+
Scenario: transfering ownership of folder doubly shared with third user
183+
Given group "group1" exists
184+
And user "user0" exists
185+
And user "user1" exists
186+
And user "user2" exists
187+
And user "user2" belongs to group "group1"
188+
And User "user0" created a folder "/test"
189+
And User "user0" uploads file "data/textfile.txt" to "/test/somefile.txt"
190+
And folder "/test" of user "user0" is shared with group "group1" with permissions 31
191+
And folder "/test" of user "user0" is shared with user "user2" with permissions 31
192+
When transfering ownership of path "test" from "user0" to "user1"
193+
And the command was successful
194+
And As an "user2"
195+
Then Downloaded content when downloading file "/test/somefile.txt" with range "bytes=0-6" should be "This is"
196+
197+
@no_encryption
198+
Scenario: transfering ownership does not transfer received shares
199+
Given user "user0" exists
200+
And user "user1" exists
201+
And user "user2" exists
202+
And User "user2" created a folder "/test"
203+
And User "user0" created a folder "/sub"
204+
And folder "/test" of user "user2" is shared with user "user0" with permissions 31
205+
And User "user0" moved folder "/test" to "/sub/test"
206+
When transfering ownership of path "sub" from "user0" to "user1"
207+
And the command was successful
208+
And As an "user1"
209+
And using received transfer folder of "user1" as dav path
210+
Then as "user1" the folder "/sub/test" does not exist
211+
212+
@no_encryption
213+
@local_storage
214+
Scenario: transfering ownership does not transfer external storage
215+
Given user "user0" exists
216+
And user "user1" exists
217+
And User "user0" created a folder "/sub"
218+
When transfering ownership of path "sub" from "user0" to "user1"
219+
And the command was successful
220+
And As an "user1"
221+
And using received transfer folder of "user1" as dav path
222+
Then as "user1" the folder "/local_storage" does not exist
223+
224+
Scenario: transfering ownership fails with invalid source user
225+
Given user "user0" exists
226+
And User "user0" created a folder "/sub"
227+
When transfering ownership of path "sub" from "invalid_user" to "user0"
228+
Then the command error output contains the text "Unknown source user"
229+
And the command failed with exit code 1
230+
231+
Scenario: transfering ownership fails with invalid target user
232+
Given user "user0" exists
233+
And User "user0" created a folder "/sub"
234+
When transfering ownership of path "sub" from "user0" to "invalid_user"
235+
Then the command error output contains the text "Unknown target user"
236+
And the command failed with exit code 1
237+
238+
Scenario: transfering ownership fails with invalid path
239+
Given user "user0" exists
240+
And user "user1" exists
241+
When transfering ownership of path "test" from "user0" to "user1"
242+
Then the command error output contains the text "Unknown target user"
243+
And the command failed with exit code 1

0 commit comments

Comments
 (0)
X Tutup