TransferOwnershipController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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\Controller;
  8. use OCA\Files\BackgroundJob\TransferOwnership;
  9. use OCA\Files\Db\TransferOwnership as TransferOwnershipEntity;
  10. use OCA\Files\Db\TransferOwnershipMapper;
  11. use OCP\AppFramework\Db\DoesNotExistException;
  12. use OCP\AppFramework\Http;
  13. use OCP\AppFramework\Http\DataResponse;
  14. use OCP\AppFramework\OCSController;
  15. use OCP\AppFramework\Utility\ITimeFactory;
  16. use OCP\BackgroundJob\IJobList;
  17. use OCP\Files\IHomeStorage;
  18. use OCP\Files\IRootFolder;
  19. use OCP\IRequest;
  20. use OCP\IUserManager;
  21. use OCP\Notification\IManager as NotificationManager;
  22. class TransferOwnershipController extends OCSController {
  23. /** @var string */
  24. private $userId;
  25. /** @var NotificationManager */
  26. private $notificationManager;
  27. /** @var ITimeFactory */
  28. private $timeFactory;
  29. /** @var IJobList */
  30. private $jobList;
  31. /** @var TransferOwnershipMapper */
  32. private $mapper;
  33. /** @var IUserManager */
  34. private $userManager;
  35. /** @var IRootFolder */
  36. private $rootFolder;
  37. public function __construct(string $appName,
  38. IRequest $request,
  39. string $userId,
  40. NotificationManager $notificationManager,
  41. ITimeFactory $timeFactory,
  42. IJobList $jobList,
  43. TransferOwnershipMapper $mapper,
  44. IUserManager $userManager,
  45. IRootFolder $rootFolder) {
  46. parent::__construct($appName, $request);
  47. $this->userId = $userId;
  48. $this->notificationManager = $notificationManager;
  49. $this->timeFactory = $timeFactory;
  50. $this->jobList = $jobList;
  51. $this->mapper = $mapper;
  52. $this->userManager = $userManager;
  53. $this->rootFolder = $rootFolder;
  54. }
  55. /**
  56. * @NoAdminRequired
  57. *
  58. * Transfer the ownership to another user
  59. *
  60. * @param string $recipient Username of the recipient
  61. * @param string $path Path of the file
  62. *
  63. * @return DataResponse<Http::STATUS_OK|Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN, array<empty>, array{}>
  64. *
  65. * 200: Ownership transferred successfully
  66. * 400: Transferring ownership is not possible
  67. * 403: Transferring ownership is not allowed
  68. */
  69. public function transfer(string $recipient, string $path): DataResponse {
  70. $recipientUser = $this->userManager->get($recipient);
  71. if ($recipientUser === null) {
  72. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  73. }
  74. $userRoot = $this->rootFolder->getUserFolder($this->userId);
  75. try {
  76. $node = $userRoot->get($path);
  77. } catch (\Exception $e) {
  78. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  79. }
  80. if ($node->getOwner()->getUID() !== $this->userId || !$node->getStorage()->instanceOfStorage(IHomeStorage::class)) {
  81. return new DataResponse([], Http::STATUS_FORBIDDEN);
  82. }
  83. $transferOwnership = new TransferOwnershipEntity();
  84. $transferOwnership->setSourceUser($this->userId);
  85. $transferOwnership->setTargetUser($recipient);
  86. $transferOwnership->setFileId($node->getId());
  87. $transferOwnership->setNodeName($node->getName());
  88. $transferOwnership = $this->mapper->insert($transferOwnership);
  89. $notification = $this->notificationManager->createNotification();
  90. $notification->setUser($recipient)
  91. ->setApp($this->appName)
  92. ->setDateTime($this->timeFactory->getDateTime())
  93. ->setSubject('transferownershipRequest', [
  94. 'sourceUser' => $this->userId,
  95. 'targetUser' => $recipient,
  96. 'nodeName' => $node->getName(),
  97. ])
  98. ->setObject('transfer', (string)$transferOwnership->getId());
  99. $this->notificationManager->notify($notification);
  100. return new DataResponse([]);
  101. }
  102. /**
  103. * @NoAdminRequired
  104. *
  105. * Accept an ownership transfer
  106. *
  107. * @param int $id ID of the ownership transfer
  108. *
  109. * @return DataResponse<Http::STATUS_OK|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, array<empty>, array{}>
  110. *
  111. * 200: Ownership transfer accepted successfully
  112. * 403: Accepting ownership transfer is not allowed
  113. * 404: Ownership transfer not found
  114. */
  115. public function accept(int $id): DataResponse {
  116. try {
  117. $transferOwnership = $this->mapper->getById($id);
  118. } catch (DoesNotExistException $e) {
  119. return new DataResponse([], Http::STATUS_NOT_FOUND);
  120. }
  121. if ($transferOwnership->getTargetUser() !== $this->userId) {
  122. return new DataResponse([], Http::STATUS_FORBIDDEN);
  123. }
  124. $notification = $this->notificationManager->createNotification();
  125. $notification->setApp('files')
  126. ->setObject('transfer', (string)$id);
  127. $this->notificationManager->markProcessed($notification);
  128. $newTransferOwnership = new TransferOwnershipEntity();
  129. $newTransferOwnership->setNodeName($transferOwnership->getNodeName());
  130. $newTransferOwnership->setFileId($transferOwnership->getFileId());
  131. $newTransferOwnership->setSourceUser($transferOwnership->getSourceUser());
  132. $newTransferOwnership->setTargetUser($transferOwnership->getTargetUser());
  133. $this->mapper->insert($newTransferOwnership);
  134. $this->jobList->add(TransferOwnership::class, [
  135. 'id' => $newTransferOwnership->getId(),
  136. ]);
  137. return new DataResponse([], Http::STATUS_OK);
  138. }
  139. /**
  140. * @NoAdminRequired
  141. *
  142. * Reject an ownership transfer
  143. *
  144. * @param int $id ID of the ownership transfer
  145. *
  146. * @return DataResponse<Http::STATUS_OK|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, array<empty>, array{}>
  147. *
  148. * 200: Ownership transfer rejected successfully
  149. * 403: Rejecting ownership transfer is not allowed
  150. * 404: Ownership transfer not found
  151. */
  152. public function reject(int $id): DataResponse {
  153. try {
  154. $transferOwnership = $this->mapper->getById($id);
  155. } catch (DoesNotExistException $e) {
  156. return new DataResponse([], Http::STATUS_NOT_FOUND);
  157. }
  158. if ($transferOwnership->getTargetUser() !== $this->userId) {
  159. return new DataResponse([], Http::STATUS_FORBIDDEN);
  160. }
  161. $notification = $this->notificationManager->createNotification();
  162. $notification->setApp('files')
  163. ->setObject('transfer', (string)$id);
  164. $this->notificationManager->markProcessed($notification);
  165. $notification = $this->notificationManager->createNotification();
  166. $notification->setUser($transferOwnership->getSourceUser())
  167. ->setApp($this->appName)
  168. ->setDateTime($this->timeFactory->getDateTime())
  169. ->setSubject('transferownershipRequestDenied', [
  170. 'sourceUser' => $transferOwnership->getSourceUser(),
  171. 'targetUser' => $transferOwnership->getTargetUser(),
  172. 'nodeName' => $transferOwnership->getNodeName()
  173. ])
  174. ->setObject('transfer', (string)$transferOwnership->getId());
  175. $this->notificationManager->notify($notification);
  176. $this->mapper->delete($transferOwnership);
  177. return new DataResponse([], Http::STATUS_OK);
  178. }
  179. }