TransferOwnershipController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Files\Controller;
  25. use OCA\Files\BackgroundJob\TransferOwnership;
  26. use OCA\Files\Db\TransferOwnershipMapper;
  27. use OCP\AppFramework\Db\DoesNotExistException;
  28. use OCP\AppFramework\Http;
  29. use OCP\AppFramework\Http\DataResponse;
  30. use OCP\AppFramework\OCSController;
  31. use OCP\AppFramework\Utility\ITimeFactory;
  32. use OCP\BackgroundJob\IJobList;
  33. use OCP\Files\IRootFolder;
  34. use OCP\IRequest;
  35. use OCP\IUserManager;
  36. use OCP\Notification\IManager as NotificationManager;
  37. class TransferOwnershipController extends OCSController {
  38. /** @var string */
  39. private $userId;
  40. /** @var NotificationManager */
  41. private $notificationManager;
  42. /** @var ITimeFactory */
  43. private $timeFactory;
  44. /** @var IJobList */
  45. private $jobList;
  46. /** @var TransferOwnershipMapper */
  47. private $mapper;
  48. /** @var IUserManager */
  49. private $userManager;
  50. /** @var IRootFolder */
  51. private $rootFolder;
  52. public function __construct(string $appName,
  53. IRequest $request,
  54. string $userId,
  55. NotificationManager $notificationManager,
  56. ITimeFactory $timeFactory,
  57. IJobList $jobList,
  58. TransferOwnershipMapper $mapper,
  59. IUserManager $userManager,
  60. IRootFolder $rootFolder) {
  61. parent::__construct($appName, $request);
  62. $this->userId = $userId;
  63. $this->notificationManager = $notificationManager;
  64. $this->timeFactory = $timeFactory;
  65. $this->jobList = $jobList;
  66. $this->mapper = $mapper;
  67. $this->userManager = $userManager;
  68. $this->rootFolder = $rootFolder;
  69. }
  70. /**
  71. * @NoAdminRequired
  72. */
  73. public function transfer(string $recipient, string $path): DataResponse {
  74. $recipientUser = $this->userManager->get($recipient);
  75. if ($recipientUser === null) {
  76. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  77. }
  78. $userRoot = $this->rootFolder->getUserFolder($this->userId);
  79. try {
  80. $node = $userRoot->get($path);
  81. } catch (\Exception $e) {
  82. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  83. }
  84. $transferOwnership = new \OCA\Files\Db\TransferOwnership();
  85. $transferOwnership->setSourceUser($this->userId);
  86. $transferOwnership->setTargetUser($recipient);
  87. $transferOwnership->setFileId($node->getId());
  88. $transferOwnership->setNodeName($node->getName());
  89. $transferOwnership = $this->mapper->insert($transferOwnership);
  90. $notification = $this->notificationManager->createNotification();
  91. $notification->setUser($recipient)
  92. ->setApp($this->appName)
  93. ->setDateTime($this->timeFactory->getDateTime())
  94. ->setSubject('transferownershipRequest', [
  95. 'sourceUser' => $this->userId,
  96. 'targetUser' => $recipient,
  97. 'nodeName' => $node->getName(),
  98. ])
  99. ->setObject('transfer', (string)$transferOwnership->getId());
  100. $this->notificationManager->notify($notification);
  101. return new DataResponse([]);
  102. }
  103. /**
  104. * @NoAdminRequired
  105. */
  106. public function accept(int $id): DataResponse {
  107. try {
  108. $transferOwnership = $this->mapper->getById($id);
  109. } catch (DoesNotExistException $e) {
  110. return new DataResponse([], Http::STATUS_NOT_FOUND);
  111. }
  112. if ($transferOwnership->getTargetUser() !== $this->userId) {
  113. return new DataResponse([], Http::STATUS_FORBIDDEN);
  114. }
  115. $this->jobList->add(TransferOwnership::class, [
  116. 'id' => $transferOwnership->getId(),
  117. ]);
  118. $notification = $this->notificationManager->createNotification();
  119. $notification->setApp('files')
  120. ->setObject('transfer', (string)$id);
  121. $this->notificationManager->markProcessed($notification);
  122. return new DataResponse([], Http::STATUS_OK);
  123. }
  124. /**
  125. * @NoAdminRequired
  126. */
  127. public function reject(int $id): DataResponse {
  128. try {
  129. $transferOwnership = $this->mapper->getById($id);
  130. } catch (DoesNotExistException $e) {
  131. return new DataResponse([], Http::STATUS_NOT_FOUND);
  132. }
  133. if ($transferOwnership->getTargetUser() !== $this->userId) {
  134. return new DataResponse([], Http::STATUS_FORBIDDEN);
  135. }
  136. $notification = $this->notificationManager->createNotification();
  137. $notification->setApp('files')
  138. ->setObject('transfer', (string)$id);
  139. $this->notificationManager->markProcessed($notification);
  140. $notification = $this->notificationManager->createNotification();
  141. $notification->setUser($transferOwnership->getSourceUser())
  142. ->setApp($this->appName)
  143. ->setDateTime($this->timeFactory->getDateTime())
  144. ->setSubject('transferownershipRequestDenied', [
  145. 'sourceUser' => $transferOwnership->getSourceUser(),
  146. 'targetUser' => $transferOwnership->getTargetUser(),
  147. 'nodeName' => $transferOwnership->getNodeName()
  148. ])
  149. ->setObject('transfer', (string)$transferOwnership->getId());
  150. $this->notificationManager->notify($notification);
  151. $this->mapper->delete($transferOwnership);
  152. return new DataResponse([], Http::STATUS_OK);
  153. }
  154. }