3737use OC \Files \View ;
3838use OCA \Files \Exception \TransferOwnershipException ;
3939use OCP \Encryption \IManager as IEncryptionManager ;
40+ use OCP \Files \Config \IUserMountCache ;
4041use OCP \Files \FileInfo ;
4142use OCP \Files \IHomeStorage ;
4243use OCP \Files \InvalidPathException ;
@@ -65,12 +66,17 @@ class OwnershipTransferService {
6566 /** @var IMountManager */
6667 private $ mountManager ;
6768
69+ /** @var IUserMountCache */
70+ private $ userMountCache ;
71+
6872 public function __construct (IEncryptionManager $ manager ,
6973 IShareManager $ shareManager ,
70- IMountManager $ mountManager ) {
74+ IMountManager $ mountManager ,
75+ IUserMountCache $ userMountCache ) {
7176 $ this ->encryptionManager = $ manager ;
7277 $ this ->shareManager = $ shareManager ;
7378 $ this ->mountManager = $ mountManager ;
79+ $ this ->userMountCache = $ userMountCache ;
7480 }
7581
7682 /**
@@ -151,7 +157,9 @@ public function transfer(IUser $sourceUser,
151157 // collect all the shares
152158 $ shares = $ this ->collectUsersShares (
153159 $ sourceUid ,
154- $ output
160+ $ output ,
161+ $ view ,
162+ $ sourcePath
155163 );
156164
157165 // transfer the files
@@ -236,7 +244,9 @@ function (FileInfo $fileInfo) use ($progress) {
236244 }
237245
238246 private function collectUsersShares (string $ sourceUid ,
239- OutputInterface $ output ): array {
247+ OutputInterface $ output ,
248+ View $ view ,
249+ ?string $ path = null ): array {
240250 $ output ->writeln ("Collecting all share information for files and folders of $ sourceUid ... " );
241251
242252 $ shares = [];
@@ -249,6 +259,23 @@ private function collectUsersShares(string $sourceUid,
249259 if (empty ($ sharePage )) {
250260 break ;
251261 }
262+ if ($ path !== null ) {
263+ $ sharePage = array_filter ($ sharePage , function (IShare $ share ) use ($ view , $ path ) {
264+ try {
265+ $ relativePath = $ view ->getPath ($ share ->getNodeId ());
266+ $ singleFileTranfer = $ view ->is_file ($ path );
267+ if ($ singleFileTranfer ) {
268+ return Filesystem::normalizePath ($ relativePath ) === Filesystem::normalizePath ($ path );
269+ }
270+
271+ return mb_strpos (
272+ Filesystem::normalizePath ($ relativePath . '/ ' , false ),
273+ Filesystem::normalizePath ($ path . '/ ' , false )) === 0 ;
274+ } catch (\Exception $ e ) {
275+ return false ;
276+ }
277+ });
278+ }
252279 $ shares = array_merge ($ shares , $ sharePage );
253280 $ offset += 50 ;
254281 }
@@ -309,6 +336,12 @@ private function restoreShares(string $sourceUid,
309336 $ share ->setSharedBy ($ destinationUid );
310337 }
311338
339+
340+ // trigger refetching of the node so that the new owner and mountpoint are taken into account
341+ // otherwise the checks on the share update will fail due to the original node not being available in the new user scope
342+ $ this ->userMountCache ->clear ();
343+ $ share ->setNodeId ($ share ->getNode ()->getId ());
344+
312345 $ this ->shareManager ->updateShare ($ share );
313346 }
314347 } catch (\OCP \Files \NotFoundException $ e ) {
0 commit comments