RemoteController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Sharing\Controller;
  8. use OCA\Files_Sharing\External\Manager;
  9. use OCA\Files_Sharing\ResponseDefinitions;
  10. use OCP\AppFramework\Http;
  11. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  12. use OCP\AppFramework\Http\DataResponse;
  13. use OCP\AppFramework\OCS\OCSForbiddenException;
  14. use OCP\AppFramework\OCS\OCSNotFoundException;
  15. use OCP\AppFramework\OCSController;
  16. use OCP\IRequest;
  17. use Psr\Log\LoggerInterface;
  18. /**
  19. * @psalm-import-type Files_SharingRemoteShare from ResponseDefinitions
  20. */
  21. class RemoteController extends OCSController {
  22. /**
  23. * Remote constructor.
  24. *
  25. * @param string $appName
  26. * @param IRequest $request
  27. * @param Manager $externalManager
  28. */
  29. public function __construct(
  30. $appName,
  31. IRequest $request,
  32. private Manager $externalManager,
  33. private LoggerInterface $logger,
  34. ) {
  35. parent::__construct($appName, $request);
  36. }
  37. /**
  38. * Get list of pending remote shares
  39. *
  40. * @return DataResponse<Http::STATUS_OK, Files_SharingRemoteShare[], array{}>
  41. *
  42. * 200: Pending remote shares returned
  43. */
  44. #[NoAdminRequired]
  45. public function getOpenShares() {
  46. return new DataResponse($this->externalManager->getOpenShares());
  47. }
  48. /**
  49. * Accept a remote share
  50. *
  51. * @param int $id ID of the share
  52. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  53. * @throws OCSNotFoundException Share not found
  54. *
  55. * 200: Share accepted successfully
  56. */
  57. #[NoAdminRequired]
  58. public function acceptShare($id) {
  59. if ($this->externalManager->acceptShare($id)) {
  60. return new DataResponse();
  61. }
  62. $this->logger->error('Could not accept federated share with id: ' . $id,
  63. ['app' => 'files_sharing']);
  64. throw new OCSNotFoundException('wrong share ID, share does not exist.');
  65. }
  66. /**
  67. * Decline a remote share
  68. *
  69. * @param int $id ID of the share
  70. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  71. * @throws OCSNotFoundException Share not found
  72. *
  73. * 200: Share declined successfully
  74. */
  75. #[NoAdminRequired]
  76. public function declineShare($id) {
  77. if ($this->externalManager->declineShare($id)) {
  78. return new DataResponse();
  79. }
  80. // Make sure the user has no notification for something that does not exist anymore.
  81. $this->externalManager->processNotification($id);
  82. throw new OCSNotFoundException('wrong share ID, share does not exist.');
  83. }
  84. /**
  85. * @param array $share Share with info from the share_external table
  86. * @return array enriched share info with data from the filecache
  87. */
  88. private static function extendShareInfo($share) {
  89. $view = new \OC\Files\View('/' . \OC_User::getUser() . '/files/');
  90. $info = $view->getFileInfo($share['mountpoint']);
  91. if ($info === false) {
  92. return $share;
  93. }
  94. $share['mimetype'] = $info->getMimetype();
  95. $share['mtime'] = $info->getMTime();
  96. $share['permissions'] = $info->getPermissions();
  97. $share['type'] = $info->getType();
  98. $share['file_id'] = $info->getId();
  99. return $share;
  100. }
  101. /**
  102. * Get a list of accepted remote shares
  103. *
  104. * @return DataResponse<Http::STATUS_OK, Files_SharingRemoteShare[], array{}>
  105. *
  106. * 200: Accepted remote shares returned
  107. */
  108. #[NoAdminRequired]
  109. public function getShares() {
  110. $shares = $this->externalManager->getAcceptedShares();
  111. $shares = array_map('self::extendShareInfo', $shares);
  112. return new DataResponse($shares);
  113. }
  114. /**
  115. * Get info of a remote share
  116. *
  117. * @param int $id ID of the share
  118. * @return DataResponse<Http::STATUS_OK, Files_SharingRemoteShare, array{}>
  119. * @throws OCSNotFoundException Share not found
  120. *
  121. * 200: Share returned
  122. */
  123. #[NoAdminRequired]
  124. public function getShare($id) {
  125. $shareInfo = $this->externalManager->getShare($id);
  126. if ($shareInfo === false) {
  127. throw new OCSNotFoundException('share does not exist');
  128. } else {
  129. $shareInfo = self::extendShareInfo($shareInfo);
  130. return new DataResponse($shareInfo);
  131. }
  132. }
  133. /**
  134. * Unshare a remote share
  135. *
  136. * @param int $id ID of the share
  137. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  138. * @throws OCSNotFoundException Share not found
  139. * @throws OCSForbiddenException Unsharing is not possible
  140. *
  141. * 200: Share unshared successfully
  142. */
  143. #[NoAdminRequired]
  144. public function unshare($id) {
  145. $shareInfo = $this->externalManager->getShare($id);
  146. if ($shareInfo === false) {
  147. throw new OCSNotFoundException('Share does not exist');
  148. }
  149. $mountPoint = '/' . \OC_User::getUser() . '/files' . $shareInfo['mountpoint'];
  150. if ($this->externalManager->removeShare($mountPoint) === true) {
  151. return new DataResponse();
  152. } else {
  153. throw new OCSForbiddenException('Could not unshare');
  154. }
  155. }
  156. }