1
0

TransferOwnership.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Files\BackgroundJob;
  25. use OCA\Files\AppInfo\Application;
  26. use OCA\Files\Db\TransferOwnership as Transfer;
  27. use OCA\Files\Db\TransferOwnershipMapper;
  28. use OCA\Files\Exception\TransferOwnershipException;
  29. use OCA\Files\Service\OwnershipTransferService;
  30. use OCP\AppFramework\Utility\ITimeFactory;
  31. use OCP\BackgroundJob\QueuedJob;
  32. use OCP\Files\IRootFolder;
  33. use OCP\IUser;
  34. use OCP\IUserManager;
  35. use OCP\Notification\IManager as NotificationManager;
  36. use Psr\Log\LoggerInterface;
  37. use function ltrim;
  38. class TransferOwnership extends QueuedJob {
  39. public function __construct(
  40. ITimeFactory $timeFactory,
  41. private IUserManager $userManager,
  42. private OwnershipTransferService $transferService,
  43. private LoggerInterface $logger,
  44. private NotificationManager $notificationManager,
  45. private TransferOwnershipMapper $mapper,
  46. private IRootFolder $rootFolder,
  47. ) {
  48. parent::__construct($timeFactory);
  49. }
  50. protected function run($argument) {
  51. $id = $argument['id'];
  52. $transfer = $this->mapper->getById($id);
  53. $sourceUser = $transfer->getSourceUser();
  54. $destinationUser = $transfer->getTargetUser();
  55. $fileId = $transfer->getFileId();
  56. $userFolder = $this->rootFolder->getUserFolder($sourceUser);
  57. $nodes = $userFolder->getById($fileId);
  58. if (empty($nodes)) {
  59. $this->logger->alert('Could not transfer ownership: Node not found');
  60. $this->failedNotication($transfer);
  61. return;
  62. }
  63. $path = $userFolder->getRelativePath($nodes[0]->getPath());
  64. $sourceUserObject = $this->userManager->get($sourceUser);
  65. $destinationUserObject = $this->userManager->get($destinationUser);
  66. if (!$sourceUserObject instanceof IUser) {
  67. $this->logger->alert('Could not transfer ownership: Unknown source user ' . $sourceUser);
  68. $this->failedNotication($transfer);
  69. return;
  70. }
  71. if (!$destinationUserObject instanceof IUser) {
  72. $this->logger->alert("Unknown destination user $destinationUser");
  73. $this->failedNotication($transfer);
  74. return;
  75. }
  76. try {
  77. $this->transferService->transfer(
  78. $sourceUserObject,
  79. $destinationUserObject,
  80. ltrim($path, '/')
  81. );
  82. $this->successNotification($transfer);
  83. } catch (TransferOwnershipException $e) {
  84. $this->logger->error(
  85. $e->getMessage(),
  86. [
  87. 'exception' => $e,
  88. ],
  89. );
  90. $this->failedNotication($transfer);
  91. }
  92. $this->mapper->delete($transfer);
  93. }
  94. private function failedNotication(Transfer $transfer): void {
  95. // Send notification to source user
  96. $notification = $this->notificationManager->createNotification();
  97. $notification->setUser($transfer->getSourceUser())
  98. ->setApp(Application::APP_ID)
  99. ->setDateTime($this->time->getDateTime())
  100. ->setSubject('transferOwnershipFailedSource', [
  101. 'sourceUser' => $transfer->getSourceUser(),
  102. 'targetUser' => $transfer->getTargetUser(),
  103. 'nodeName' => $transfer->getNodeName(),
  104. ])
  105. ->setObject('transfer', (string)$transfer->getId());
  106. $this->notificationManager->notify($notification);
  107. // Send notification to source user
  108. $notification = $this->notificationManager->createNotification();
  109. $notification->setUser($transfer->getTargetUser())
  110. ->setApp(Application::APP_ID)
  111. ->setDateTime($this->time->getDateTime())
  112. ->setSubject('transferOwnershipFailedTarget', [
  113. 'sourceUser' => $transfer->getSourceUser(),
  114. 'targetUser' => $transfer->getTargetUser(),
  115. 'nodeName' => $transfer->getNodeName(),
  116. ])
  117. ->setObject('transfer', (string)$transfer->getId());
  118. $this->notificationManager->notify($notification);
  119. }
  120. private function successNotification(Transfer $transfer): void {
  121. // Send notification to source user
  122. $notification = $this->notificationManager->createNotification();
  123. $notification->setUser($transfer->getSourceUser())
  124. ->setApp(Application::APP_ID)
  125. ->setDateTime($this->time->getDateTime())
  126. ->setSubject('transferOwnershipDoneSource', [
  127. 'sourceUser' => $transfer->getSourceUser(),
  128. 'targetUser' => $transfer->getTargetUser(),
  129. 'nodeName' => $transfer->getNodeName(),
  130. ])
  131. ->setObject('transfer', (string)$transfer->getId());
  132. $this->notificationManager->notify($notification);
  133. // Send notification to source user
  134. $notification = $this->notificationManager->createNotification();
  135. $notification->setUser($transfer->getTargetUser())
  136. ->setApp(Application::APP_ID)
  137. ->setDateTime($this->time->getDateTime())
  138. ->setSubject('transferOwnershipDoneTarget', [
  139. 'sourceUser' => $transfer->getSourceUser(),
  140. 'targetUser' => $transfer->getTargetUser(),
  141. 'nodeName' => $transfer->getNodeName(),
  142. ])
  143. ->setObject('transfer', (string)$transfer->getId());
  144. $this->notificationManager->notify($notification);
  145. }
  146. }