TransferOwnershipController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. public function __construct(
  25. string $appName,
  26. IRequest $request,
  27. private string $userId,
  28. private NotificationManager $notificationManager,
  29. private ITimeFactory $timeFactory,
  30. private IJobList $jobList,
  31. private TransferOwnershipMapper $mapper,
  32. private IUserManager $userManager,
  33. private IRootFolder $rootFolder,
  34. ) {
  35. parent::__construct($appName, $request);
  36. }
  37. /**
  38. * Transfer the ownership to another user
  39. *
  40. * @param string $recipient Username of the recipient
  41. * @param string $path Path of the file
  42. *
  43. * @return DataResponse<Http::STATUS_OK|Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN, list<empty>, array{}>
  44. *
  45. * 200: Ownership transferred successfully
  46. * 400: Transferring ownership is not possible
  47. * 403: Transferring ownership is not allowed
  48. */
  49. #[NoAdminRequired]
  50. public function transfer(string $recipient, string $path): DataResponse {
  51. $recipientUser = $this->userManager->get($recipient);
  52. if ($recipientUser === null) {
  53. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  54. }
  55. $userRoot = $this->rootFolder->getUserFolder($this->userId);
  56. try {
  57. $node = $userRoot->get($path);
  58. } catch (\Exception $e) {
  59. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  60. }
  61. if ($node->getOwner()->getUID() !== $this->userId || !$node->getStorage()->instanceOfStorage(IHomeStorage::class)) {
  62. return new DataResponse([], Http::STATUS_FORBIDDEN);
  63. }
  64. $transferOwnership = new TransferOwnershipEntity();
  65. $transferOwnership->setSourceUser($this->userId);
  66. $transferOwnership->setTargetUser($recipient);
  67. $transferOwnership->setFileId($node->getId());
  68. $transferOwnership->setNodeName($node->getName());
  69. $transferOwnership = $this->mapper->insert($transferOwnership);
  70. $notification = $this->notificationManager->createNotification();
  71. $notification->setUser($recipient)
  72. ->setApp($this->appName)
  73. ->setDateTime($this->timeFactory->getDateTime())
  74. ->setSubject('transferownershipRequest', [
  75. 'sourceUser' => $this->userId,
  76. 'targetUser' => $recipient,
  77. 'nodeName' => $node->getName(),
  78. ])
  79. ->setObject('transfer', (string)$transferOwnership->getId());
  80. $this->notificationManager->notify($notification);
  81. return new DataResponse([]);
  82. }
  83. /**
  84. * Accept an ownership transfer
  85. *
  86. * @param int $id ID of the ownership transfer
  87. *
  88. * @return DataResponse<Http::STATUS_OK|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, list<empty>, array{}>
  89. *
  90. * 200: Ownership transfer accepted successfully
  91. * 403: Accepting ownership transfer is not allowed
  92. * 404: Ownership transfer not found
  93. */
  94. #[NoAdminRequired]
  95. public function accept(int $id): DataResponse {
  96. try {
  97. $transferOwnership = $this->mapper->getById($id);
  98. } catch (DoesNotExistException $e) {
  99. return new DataResponse([], Http::STATUS_NOT_FOUND);
  100. }
  101. if ($transferOwnership->getTargetUser() !== $this->userId) {
  102. return new DataResponse([], Http::STATUS_FORBIDDEN);
  103. }
  104. $this->jobList->add(TransferOwnership::class, [
  105. 'id' => $transferOwnership->getId(),
  106. ]);
  107. $notification = $this->notificationManager->createNotification();
  108. $notification->setApp('files')
  109. ->setObject('transfer', (string)$id);
  110. $this->notificationManager->markProcessed($notification);
  111. return new DataResponse([], Http::STATUS_OK);
  112. }
  113. /**
  114. * Reject an ownership transfer
  115. *
  116. * @param int $id ID of the ownership transfer
  117. *
  118. * @return DataResponse<Http::STATUS_OK|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, list<empty>, array{}>
  119. *
  120. * 200: Ownership transfer rejected successfully
  121. * 403: Rejecting ownership transfer is not allowed
  122. * 404: Ownership transfer not found
  123. */
  124. #[NoAdminRequired]
  125. public function reject(int $id): DataResponse {
  126. try {
  127. $transferOwnership = $this->mapper->getById($id);
  128. } catch (DoesNotExistException $e) {
  129. return new DataResponse([], Http::STATUS_NOT_FOUND);
  130. }
  131. if ($transferOwnership->getTargetUser() !== $this->userId) {
  132. return new DataResponse([], Http::STATUS_FORBIDDEN);
  133. }
  134. $notification = $this->notificationManager->createNotification();
  135. $notification->setApp('files')
  136. ->setObject('transfer', (string)$id);
  137. $this->notificationManager->markProcessed($notification);
  138. $this->mapper->delete($transferOwnership);
  139. // A "request denied" notification will be created by Notifier::dismissNotification
  140. return new DataResponse([], Http::STATUS_OK);
  141. }
  142. }