1
0

RemoteController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 Files_SharingRemoteShare 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, Files_SharingRemoteShare[], array{}>
  62. *
  63. * 200: Pending remote shares returned
  64. */
  65. public function getOpenShares() {
  66. return new DataResponse($this->externalManager->getOpenShares());
  67. }
  68. /**
  69. * @NoAdminRequired
  70. *
  71. * Accept a remote share
  72. *
  73. * @param int $id ID of the share
  74. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  75. * @throws OCSNotFoundException Share not found
  76. *
  77. * 200: Share accepted successfully
  78. */
  79. public function acceptShare($id) {
  80. if ($this->externalManager->acceptShare($id)) {
  81. return new DataResponse();
  82. }
  83. $this->logger->error('Could not accept federated share with id: ' . $id,
  84. ['app' => 'files_sharing']);
  85. throw new OCSNotFoundException('wrong share ID, share does not exist.');
  86. }
  87. /**
  88. * @NoAdminRequired
  89. *
  90. * Decline a remote share
  91. *
  92. * @param int $id ID of the share
  93. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  94. * @throws OCSNotFoundException Share not found
  95. *
  96. * 200: Share declined successfully
  97. */
  98. public function declineShare($id) {
  99. if ($this->externalManager->declineShare($id)) {
  100. return new DataResponse();
  101. }
  102. // Make sure the user has no notification for something that does not exist anymore.
  103. $this->externalManager->processNotification($id);
  104. throw new OCSNotFoundException('wrong share ID, share does not exist.');
  105. }
  106. /**
  107. * @param array $share Share with info from the share_external table
  108. * @return array enriched share info with data from the filecache
  109. */
  110. private static function extendShareInfo($share) {
  111. $view = new \OC\Files\View('/' . \OC_User::getUser() . '/files/');
  112. $info = $view->getFileInfo($share['mountpoint']);
  113. if ($info === false) {
  114. return $share;
  115. }
  116. $share['mimetype'] = $info->getMimetype();
  117. $share['mtime'] = $info->getMTime();
  118. $share['permissions'] = $info->getPermissions();
  119. $share['type'] = $info->getType();
  120. $share['file_id'] = $info->getId();
  121. return $share;
  122. }
  123. /**
  124. * @NoAdminRequired
  125. *
  126. * Get a list of accepted remote shares
  127. *
  128. * @return DataResponse<Http::STATUS_OK, Files_SharingRemoteShare[], array{}>
  129. *
  130. * 200: Accepted remote shares returned
  131. */
  132. public function getShares() {
  133. $shares = $this->externalManager->getAcceptedShares();
  134. $shares = array_map('self::extendShareInfo', $shares);
  135. return new DataResponse($shares);
  136. }
  137. /**
  138. * @NoAdminRequired
  139. *
  140. * Get info of a remote share
  141. *
  142. * @param int $id ID of the share
  143. * @return DataResponse<Http::STATUS_OK, Files_SharingRemoteShare, array{}>
  144. * @throws OCSNotFoundException Share not found
  145. *
  146. * 200: Share returned
  147. */
  148. public function getShare($id) {
  149. $shareInfo = $this->externalManager->getShare($id);
  150. if ($shareInfo === false) {
  151. throw new OCSNotFoundException('share does not exist');
  152. } else {
  153. $shareInfo = self::extendShareInfo($shareInfo);
  154. return new DataResponse($shareInfo);
  155. }
  156. }
  157. /**
  158. * @NoAdminRequired
  159. *
  160. * Unshare a remote share
  161. *
  162. * @param int $id ID of the share
  163. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  164. * @throws OCSNotFoundException Share not found
  165. * @throws OCSForbiddenException Unsharing is not possible
  166. *
  167. * 200: Share unshared successfully
  168. */
  169. public function unshare($id) {
  170. $shareInfo = $this->externalManager->getShare($id);
  171. if ($shareInfo === false) {
  172. throw new OCSNotFoundException('Share does not exist');
  173. }
  174. $mountPoint = '/' . \OC_User::getUser() . '/files' . $shareInfo['mountpoint'];
  175. if ($this->externalManager->removeShare($mountPoint) === true) {
  176. return new DataResponse();
  177. } else {
  178. throw new OCSForbiddenException('Could not unshare');
  179. }
  180. }
  181. }