1
0

TransferOwnershipController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. public function transfer(string $recipient, string $path): DataResponse {
  78. $recipientUser = $this->userManager->get($recipient);
  79. if ($recipientUser === null) {
  80. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  81. }
  82. $userRoot = $this->rootFolder->getUserFolder($this->userId);
  83. try {
  84. $node = $userRoot->get($path);
  85. } catch (\Exception $e) {
  86. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  87. }
  88. if ($node->getOwner()->getUID() !== $this->userId || !$node->getStorage()->instanceOfStorage(IHomeStorage::class)) {
  89. return new DataResponse([], Http::STATUS_FORBIDDEN);
  90. }
  91. $transferOwnership = new TransferOwnershipEntity();
  92. $transferOwnership->setSourceUser($this->userId);
  93. $transferOwnership->setTargetUser($recipient);
  94. $transferOwnership->setFileId($node->getId());
  95. $transferOwnership->setNodeName($node->getName());
  96. $transferOwnership = $this->mapper->insert($transferOwnership);
  97. $notification = $this->notificationManager->createNotification();
  98. $notification->setUser($recipient)
  99. ->setApp($this->appName)
  100. ->setDateTime($this->timeFactory->getDateTime())
  101. ->setSubject('transferownershipRequest', [
  102. 'sourceUser' => $this->userId,
  103. 'targetUser' => $recipient,
  104. 'nodeName' => $node->getName(),
  105. ])
  106. ->setObject('transfer', (string)$transferOwnership->getId());
  107. $this->notificationManager->notify($notification);
  108. return new DataResponse([]);
  109. }
  110. /**
  111. * @NoAdminRequired
  112. */
  113. public function accept(int $id): DataResponse {
  114. try {
  115. $transferOwnership = $this->mapper->getById($id);
  116. } catch (DoesNotExistException $e) {
  117. return new DataResponse([], Http::STATUS_NOT_FOUND);
  118. }
  119. if ($transferOwnership->getTargetUser() !== $this->userId) {
  120. return new DataResponse([], Http::STATUS_FORBIDDEN);
  121. }
  122. $notification = $this->notificationManager->createNotification();
  123. $notification->setApp('files')
  124. ->setObject('transfer', (string)$id);
  125. $this->notificationManager->markProcessed($notification);
  126. $newTransferOwnership = new TransferOwnershipEntity();
  127. $newTransferOwnership->setNodeName($transferOwnership->getNodeName());
  128. $newTransferOwnership->setFileId($transferOwnership->getFileId());
  129. $newTransferOwnership->setSourceUser($transferOwnership->getSourceUser());
  130. $newTransferOwnership->setTargetUser($transferOwnership->getTargetUser());
  131. $this->mapper->insert($newTransferOwnership);
  132. $this->jobList->add(TransferOwnership::class, [
  133. 'id' => $newTransferOwnership->getId(),
  134. ]);
  135. return new DataResponse([], Http::STATUS_OK);
  136. }
  137. /**
  138. * @NoAdminRequired
  139. */
  140. public function reject(int $id): DataResponse {
  141. try {
  142. $transferOwnership = $this->mapper->getById($id);
  143. } catch (DoesNotExistException $e) {
  144. return new DataResponse([], Http::STATUS_NOT_FOUND);
  145. }
  146. if ($transferOwnership->getTargetUser() !== $this->userId) {
  147. return new DataResponse([], Http::STATUS_FORBIDDEN);
  148. }
  149. $notification = $this->notificationManager->createNotification();
  150. $notification->setApp('files')
  151. ->setObject('transfer', (string)$id);
  152. $this->notificationManager->markProcessed($notification);
  153. $notification = $this->notificationManager->createNotification();
  154. $notification->setUser($transferOwnership->getSourceUser())
  155. ->setApp($this->appName)
  156. ->setDateTime($this->timeFactory->getDateTime())
  157. ->setSubject('transferownershipRequestDenied', [
  158. 'sourceUser' => $transferOwnership->getSourceUser(),
  159. 'targetUser' => $transferOwnership->getTargetUser(),
  160. 'nodeName' => $transferOwnership->getNodeName()
  161. ])
  162. ->setObject('transfer', (string)$transferOwnership->getId());
  163. $this->notificationManager->notify($notification);
  164. $this->mapper->delete($transferOwnership);
  165. return new DataResponse([], Http::STATUS_OK);
  166. }
  167. }