4343use OCP \Files \InvalidPathException ;
4444use OCP \Files \Mount \IMountManager ;
4545use OCP \IUser ;
46+ use OCP \IUserManager ;
4647use OCP \Share \IManager as IShareManager ;
4748use OCP \Share \IShare ;
4849use Symfony \Component \Console \Helper \ProgressBar ;
@@ -69,14 +70,19 @@ class OwnershipTransferService {
6970 /** @var IUserMountCache */
7071 private $ userMountCache ;
7172
73+ /** @var IUserManager */
74+ private $ userManager ;
75+
7276 public function __construct (IEncryptionManager $ manager ,
7377 IShareManager $ shareManager ,
7478 IMountManager $ mountManager ,
75- IUserMountCache $ userMountCache ) {
79+ IUserMountCache $ userMountCache ,
80+ IUserManager $ userManager ) {
7681 $ this ->encryptionManager = $ manager ;
7782 $ this ->shareManager = $ shareManager ;
7883 $ this ->mountManager = $ mountManager ;
7984 $ this ->userMountCache = $ userMountCache ;
85+ $ this ->userManager = $ userManager ;
8086 }
8187
8288 /**
@@ -401,13 +407,20 @@ private function restoreShares(string $sourceUid,
401407 $ share ->setSharedBy ($ destinationUid );
402408 }
403409
410+ if ($ share ->getShareType () === IShare::TYPE_USER &&
411+ !$ this ->userManager ->userExists ($ share ->getSharedWith ())) {
412+ // stray share with deleted user
413+ $ output ->writeln ('<error>Share with id ' . $ share ->getId () . ' points at deleted user " ' . $ share ->getSharedWith () . '", deleting</error> ' );
414+ $ this ->shareManager ->deleteShare ($ share );
415+ continue ;
416+ } else {
417+ // trigger refetching of the node so that the new owner and mountpoint are taken into account
418+ // otherwise the checks on the share update will fail due to the original node not being available in the new user scope
419+ $ this ->userMountCache ->clear ();
420+ $ share ->setNodeId ($ share ->getNode ()->getId ());
404421
405- // trigger refetching of the node so that the new owner and mountpoint are taken into account
406- // otherwise the checks on the share update will fail due to the original node not being available in the new user scope
407- $ this ->userMountCache ->clear ();
408- $ share ->setNodeId ($ share ->getNode ()->getId ());
409-
410- $ this ->shareManager ->updateShare ($ share );
422+ $ this ->shareManager ->updateShare ($ share );
423+ }
411424 }
412425 } catch (\OCP \Files \NotFoundException $ e ) {
413426 $ output ->writeln ('<error>Share with id ' . $ share ->getId () . ' points at deleted file, skipping</error> ' );
0 commit comments