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 ;
43+ use OCP \Files \NotFoundException ;
4244use OCP \IUser ;
4345use OCP \Share \IManager as IShareManager ;
4446use 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 ) {
0 commit comments