1
0

TransferOwnershipController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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\Attribute\NoAdminRequired;
  14. use OCP\AppFramework\Http\DataResponse;
  15. use OCP\AppFramework\OCSController;
  16. use OCP\AppFramework\Utility\ITimeFactory;
  17. use OCP\BackgroundJob\IJobList;
  18. use OCP\Files\IHomeStorage;
  19. use OCP\Files\IRootFolder;
  20. use OCP\IRequest;
  21. use OCP\IUserManager;
  22. use OCP\Notification\IManager as NotificationManager;
  23. class TransferOwnershipController extends OCSController {
  24. /** @var string */
  25. private $userId;
  26. /** @var NotificationManager */
  27. private $notificationManager;
  28. /** @var ITimeFactory */
  29. private $timeFactory;
  30. /** @var IJobList */
  31. private $jobList;
  32. /** @var TransferOwnershipMapper */
  33. private $mapper;
  34. /** @var IUserManager */
  35. private $userManager;
  36. /** @var IRootFolder */
  37. private $rootFolder;
  38. public function __construct(string $appName,
  39. IRequest $request,
  40. string $userId,
  41. NotificationManager $notificationManager,
  42. ITimeFactory $timeFactory,
  43. IJobList $jobList,
  44. TransferOwnershipMapper $mapper,
  45. IUserManager $userManager,
  46. IRootFolder $rootFolder) {
  47. parent::__construct($appName, $request);
  48. $this->userId = $userId;
  49. $this->notificationManager = $notificationManager;
  50. $this->timeFactory = $timeFactory;
  51. $this->jobList = $jobList;
  52. $this->mapper = $mapper;
  53. $this->userManager = $userManager;
  54. $this->rootFolder = $rootFolder;
  55. }
  56. /**
  57. * Transfer the ownership to another user
  58. *
  59. * @param string $recipient Username of the recipient
  60. * @param string $path Path of the file
  61. *
  62. * @return DataResponse<Http::STATUS_OK|Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN, array<empty>, array{}>
  63. *
  64. * 200: Ownership transferred successfully
  65. * 400: Transferring ownership is not possible
  66. * 403: Transferring ownership is not allowed
  67. */
  68. #[NoAdminRequired]
  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. * Accept an ownership transfer
  104. *
  105. * @param int $id ID of the ownership transfer
  106. *
  107. * @return DataResponse<Http::STATUS_OK|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, array<empty>, array{}>
  108. *
  109. * 200: Ownership transfer accepted successfully
  110. * 403: Accepting ownership transfer is not allowed
  111. * 404: Ownership transfer not found
  112. */
  113. #[NoAdminRequired]
  114. public function accept(int $id): DataResponse {
  115. try {
  116. $transferOwnership = $this->mapper->getById($id);
  117. } catch (DoesNotExistException $e) {
  118. return new DataResponse([], Http::STATUS_NOT_FOUND);
  119. }
  120. if ($transferOwnership->getTargetUser() !== $this->userId) {
  121. return new DataResponse([], Http::STATUS_FORBIDDEN);
  122. }
  123. $notification = $this->notificationManager->createNotification();
  124. $notification->setApp('files')
  125. ->setObject('transfer', (string)$id);
  126. $this->notificationManager->markProcessed($notification);
  127. $newTransferOwnership = new TransferOwnershipEntity();
  128. $newTransferOwnership->setNodeName($transferOwnership->getNodeName());
  129. $newTransferOwnership->setFileId($transferOwnership->getFileId());
  130. $newTransferOwnership->setSourceUser($transferOwnership->getSourceUser());
  131. $newTransferOwnership->setTargetUser($transferOwnership->getTargetUser());
  132. $this->mapper->insert($newTransferOwnership);
  133. $this->jobList->add(TransferOwnership::class, [
  134. 'id' => $newTransferOwnership->getId(),
  135. ]);
  136. return new DataResponse([], Http::STATUS_OK);
  137. }
  138. /**
  139. * Reject an ownership transfer
  140. *
  141. * @param int $id ID of the ownership transfer
  142. *
  143. * @return DataResponse<Http::STATUS_OK|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, array<empty>, array{}>
  144. *
  145. * 200: Ownership transfer rejected successfully
  146. * 403: Rejecting ownership transfer is not allowed
  147. * 404: Ownership transfer not found
  148. */
  149. #[NoAdminRequired]
  150. public function reject(int $id): DataResponse {
  151. try {
  152. $transferOwnership = $this->mapper->getById($id);
  153. } catch (DoesNotExistException $e) {
  154. return new DataResponse([], Http::STATUS_NOT_FOUND);
  155. }
  156. if ($transferOwnership->getTargetUser() !== $this->userId) {
  157. return new DataResponse([], Http::STATUS_FORBIDDEN);
  158. }
  159. $notification = $this->notificationManager->createNotification();
  160. $notification->setApp('files')
  161. ->setObject('transfer', (string)$id);
  162. $this->notificationManager->markProcessed($notification);
  163. $this->mapper->delete($transferOwnership);
  164. // A "request denied" notification will be created by Notifier::dismissNotification
  165. return new DataResponse([], Http::STATUS_OK);
  166. }
  167. }