1
0

RemoteController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  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, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Files_Sharing\Controller;
  25. use OCA\Files_Sharing\External\Manager;
  26. use OCP\AppFramework\Http\DataResponse;
  27. use OCP\AppFramework\OCS\OCSForbiddenException;
  28. use OCP\AppFramework\OCS\OCSNotFoundException;
  29. use OCP\AppFramework\OCSController;
  30. use OCP\ILogger;
  31. use OCP\IRequest;
  32. class RemoteController extends OCSController {
  33. /** @var Manager */
  34. private $externalManager;
  35. /** @var ILogger */
  36. private $logger;
  37. /**
  38. * @NoAdminRequired
  39. *
  40. * Remote constructor.
  41. *
  42. * @param string $appName
  43. * @param IRequest $request
  44. * @param Manager $externalManager
  45. */
  46. public function __construct($appName,
  47. IRequest $request,
  48. Manager $externalManager,
  49. ILogger $logger) {
  50. parent::__construct($appName, $request);
  51. $this->externalManager = $externalManager;
  52. $this->logger = $logger;
  53. }
  54. /**
  55. * @NoAdminRequired
  56. *
  57. * Get list of pending remote shares
  58. *
  59. * @return DataResponse
  60. */
  61. public function getOpenShares() {
  62. return new DataResponse($this->externalManager->getOpenShares());
  63. }
  64. /**
  65. * @NoAdminRequired
  66. *
  67. * Accept a remote share
  68. *
  69. * @param int $id
  70. * @return DataResponse
  71. * @throws OCSNotFoundException
  72. */
  73. public function acceptShare($id) {
  74. if ($this->externalManager->acceptShare($id)) {
  75. return new DataResponse();
  76. }
  77. $this->logger->error('Could not accept federated share with id: ' . $id,
  78. ['app' => 'files_sharing']);
  79. throw new OCSNotFoundException('wrong share ID, share does not exist.');
  80. }
  81. /**
  82. * @NoAdminRequired
  83. *
  84. * Decline a remote share
  85. *
  86. * @param int $id
  87. * @return DataResponse
  88. * @throws OCSNotFoundException
  89. */
  90. public function declineShare($id) {
  91. if ($this->externalManager->declineShare($id)) {
  92. return new DataResponse();
  93. }
  94. // Make sure the user has no notification for something that does not exist anymore.
  95. $this->externalManager->processNotification($id);
  96. throw new OCSNotFoundException('wrong share ID, share does not exist.');
  97. }
  98. /**
  99. * @param array $share Share with info from the share_external table
  100. * @return array enriched share info with data from the filecache
  101. */
  102. private static function extendShareInfo($share) {
  103. $view = new \OC\Files\View('/' . \OC_User::getUser() . '/files/');
  104. $info = $view->getFileInfo($share['mountpoint']);
  105. if ($info === false) {
  106. return $share;
  107. }
  108. $share['mimetype'] = $info->getMimetype();
  109. $share['mtime'] = $info->getMTime();
  110. $share['permissions'] = $info->getPermissions();
  111. $share['type'] = $info->getType();
  112. $share['file_id'] = $info->getId();
  113. return $share;
  114. }
  115. /**
  116. * @NoAdminRequired
  117. *
  118. * List accepted remote shares
  119. *
  120. * @return DataResponse
  121. */
  122. public function getShares() {
  123. $shares = $this->externalManager->getAcceptedShares();
  124. $shares = array_map('self::extendShareInfo', $shares);
  125. return new DataResponse($shares);
  126. }
  127. /**
  128. * @NoAdminRequired
  129. *
  130. * Get info of a remote share
  131. *
  132. * @param int $id
  133. * @return DataResponse
  134. * @throws OCSNotFoundException
  135. */
  136. public function getShare($id) {
  137. $shareInfo = $this->externalManager->getShare($id);
  138. if ($shareInfo === false) {
  139. throw new OCSNotFoundException('share does not exist');
  140. } else {
  141. $shareInfo = self::extendShareInfo($shareInfo);
  142. return new DataResponse($shareInfo);
  143. }
  144. }
  145. /**
  146. * @NoAdminRequired
  147. *
  148. * Unshare a remote share
  149. *
  150. * @param int $id
  151. * @return DataResponse
  152. * @throws OCSNotFoundException
  153. * @throws OCSForbiddenException
  154. */
  155. public function unshare($id) {
  156. $shareInfo = $this->externalManager->getShare($id);
  157. if ($shareInfo === false) {
  158. throw new OCSNotFoundException('Share does not exist');
  159. }
  160. $mountPoint = '/' . \OC_User::getUser() . '/files' . $shareInfo['mountpoint'];
  161. if ($this->externalManager->removeShare($mountPoint) === true) {
  162. return new DataResponse();
  163. } else {
  164. throw new OCSForbiddenException('Could not unshare');
  165. }
  166. }
  167. }