Skip to content

Commit

Permalink
Merge pull request #22804 from nextcloud/backport/stable19/22116-2264…
Browse files Browse the repository at this point in the history
…8-22761

[stable19] Fix share transfer of single files and on the transfered node
  • Loading branch information
rullzer authored Sep 28, 2020
2 parents ed22e1b + 083d1da commit 164096a
Show file tree
Hide file tree
Showing 6 changed files with 371 additions and 5 deletions.
40 changes: 37 additions & 3 deletions apps/files/lib/Service/OwnershipTransferService.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@
use OC\Files\View;
use OCA\Files\Exception\TransferOwnershipException;
use OCP\Encryption\IManager as IEncryptionManager;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\FileInfo;
use OCP\Files\IHomeStorage;
use OCP\Files\InvalidPathException;
use OCP\Files\Mount\IMountManager;
use OCP\IUser;
use OCP\Share\IManager as IShareManager;
use OCP\Share\IShare;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -62,12 +64,17 @@ class OwnershipTransferService {
/** @var IMountManager */
private $mountManager;

/** @var IUserMountCache */
private $userMountCache;

public function __construct(IEncryptionManager $manager,
IShareManager $shareManager,
IMountManager $mountManager) {
IMountManager $mountManager,
IUserMountCache $userMountCache) {
$this->encryptionManager = $manager;
$this->shareManager = $shareManager;
$this->mountManager = $mountManager;
$this->userMountCache = $userMountCache;
}

/**
Expand Down Expand Up @@ -148,7 +155,9 @@ public function transfer(IUser $sourceUser,
// collect all the shares
$shares = $this->collectUsersShares(
$sourceUid,
$output
$output,
$view,
$sourcePath
);

// transfer the files
Expand Down Expand Up @@ -233,7 +242,9 @@ function (FileInfo $fileInfo) use ($progress) {
}

private function collectUsersShares(string $sourceUid,
OutputInterface $output): array {
OutputInterface $output,
View $view,
string $path): array {
$output->writeln("Collecting all share information for files and folders of $sourceUid ...");

$shares = [];
Expand All @@ -246,6 +257,23 @@ private function collectUsersShares(string $sourceUid,
if (empty($sharePage)) {
break;
}
if ($path !== "$sourceUid/files") {
$sharePage = array_filter($sharePage, function (IShare $share) use ($view, $path) {
try {
$relativePath = $view->getPath($share->getNodeId());
$singleFileTranfer = $view->is_file($path);
if ($singleFileTranfer) {
return Filesystem::normalizePath($relativePath) === Filesystem::normalizePath($path);
}

return mb_strpos(
Filesystem::normalizePath($relativePath . '/', false),
Filesystem::normalizePath($path . '/', false)) === 0;
} catch (\Exception $e) {
return false;
}
});
}
$shares = array_merge($shares, $sharePage);
$offset += 50;
}
Expand Down Expand Up @@ -306,6 +334,12 @@ private function restoreShares(string $sourceUid,
$share->setSharedBy($destinationUid);
}


// trigger refetching of the node so that the new owner and mountpoint are taken into account
// otherwise the checks on the share update will fail due to the original node not being available in the new user scope
$this->userMountCache->clear();
$share->setNodeId($share->getNode()->getId());

$this->shareManager->updateShare($share);
}
} catch (\OCP\Files\NotFoundException $e) {
Expand Down
8 changes: 8 additions & 0 deletions build/integration/features/bootstrap/CommandLineContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
require __DIR__ . '/../../vendor/autoload.php';

use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use PHPUnit\Framework\Assert;

class CommandLineContext implements \Behat\Behat\Context\Context {
use CommandLine;
Expand Down Expand Up @@ -128,4 +129,11 @@ public function usingTransferFolderAsDavPath($user) {
$davPath = rtrim($davPath, '/') . $this->lastTransferPath;
$this->featureContext->usingDavPath($davPath);
}

/**
* @Then /^transfer folder name contains "([^"]+)"$/
*/
public function transferFolderNameContains($text) {
Assert::assertContains($text, $this->lastTransferPath);
}
}
2 changes: 1 addition & 1 deletion build/integration/features/bootstrap/Provisioning.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function assureUserExists($user) {
}

/**
* @Given /^user "([^"]*)" with displayname "([^"]*)" exists$/
* @Given /^user "([^"]*)" with displayname "((?:[^"]|\\")*)" exists$/
* @param string $user
*/
public function assureUserWithDisplaynameExists($user, $displayname) {
Expand Down
Loading

0 comments on commit 164096a

Please sign in to comment.