1
0

TransferOwnership.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Files\BackgroundJob;
  8. use OCA\Files\AppInfo\Application;
  9. use OCA\Files\Db\TransferOwnership as Transfer;
  10. use OCA\Files\Db\TransferOwnershipMapper;
  11. use OCA\Files\Exception\TransferOwnershipException;
  12. use OCA\Files\Service\OwnershipTransferService;
  13. use OCP\AppFramework\Utility\ITimeFactory;
  14. use OCP\BackgroundJob\QueuedJob;
  15. use OCP\Files\IRootFolder;
  16. use OCP\IUser;
  17. use OCP\IUserManager;
  18. use OCP\Notification\IManager as NotificationManager;
  19. use Psr\Log\LoggerInterface;
  20. use function ltrim;
  21. class TransferOwnership extends QueuedJob {
  22. public function __construct(
  23. ITimeFactory $timeFactory,
  24. private IUserManager $userManager,
  25. private OwnershipTransferService $transferService,
  26. private LoggerInterface $logger,
  27. private NotificationManager $notificationManager,
  28. private TransferOwnershipMapper $mapper,
  29. private IRootFolder $rootFolder,
  30. ) {
  31. parent::__construct($timeFactory);
  32. }
  33. protected function run($argument) {
  34. $id = $argument['id'];
  35. $transfer = $this->mapper->getById($id);
  36. $sourceUser = $transfer->getSourceUser();
  37. $destinationUser = $transfer->getTargetUser();
  38. $fileId = $transfer->getFileId();
  39. $userFolder = $this->rootFolder->getUserFolder($sourceUser);
  40. $node = $userFolder->getFirstNodeById($fileId);
  41. if (!$node) {
  42. $this->logger->alert('Could not transfer ownership: Node not found');
  43. $this->failedNotication($transfer);
  44. return;
  45. }
  46. $path = $userFolder->getRelativePath($node->getPath());
  47. $sourceUserObject = $this->userManager->get($sourceUser);
  48. $destinationUserObject = $this->userManager->get($destinationUser);
  49. if (!$sourceUserObject instanceof IUser) {
  50. $this->logger->alert('Could not transfer ownership: Unknown source user ' . $sourceUser);
  51. $this->failedNotication($transfer);
  52. return;
  53. }
  54. if (!$destinationUserObject instanceof IUser) {
  55. $this->logger->alert("Unknown destination user $destinationUser");
  56. $this->failedNotication($transfer);
  57. return;
  58. }
  59. try {
  60. $this->transferService->transfer(
  61. $sourceUserObject,
  62. $destinationUserObject,
  63. ltrim($path, '/')
  64. );
  65. $this->successNotification($transfer);
  66. } catch (TransferOwnershipException $e) {
  67. $this->logger->error(
  68. $e->getMessage(),
  69. [
  70. 'exception' => $e,
  71. ],
  72. );
  73. $this->failedNotication($transfer);
  74. }
  75. $this->mapper->delete($transfer);
  76. }
  77. private function failedNotication(Transfer $transfer): void {
  78. // Send notification to source user
  79. $notification = $this->notificationManager->createNotification();
  80. $notification->setUser($transfer->getSourceUser())
  81. ->setApp(Application::APP_ID)
  82. ->setDateTime($this->time->getDateTime())
  83. ->setSubject('transferOwnershipFailedSource', [
  84. 'sourceUser' => $transfer->getSourceUser(),
  85. 'targetUser' => $transfer->getTargetUser(),
  86. 'nodeName' => $transfer->getNodeName(),
  87. ])
  88. ->setObject('transfer', (string)$transfer->getId());
  89. $this->notificationManager->notify($notification);
  90. // Send notification to source user
  91. $notification = $this->notificationManager->createNotification();
  92. $notification->setUser($transfer->getTargetUser())
  93. ->setApp(Application::APP_ID)
  94. ->setDateTime($this->time->getDateTime())
  95. ->setSubject('transferOwnershipFailedTarget', [
  96. 'sourceUser' => $transfer->getSourceUser(),
  97. 'targetUser' => $transfer->getTargetUser(),
  98. 'nodeName' => $transfer->getNodeName(),
  99. ])
  100. ->setObject('transfer', (string)$transfer->getId());
  101. $this->notificationManager->notify($notification);
  102. }
  103. private function successNotification(Transfer $transfer): void {
  104. // Send notification to source user
  105. $notification = $this->notificationManager->createNotification();
  106. $notification->setUser($transfer->getSourceUser())
  107. ->setApp(Application::APP_ID)
  108. ->setDateTime($this->time->getDateTime())
  109. ->setSubject('transferOwnershipDoneSource', [
  110. 'sourceUser' => $transfer->getSourceUser(),
  111. 'targetUser' => $transfer->getTargetUser(),
  112. 'nodeName' => $transfer->getNodeName(),
  113. ])
  114. ->setObject('transfer', (string)$transfer->getId());
  115. $this->notificationManager->notify($notification);
  116. // Send notification to source user
  117. $notification = $this->notificationManager->createNotification();
  118. $notification->setUser($transfer->getTargetUser())
  119. ->setApp(Application::APP_ID)
  120. ->setDateTime($this->time->getDateTime())
  121. ->setSubject('transferOwnershipDoneTarget', [
  122. 'sourceUser' => $transfer->getSourceUser(),
  123. 'targetUser' => $transfer->getTargetUser(),
  124. 'nodeName' => $transfer->getNodeName(),
  125. ])
  126. ->setObject('transfer', (string)$transfer->getId());
  127. $this->notificationManager->notify($notification);
  128. }
  129. }