35
35
use OC \Files \View ;
36
36
use OCA \Files \Exception \TransferOwnershipException ;
37
37
use OCP \Encryption \IManager as IEncryptionManager ;
38
+ use OCP \Files \Config \IUserMountCache ;
38
39
use OCP \Files \FileInfo ;
39
40
use OCP \Files \IHomeStorage ;
40
41
use OCP \Files \InvalidPathException ;
41
42
use OCP \Files \Mount \IMountManager ;
42
43
use OCP \IUser ;
43
44
use OCP \Share \IManager as IShareManager ;
45
+ use OCP \Share \IShare ;
44
46
use Symfony \Component \Console \Helper \ProgressBar ;
45
47
use Symfony \Component \Console \Output \NullOutput ;
46
48
use Symfony \Component \Console \Output \OutputInterface ;
@@ -62,12 +64,17 @@ class OwnershipTransferService {
62
64
/** @var IMountManager */
63
65
private $ mountManager ;
64
66
67
+ /** @var IUserMountCache */
68
+ private $ userMountCache ;
69
+
65
70
public function __construct (IEncryptionManager $ manager ,
66
71
IShareManager $ shareManager ,
67
- IMountManager $ mountManager ) {
72
+ IMountManager $ mountManager ,
73
+ IUserMountCache $ userMountCache ) {
68
74
$ this ->encryptionManager = $ manager ;
69
75
$ this ->shareManager = $ shareManager ;
70
76
$ this ->mountManager = $ mountManager ;
77
+ $ this ->userMountCache = $ userMountCache ;
71
78
}
72
79
73
80
/**
@@ -148,7 +155,9 @@ public function transfer(IUser $sourceUser,
148
155
// collect all the shares
149
156
$ shares = $ this ->collectUsersShares (
150
157
$ sourceUid ,
151
- $ output
158
+ $ output ,
159
+ $ view ,
160
+ $ sourcePath
152
161
);
153
162
154
163
// transfer the files
@@ -233,7 +242,9 @@ function (FileInfo $fileInfo) use ($progress) {
233
242
}
234
243
235
244
private function collectUsersShares (string $ sourceUid ,
236
- OutputInterface $ output ): array {
245
+ OutputInterface $ output ,
246
+ View $ view ,
247
+ string $ path ): array {
237
248
$ output ->writeln ("Collecting all share information for files and folders of $ sourceUid ... " );
238
249
239
250
$ shares = [];
@@ -246,6 +257,23 @@ private function collectUsersShares(string $sourceUid,
246
257
if (empty ($ sharePage )) {
247
258
break ;
248
259
}
260
+ if ($ path !== "$ sourceUid/files " ) {
261
+ $ sharePage = array_filter ($ sharePage , function (IShare $ share ) use ($ view , $ path ) {
262
+ try {
263
+ $ relativePath = $ view ->getPath ($ share ->getNodeId ());
264
+ $ singleFileTranfer = $ view ->is_file ($ path );
265
+ if ($ singleFileTranfer ) {
266
+ return Filesystem::normalizePath ($ relativePath ) === Filesystem::normalizePath ($ path );
267
+ }
268
+
269
+ return mb_strpos (
270
+ Filesystem::normalizePath ($ relativePath . '/ ' , false ),
271
+ Filesystem::normalizePath ($ path . '/ ' , false )) === 0 ;
272
+ } catch (\Exception $ e ) {
273
+ return false ;
274
+ }
275
+ });
276
+ }
249
277
$ shares = array_merge ($ shares , $ sharePage );
250
278
$ offset += 50 ;
251
279
}
@@ -306,6 +334,12 @@ private function restoreShares(string $sourceUid,
306
334
$ share ->setSharedBy ($ destinationUid );
307
335
}
308
336
337
+
338
+ // trigger refetching of the node so that the new owner and mountpoint are taken into account
339
+ // otherwise the checks on the share update will fail due to the original node not being available in the new user scope
340
+ $ this ->userMountCache ->clear ();
341
+ $ share ->setNodeId ($ share ->getNode ()->getId ());
342
+
309
343
$ this ->shareManager ->updateShare ($ share );
310
344
}
311
345
} catch (\OCP \Files \NotFoundException $ e ) {
0 commit comments