TransferOwnership.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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\ILogger;
  34. use OCP\IUser;
  35. use OCP\IUserManager;
  36. use OCP\Notification\IManager as NotificationManager;
  37. use function ltrim;
  38. class TransferOwnership extends QueuedJob {
  39. /** @var IUserManager $userManager */
  40. private $userManager;
  41. /** @var OwnershipTransferService */
  42. private $transferService;
  43. /** @var ILogger */
  44. private $logger;
  45. /** @var NotificationManager */
  46. private $notificationManager;
  47. /** @var TransferOwnershipMapper */
  48. private $mapper;
  49. /** @var IRootFolder */
  50. private $rootFolder;
  51. public function __construct(ITimeFactory $timeFactory,
  52. IUserManager $userManager,
  53. OwnershipTransferService $transferService,
  54. ILogger $logger,
  55. NotificationManager $notificationManager,
  56. TransferOwnershipMapper $mapper,
  57. IRootFolder $rootFolder) {
  58. parent::__construct($timeFactory);
  59. $this->userManager = $userManager;
  60. $this->transferService = $transferService;
  61. $this->logger = $logger;
  62. $this->notificationManager = $notificationManager;
  63. $this->mapper = $mapper;
  64. $this->rootFolder = $rootFolder;
  65. }
  66. protected function run($argument) {
  67. $id = $argument['id'];
  68. $transfer = $this->mapper->getById($id);
  69. $sourceUser = $transfer->getSourceUser();
  70. $destinationUser = $transfer->getTargetUser();
  71. $fileId = $transfer->getFileId();
  72. $userFolder = $this->rootFolder->getUserFolder($sourceUser);
  73. $nodes = $userFolder->getById($fileId);
  74. if (empty($nodes)) {
  75. $this->logger->alert('Could not transfer ownership: Node not found');
  76. $this->failedNotication($transfer);
  77. return;
  78. }
  79. $path = $userFolder->getRelativePath($nodes[0]->getPath());
  80. $sourceUserObject = $this->userManager->get($sourceUser);
  81. $destinationUserObject = $this->userManager->get($destinationUser);
  82. if (!$sourceUserObject instanceof IUser) {
  83. $this->logger->alert('Could not transfer ownership: Unknown source user ' . $sourceUser);
  84. $this->failedNotication($transfer);
  85. return;
  86. }
  87. if (!$destinationUserObject instanceof IUser) {
  88. $this->logger->alert("Unknown destination user $destinationUser");
  89. $this->failedNotication($transfer);
  90. return;
  91. }
  92. try {
  93. $this->transferService->transfer(
  94. $sourceUserObject,
  95. $destinationUserObject,
  96. ltrim($path, '/')
  97. );
  98. $this->successNotification($transfer);
  99. } catch (TransferOwnershipException $e) {
  100. $this->logger->logException($e);
  101. $this->failedNotication($transfer);
  102. }
  103. $this->mapper->delete($transfer);
  104. }
  105. private function failedNotication(Transfer $transfer): void {
  106. // Send notification to source user
  107. $notification = $this->notificationManager->createNotification();
  108. $notification->setUser($transfer->getSourceUser())
  109. ->setApp(Application::APP_ID)
  110. ->setDateTime($this->time->getDateTime())
  111. ->setSubject('transferOwnershipFailedSource', [
  112. 'sourceUser' => $transfer->getSourceUser(),
  113. 'targetUser' => $transfer->getTargetUser(),
  114. 'nodeName' => $transfer->getNodeName(),
  115. ])
  116. ->setObject('transfer', (string)$transfer->getId());
  117. $this->notificationManager->notify($notification);
  118. // Send notification to source user
  119. $notification = $this->notificationManager->createNotification();
  120. $notification->setUser($transfer->getTargetUser())
  121. ->setApp(Application::APP_ID)
  122. ->setDateTime($this->time->getDateTime())
  123. ->setSubject('transferOwnershipFailedTarget', [
  124. 'sourceUser' => $transfer->getSourceUser(),
  125. 'targetUser' => $transfer->getTargetUser(),
  126. 'nodeName' => $transfer->getNodeName(),
  127. ])
  128. ->setObject('transfer', (string)$transfer->getId());
  129. $this->notificationManager->notify($notification);
  130. }
  131. private function successNotification(Transfer $transfer): void {
  132. // Send notification to source user
  133. $notification = $this->notificationManager->createNotification();
  134. $notification->setUser($transfer->getSourceUser())
  135. ->setApp(Application::APP_ID)
  136. ->setDateTime($this->time->getDateTime())
  137. ->setSubject('transferOwnershipDoneSource', [
  138. 'sourceUser' => $transfer->getSourceUser(),
  139. 'targetUser' => $transfer->getTargetUser(),
  140. 'nodeName' => $transfer->getNodeName(),
  141. ])
  142. ->setObject('transfer', (string)$transfer->getId());
  143. $this->notificationManager->notify($notification);
  144. // Send notification to source user
  145. $notification = $this->notificationManager->createNotification();
  146. $notification->setUser($transfer->getTargetUser())
  147. ->setApp(Application::APP_ID)
  148. ->setDateTime($this->time->getDateTime())
  149. ->setSubject('transferOwnershipDoneTarget', [
  150. 'sourceUser' => $transfer->getSourceUser(),
  151. 'targetUser' => $transfer->getTargetUser(),
  152. 'nodeName' => $transfer->getNodeName(),
  153. ])
  154. ->setObject('transfer', (string)$transfer->getId());
  155. $this->notificationManager->notify($notification);
  156. }
  157. }