1
0

RemoteController.php 4.7 KB

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