RemoteController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Kate Döen <kate.doeen@nextcloud.com>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files_Sharing\Controller;
  26. use OCA\Files_Sharing\External\Manager;
  27. use OCA\Files_Sharing\ResponseDefinitions;
  28. use OCP\AppFramework\Http;
  29. use OCP\AppFramework\Http\DataResponse;
  30. use OCP\AppFramework\OCS\OCSForbiddenException;
  31. use OCP\AppFramework\OCS\OCSNotFoundException;
  32. use OCP\AppFramework\OCSController;
  33. use OCP\IRequest;
  34. use Psr\Log\LoggerInterface;
  35. /**
  36. * @psalm-import-type FilesSharingRemoteShare from ResponseDefinitions
  37. */
  38. class RemoteController extends OCSController {
  39. /**
  40. * @NoAdminRequired
  41. *
  42. * Remote constructor.
  43. *
  44. * @param string $appName
  45. * @param IRequest $request
  46. * @param Manager $externalManager
  47. */
  48. public function __construct(
  49. $appName,
  50. IRequest $request,
  51. private Manager $externalManager,
  52. private LoggerInterface $logger,
  53. ) {
  54. parent::__construct($appName, $request);
  55. }
  56. /**
  57. * @NoAdminRequired
  58. *
  59. * Get list of pending remote shares
  60. *
  61. * @return DataResponse<Http::STATUS_OK, FilesSharingRemoteShare[], array{}>
  62. */
  63. public function getOpenShares() {
  64. return new DataResponse($this->externalManager->getOpenShares());
  65. }
  66. /**
  67. * @NoAdminRequired
  68. *
  69. * Accept a remote share
  70. *
  71. * @param int $id ID of the share
  72. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  73. * @throws OCSNotFoundException Share not found
  74. *
  75. * 200: Share accepted successfully
  76. */
  77. public function acceptShare($id) {
  78. if ($this->externalManager->acceptShare($id)) {
  79. return new DataResponse();
  80. }
  81. $this->logger->error('Could not accept federated share with id: ' . $id,
  82. ['app' => 'files_sharing']);
  83. throw new OCSNotFoundException('wrong share ID, share does not exist.');
  84. }
  85. /**
  86. * @NoAdminRequired
  87. *
  88. * Decline a remote share
  89. *
  90. * @param int $id ID of the share
  91. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  92. * @throws OCSNotFoundException Share not found
  93. *
  94. * 200: Share declined successfully
  95. */
  96. public function declineShare($id) {
  97. if ($this->externalManager->declineShare($id)) {
  98. return new DataResponse();
  99. }
  100. // Make sure the user has no notification for something that does not exist anymore.
  101. $this->externalManager->processNotification($id);
  102. throw new OCSNotFoundException('wrong share ID, share does not exist.');
  103. }
  104. /**
  105. * @param array $share Share with info from the share_external table
  106. * @return array enriched share info with data from the filecache
  107. */
  108. private static function extendShareInfo($share) {
  109. $view = new \OC\Files\View('/' . \OC_User::getUser() . '/files/');
  110. $info = $view->getFileInfo($share['mountpoint']);
  111. if ($info === false) {
  112. return $share;
  113. }
  114. $share['mimetype'] = $info->getMimetype();
  115. $share['mtime'] = $info->getMTime();
  116. $share['permissions'] = $info->getPermissions();
  117. $share['type'] = $info->getType();
  118. $share['file_id'] = $info->getId();
  119. return $share;
  120. }
  121. /**
  122. * @NoAdminRequired
  123. *
  124. * Get a list of accepted remote shares
  125. *
  126. * @return DataResponse<Http::STATUS_OK, FilesSharingRemoteShare[], array{}>
  127. */
  128. public function getShares() {
  129. $shares = $this->externalManager->getAcceptedShares();
  130. $shares = array_map('self::extendShareInfo', $shares);
  131. return new DataResponse($shares);
  132. }
  133. /**
  134. * @NoAdminRequired
  135. *
  136. * Get info of a remote share
  137. *
  138. * @param int $id ID of the share
  139. * @return DataResponse<Http::STATUS_OK, FilesSharingRemoteShare, array{}>
  140. * @throws OCSNotFoundException Share not found
  141. *
  142. * 200: Share returned
  143. */
  144. public function getShare($id) {
  145. $shareInfo = $this->externalManager->getShare($id);
  146. if ($shareInfo === false) {
  147. throw new OCSNotFoundException('share does not exist');
  148. } else {
  149. $shareInfo = self::extendShareInfo($shareInfo);
  150. return new DataResponse($shareInfo);
  151. }
  152. }
  153. /**
  154. * @NoAdminRequired
  155. *
  156. * Unshare a remote share
  157. *
  158. * @param int $id ID of the share
  159. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  160. * @throws OCSNotFoundException Share not found
  161. * @throws OCSForbiddenException Unsharing is not possible
  162. *
  163. * 200: Share unshared successfully
  164. */
  165. public function unshare($id) {
  166. $shareInfo = $this->externalManager->getShare($id);
  167. if ($shareInfo === false) {
  168. throw new OCSNotFoundException('Share does not exist');
  169. }
  170. $mountPoint = '/' . \OC_User::getUser() . '/files' . $shareInfo['mountpoint'];
  171. if ($this->externalManager->removeShare($mountPoint) === true) {
  172. return new DataResponse();
  173. } else {
  174. throw new OCSForbiddenException('Could not unshare');
  175. }
  176. }
  177. }