Skip to content

Commit 1eb8ddc

Browse files
committed
Transfer shares of the transferred root node
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent a4d511d commit 1eb8ddc

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

apps/files/lib/Service/OwnershipTransferService.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@
3535
use OC\Files\View;
3636
use OCA\Files\Exception\TransferOwnershipException;
3737
use OCP\Encryption\IManager as IEncryptionManager;
38+
use OCP\Files\Config\IUserMountCache;
3839
use OCP\Files\FileInfo;
3940
use OCP\Files\IHomeStorage;
4041
use OCP\Files\InvalidPathException;
4142
use OCP\Files\Mount\IMountManager;
43+
use OCP\Files\NotFoundException;
4244
use OCP\IUser;
4345
use OCP\Share\IManager as IShareManager;
4446
use OCP\Share\IShare;
@@ -63,12 +65,17 @@ class OwnershipTransferService {
6365
/** @var IMountManager */
6466
private $mountManager;
6567

68+
/** @var IUserMountCache */
69+
private $userMountCache;
70+
6671
public function __construct(IEncryptionManager $manager,
6772
IShareManager $shareManager,
68-
IMountManager $mountManager) {
73+
IMountManager $mountManager,
74+
IUserMountCache $userMountCache) {
6975
$this->encryptionManager = $manager;
7076
$this->shareManager = $shareManager;
7177
$this->mountManager = $mountManager;
78+
$this->userMountCache = $userMountCache;
7279
}
7380

7481
/**
@@ -149,6 +156,7 @@ public function transfer(IUser $sourceUser,
149156
// collect all the shares
150157
$shares = $this->collectUsersShares(
151158
$sourceUid,
159+
$sourcePath,
152160
$output
153161
);
154162

@@ -161,6 +169,8 @@ public function transfer(IUser $sourceUser,
161169
$output
162170
);
163171

172+
$view = new View();
173+
164174
// restore the shares
165175
$this->restoreShares(
166176
$sourceUid,
@@ -234,6 +244,7 @@ function (FileInfo $fileInfo) use ($progress) {
234244
}
235245

236246
private function collectUsersShares(string $sourceUid,
247+
string $path = null,
237248
OutputInterface $output): array {
238249
$output->writeln("Collecting all share information for files and folders of $sourceUid ...");
239250

@@ -247,6 +258,16 @@ private function collectUsersShares(string $sourceUid,
247258
if (empty($sharePage)) {
248259
break;
249260
}
261+
if ($path) {
262+
$sharePage = array_filter($sharePage, function (IShare $share) use ($path) {
263+
try {
264+
$node = $share->getNode();
265+
return \OC\Files\Filesystem::normalizePath($path) === $node->getPath();
266+
} catch (\Exception $e) {
267+
return false;
268+
}
269+
});
270+
}
250271
$shares = array_merge($shares, $sharePage);
251272
$offset += 50;
252273
}
@@ -307,6 +328,12 @@ private function restoreShares(string $sourceUid,
307328
$share->setSharedBy($destinationUid);
308329
}
309330

331+
332+
// trigger refetching of the node so that the new owner and mountpoint are taken into account
333+
// otherwise the checks on the share update will fail due to the original node not being available in the new user scope
334+
$this->userMountCache->clear();
335+
$share->setNodeId($share->getNode()->getId());
336+
310337
$this->shareManager->updateShare($share);
311338
}
312339
} catch (\OCP\Files\NotFoundException $e) {

lib/private/Files/Config/UserMountCache.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,4 +409,9 @@ public function getUsedSpaceForUsers(array $users) {
409409
$result->closeCursor();
410410
return $results;
411411
}
412+
413+
public function clear() {
414+
$this->cacheInfoCache = new CappedMemoryCache();
415+
$this->mountsForUsers = new CappedMemoryCache();
416+
}
412417
}

0 commit comments

Comments
 (0)