3535use OC \Files \View ;
3636use OCA \Files \Exception \TransferOwnershipException ;
3737use OCP \Encryption \IManager as IEncryptionManager ;
38+ use OCP \Files \Config \IUserMountCache ;
3839use OCP \Files \FileInfo ;
3940use OCP \Files \IHomeStorage ;
4041use OCP \Files \InvalidPathException ;
4142use OCP \Files \Mount \IMountManager ;
4243use OCP \IUser ;
4344use OCP \Share \IManager as IShareManager ;
45+ use OCP \Share \IShare ;
4446use Symfony \Component \Console \Helper \ProgressBar ;
4547use Symfony \Component \Console \Output \NullOutput ;
4648use Symfony \Component \Console \Output \OutputInterface ;
@@ -62,12 +64,17 @@ class OwnershipTransferService {
6264 /** @var IMountManager */
6365 private $ mountManager ;
6466
67+ /** @var IUserMountCache */
68+ private $ userMountCache ;
69+
6570 public function __construct (IEncryptionManager $ manager ,
6671 IShareManager $ shareManager ,
67- IMountManager $ mountManager ) {
72+ IMountManager $ mountManager ,
73+ IUserMountCache $ userMountCache ) {
6874 $ this ->encryptionManager = $ manager ;
6975 $ this ->shareManager = $ shareManager ;
7076 $ this ->mountManager = $ mountManager ;
77+ $ this ->userMountCache = $ userMountCache ;
7178 }
7279
7380 /**
@@ -148,7 +155,9 @@ public function transfer(IUser $sourceUser,
148155 // collect all the shares
149156 $ shares = $ this ->collectUsersShares (
150157 $ sourceUid ,
151- $ output
158+ $ output ,
159+ $ view ,
160+ $ sourcePath
152161 );
153162
154163 // transfer the files
@@ -233,7 +242,9 @@ function (FileInfo $fileInfo) use ($progress) {
233242 }
234243
235244 private function collectUsersShares (string $ sourceUid ,
236- OutputInterface $ output ): array {
245+ OutputInterface $ output ,
246+ View $ view ,
247+ string $ path ): array {
237248 $ output ->writeln ("Collecting all share information for files and folders of $ sourceUid ... " );
238249
239250 $ shares = [];
@@ -246,6 +257,23 @@ private function collectUsersShares(string $sourceUid,
246257 if (empty ($ sharePage )) {
247258 break ;
248259 }
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+ }
249277 $ shares = array_merge ($ shares , $ sharePage );
250278 $ offset += 50 ;
251279 }
@@ -306,6 +334,12 @@ private function restoreShares(string $sourceUid,
306334 $ share ->setSharedBy ($ destinationUid );
307335 }
308336
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+
309343 $ this ->shareManager ->updateShare ($ share );
310344 }
311345 } catch (\OCP \Files \NotFoundException $ e ) {
0 commit comments