TransferOwnershipController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\Files\Controller;
  27. use OCA\Files\BackgroundJob\TransferOwnership;
  28. use OCA\Files\Db\TransferOwnership as TransferOwnershipEntity;
  29. use OCA\Files\Db\TransferOwnershipMapper;
  30. use OCP\Files\IHomeStorage;
  31. use OCP\AppFramework\Db\DoesNotExistException;
  32. use OCP\AppFramework\Http;
  33. use OCP\AppFramework\Http\DataResponse;
  34. use OCP\AppFramework\OCSController;
  35. use OCP\AppFramework\Utility\ITimeFactory;
  36. use OCP\BackgroundJob\IJobList;
  37. use OCP\Files\IRootFolder;
  38. use OCP\IRequest;
  39. use OCP\IUserManager;
  40. use OCP\Notification\IManager as NotificationManager;
  41. class TransferOwnershipController extends OCSController {
  42. /** @var string */
  43. private $userId;
  44. /** @var NotificationManager */
  45. private $notificationManager;
  46. /** @var ITimeFactory */
  47. private $timeFactory;
  48. /** @var IJobList */
  49. private $jobList;
  50. /** @var TransferOwnershipMapper */
  51. private $mapper;
  52. /** @var IUserManager */
  53. private $userManager;
  54. /** @var IRootFolder */
  55. private $rootFolder;
  56. public function __construct(string $appName,
  57. IRequest $request,
  58. string $userId,
  59. NotificationManager $notificationManager,
  60. ITimeFactory $timeFactory,
  61. IJobList $jobList,
  62. TransferOwnershipMapper $mapper,
  63. IUserManager $userManager,
  64. IRootFolder $rootFolder) {
  65. parent::__construct($appName, $request);
  66. $this->userId = $userId;
  67. $this->notificationManager = $notificationManager;
  68. $this->timeFactory = $timeFactory;
  69. $this->jobList = $jobList;
  70. $this->mapper = $mapper;
  71. $this->userManager = $userManager;
  72. $this->rootFolder = $rootFolder;
  73. }
  74. /**
  75. * @NoAdminRequired
  76. *
  77. * Transfer the ownership to another user
  78. *
  79. * @param string $recipient Username of the recipient
  80. * @param string $path Path of the file
  81. *
  82. * @return DataResponse<Http::STATUS_OK|Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN, array<empty>, array{}>
  83. *
  84. * 200: Ownership transferred successfully
  85. * 400: Transferring ownership is not possible
  86. * 403: Transferring ownership is not allowed
  87. */
  88. public function transfer(string $recipient, string $path): DataResponse {
  89. $recipientUser = $this->userManager->get($recipient);
  90. if ($recipientUser === null) {
  91. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  92. }
  93. $userRoot = $this->rootFolder->getUserFolder($this->userId);
  94. try {
  95. $node = $userRoot->get($path);
  96. } catch (\Exception $e) {
  97. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  98. }
  99. if ($node->getOwner()->getUID() !== $this->userId || !$node->getStorage()->instanceOfStorage(IHomeStorage::class)) {
  100. return new DataResponse([], Http::STATUS_FORBIDDEN);
  101. }
  102. $transferOwnership = new TransferOwnershipEntity();
  103. $transferOwnership->setSourceUser($this->userId);
  104. $transferOwnership->setTargetUser($recipient);
  105. $transferOwnership->setFileId($node->getId());
  106. $transferOwnership->setNodeName($node->getName());
  107. $transferOwnership = $this->mapper->insert($transferOwnership);
  108. $notification = $this->notificationManager->createNotification();
  109. $notification->setUser($recipient)
  110. ->setApp($this->appName)
  111. ->setDateTime($this->timeFactory->getDateTime())
  112. ->setSubject('transferownershipRequest', [
  113. 'sourceUser' => $this->userId,
  114. 'targetUser' => $recipient,
  115. 'nodeName' => $node->getName(),
  116. ])
  117. ->setObject('transfer', (string)$transferOwnership->getId());
  118. $this->notificationManager->notify($notification);
  119. return new DataResponse([]);
  120. }
  121. /**
  122. * @NoAdminRequired
  123. *
  124. * Accept an ownership transfer
  125. *
  126. * @param int $id ID of the ownership transfer
  127. *
  128. * @return DataResponse<Http::STATUS_OK|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, array<empty>, array{}>
  129. *
  130. * 200: Ownership transfer accepted successfully
  131. * 403: Accepting ownership transfer is not allowed
  132. * 404: Ownership transfer not found
  133. */
  134. public function accept(int $id): DataResponse {
  135. try {
  136. $transferOwnership = $this->mapper->getById($id);
  137. } catch (DoesNotExistException $e) {
  138. return new DataResponse([], Http::STATUS_NOT_FOUND);
  139. }
  140. if ($transferOwnership->getTargetUser() !== $this->userId) {
  141. return new DataResponse([], Http::STATUS_FORBIDDEN);
  142. }
  143. $notification = $this->notificationManager->createNotification();
  144. $notification->setApp('files')
  145. ->setObject('transfer', (string)$id);
  146. $this->notificationManager->markProcessed($notification);
  147. $newTransferOwnership = new TransferOwnershipEntity();
  148. $newTransferOwnership->setNodeName($transferOwnership->getNodeName());
  149. $newTransferOwnership->setFileId($transferOwnership->getFileId());
  150. $newTransferOwnership->setSourceUser($transferOwnership->getSourceUser());
  151. $newTransferOwnership->setTargetUser($transferOwnership->getTargetUser());
  152. $this->mapper->insert($newTransferOwnership);
  153. $this->jobList->add(TransferOwnership::class, [
  154. 'id' => $newTransferOwnership->getId(),
  155. ]);
  156. return new DataResponse([], Http::STATUS_OK);
  157. }
  158. /**
  159. * @NoAdminRequired
  160. *
  161. * Reject an ownership transfer
  162. *
  163. * @param int $id ID of the ownership transfer
  164. *
  165. * @return DataResponse<Http::STATUS_OK|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, array<empty>, array{}>
  166. *
  167. * 200: Ownership transfer rejected successfully
  168. * 403: Rejecting ownership transfer is not allowed
  169. * 404: Ownership transfer not found
  170. */
  171. public function reject(int $id): DataResponse {
  172. try {
  173. $transferOwnership = $this->mapper->getById($id);
  174. } catch (DoesNotExistException $e) {
  175. return new DataResponse([], Http::STATUS_NOT_FOUND);
  176. }
  177. if ($transferOwnership->getTargetUser() !== $this->userId) {
  178. return new DataResponse([], Http::STATUS_FORBIDDEN);
  179. }
  180. $notification = $this->notificationManager->createNotification();
  181. $notification->setApp('files')
  182. ->setObject('transfer', (string)$id);
  183. $this->notificationManager->markProcessed($notification);
  184. $notification = $this->notificationManager->createNotification();
  185. $notification->setUser($transferOwnership->getSourceUser())
  186. ->setApp($this->appName)
  187. ->setDateTime($this->timeFactory->getDateTime())
  188. ->setSubject('transferownershipRequestDenied', [
  189. 'sourceUser' => $transferOwnership->getSourceUser(),
  190. 'targetUser' => $transferOwnership->getTargetUser(),
  191. 'nodeName' => $transferOwnership->getNodeName()
  192. ])
  193. ->setObject('transfer', (string)$transferOwnership->getId());
  194. $this->notificationManager->notify($notification);
  195. $this->mapper->delete($transferOwnership);
  196. return new DataResponse([], Http::STATUS_OK);
  197. }
  198. }