You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RequestHandlerController.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\FederatedFileSharing\Controller;
  28. use OCA\FederatedFileSharing\AddressHandler;
  29. use OCA\FederatedFileSharing\FederatedShareProvider;
  30. use OCA\FederatedFileSharing\Notifications;
  31. use OCP\AppFramework\Http;
  32. use OCP\AppFramework\OCS\OCSBadRequestException;
  33. use OCP\AppFramework\OCS\OCSException;
  34. use OCP\AppFramework\OCS\OCSForbiddenException;
  35. use OCP\AppFramework\OCSController;
  36. use OCP\Constants;
  37. use OCP\Federation\Exceptions\ProviderCouldNotAddShareException;
  38. use OCP\Federation\Exceptions\ProviderDoesNotExistsException;
  39. use OCP\Federation\ICloudFederationFactory;
  40. use OCP\Federation\ICloudFederationProviderManager;
  41. use OCP\Federation\ICloudIdManager;
  42. use OCP\IDBConnection;
  43. use OCP\ILogger;
  44. use OCP\IRequest;
  45. use OCP\IUserManager;
  46. use OCP\Share;
  47. use OCP\Share\Exceptions\ShareNotFound;
  48. class RequestHandlerController extends OCSController {
  49. /** @var FederatedShareProvider */
  50. private $federatedShareProvider;
  51. /** @var IDBConnection */
  52. private $connection;
  53. /** @var Share\IManager */
  54. private $shareManager;
  55. /** @var Notifications */
  56. private $notifications;
  57. /** @var AddressHandler */
  58. private $addressHandler;
  59. /** @var IUserManager */
  60. private $userManager;
  61. /** @var string */
  62. private $shareTable = 'share';
  63. /** @var ICloudIdManager */
  64. private $cloudIdManager;
  65. /** @var ILogger */
  66. private $logger;
  67. /** @var ICloudFederationFactory */
  68. private $cloudFederationFactory;
  69. /** @var ICloudFederationProviderManager */
  70. private $cloudFederationProviderManager;
  71. /**
  72. * Server2Server constructor.
  73. *
  74. * @param string $appName
  75. * @param IRequest $request
  76. * @param FederatedShareProvider $federatedShareProvider
  77. * @param IDBConnection $connection
  78. * @param Share\IManager $shareManager
  79. * @param Notifications $notifications
  80. * @param AddressHandler $addressHandler
  81. * @param IUserManager $userManager
  82. * @param ICloudIdManager $cloudIdManager
  83. * @param ILogger $logger
  84. * @param ICloudFederationFactory $cloudFederationFactory
  85. * @param ICloudFederationProviderManager $cloudFederationProviderManager
  86. */
  87. public function __construct($appName,
  88. IRequest $request,
  89. FederatedShareProvider $federatedShareProvider,
  90. IDBConnection $connection,
  91. Share\IManager $shareManager,
  92. Notifications $notifications,
  93. AddressHandler $addressHandler,
  94. IUserManager $userManager,
  95. ICloudIdManager $cloudIdManager,
  96. ILogger $logger,
  97. ICloudFederationFactory $cloudFederationFactory,
  98. ICloudFederationProviderManager $cloudFederationProviderManager
  99. ) {
  100. parent::__construct($appName, $request);
  101. $this->federatedShareProvider = $federatedShareProvider;
  102. $this->connection = $connection;
  103. $this->shareManager = $shareManager;
  104. $this->notifications = $notifications;
  105. $this->addressHandler = $addressHandler;
  106. $this->userManager = $userManager;
  107. $this->cloudIdManager = $cloudIdManager;
  108. $this->logger = $logger;
  109. $this->cloudFederationFactory = $cloudFederationFactory;
  110. $this->cloudFederationProviderManager = $cloudFederationProviderManager;
  111. }
  112. /**
  113. * @NoCSRFRequired
  114. * @PublicPage
  115. *
  116. * create a new share
  117. *
  118. * @return Http\DataResponse
  119. * @throws OCSException
  120. */
  121. public function createShare() {
  122. $remote = isset($_POST['remote']) ? $_POST['remote'] : null;
  123. $token = isset($_POST['token']) ? $_POST['token'] : null;
  124. $name = isset($_POST['name']) ? $_POST['name'] : null;
  125. $owner = isset($_POST['owner']) ? $_POST['owner'] : null;
  126. $sharedBy = isset($_POST['sharedBy']) ? $_POST['sharedBy'] : null;
  127. $shareWith = isset($_POST['shareWith']) ? $_POST['shareWith'] : null;
  128. $remoteId = isset($_POST['remoteId']) ? (int)$_POST['remoteId'] : null;
  129. $sharedByFederatedId = isset($_POST['sharedByFederatedId']) ? $_POST['sharedByFederatedId'] : null;
  130. $ownerFederatedId = isset($_POST['ownerFederatedId']) ? $_POST['ownerFederatedId'] : null;
  131. if ($ownerFederatedId === null) {
  132. $ownerFederatedId = $this->cloudIdManager->getCloudId($owner, $this->cleanupRemote($remote))->getId();
  133. }
  134. // if the owner of the share and the initiator are the same user
  135. // we also complete the federated share ID for the initiator
  136. if ($sharedByFederatedId === null && $owner === $sharedBy) {
  137. $sharedByFederatedId = $ownerFederatedId;
  138. }
  139. $share = $this->cloudFederationFactory->getCloudFederationShare(
  140. $shareWith,
  141. $name,
  142. '',
  143. $remoteId,
  144. $ownerFederatedId,
  145. $owner,
  146. $sharedByFederatedId,
  147. $sharedBy,
  148. $token,
  149. 'user',
  150. 'file'
  151. );
  152. try {
  153. $provider = $this->cloudFederationProviderManager->getCloudFederationProvider('file');
  154. $provider->shareReceived($share);
  155. } catch (ProviderDoesNotExistsException $e) {
  156. throw new OCSException('Server does not support federated cloud sharing', 503);
  157. } catch (ProviderCouldNotAddShareException $e) {
  158. throw new OCSException($e->getMessage(), 400);
  159. } catch (\Exception $e) {
  160. throw new OCSException('internal server error, was not able to add share from ' . $remote, 500);
  161. }
  162. return new Http\DataResponse();
  163. }
  164. /**
  165. * @NoCSRFRequired
  166. * @PublicPage
  167. *
  168. * create re-share on behalf of another user
  169. *
  170. * @param int $id
  171. * @return Http\DataResponse
  172. * @throws OCSBadRequestException
  173. * @throws OCSException
  174. * @throws OCSForbiddenException
  175. */
  176. public function reShare($id) {
  177. $token = $this->request->getParam('token', null);
  178. $shareWith = $this->request->getParam('shareWith', null);
  179. $permission = (int)$this->request->getParam('permission', null);
  180. $remoteId = (int)$this->request->getParam('remoteId', null);
  181. if ($id === null ||
  182. $token === null ||
  183. $shareWith === null ||
  184. $permission === null ||
  185. $remoteId === null
  186. ) {
  187. throw new OCSBadRequestException();
  188. }
  189. $notification = [
  190. 'sharedSecret' => $token,
  191. 'shareWith' => $shareWith,
  192. 'senderId' => $remoteId,
  193. 'message' => 'Recipient of a share ask the owner to reshare the file'
  194. ];
  195. try {
  196. $provider = $this->cloudFederationProviderManager->getCloudFederationProvider('file');
  197. [$newToken, $localId] = $provider->notificationReceived('REQUEST_RESHARE', $id, $notification);
  198. return new Http\DataResponse([
  199. 'token' => $newToken,
  200. 'remoteId' => $localId
  201. ]);
  202. } catch (ProviderDoesNotExistsException $e) {
  203. throw new OCSException('Server does not support federated cloud sharing', 503);
  204. } catch (ShareNotFound $e) {
  205. $this->logger->debug('Share not found: ' . $e->getMessage());
  206. } catch (\Exception $e) {
  207. $this->logger->debug('internal server error, can not process notification: ' . $e->getMessage());
  208. }
  209. throw new OCSBadRequestException();
  210. }
  211. /**
  212. * @NoCSRFRequired
  213. * @PublicPage
  214. *
  215. * accept server-to-server share
  216. *
  217. * @param int $id
  218. * @return Http\DataResponse
  219. * @throws OCSException
  220. * @throws ShareNotFound
  221. * @throws \OC\HintException
  222. */
  223. public function acceptShare($id) {
  224. $token = isset($_POST['token']) ? $_POST['token'] : null;
  225. $notification = [
  226. 'sharedSecret' => $token,
  227. 'message' => 'Recipient accept the share'
  228. ];
  229. try {
  230. $provider = $this->cloudFederationProviderManager->getCloudFederationProvider('file');
  231. $provider->notificationReceived('SHARE_ACCEPTED', $id, $notification);
  232. } catch (ProviderDoesNotExistsException $e) {
  233. throw new OCSException('Server does not support federated cloud sharing', 503);
  234. } catch (ShareNotFound $e) {
  235. $this->logger->debug('Share not found: ' . $e->getMessage());
  236. } catch (\Exception $e) {
  237. $this->logger->debug('internal server error, can not process notification: ' . $e->getMessage());
  238. }
  239. return new Http\DataResponse();
  240. }
  241. /**
  242. * @NoCSRFRequired
  243. * @PublicPage
  244. *
  245. * decline server-to-server share
  246. *
  247. * @param int $id
  248. * @return Http\DataResponse
  249. * @throws OCSException
  250. */
  251. public function declineShare($id) {
  252. $token = isset($_POST['token']) ? $_POST['token'] : null;
  253. $notification = [
  254. 'sharedSecret' => $token,
  255. 'message' => 'Recipient declined the share'
  256. ];
  257. try {
  258. $provider = $this->cloudFederationProviderManager->getCloudFederationProvider('file');
  259. $provider->notificationReceived('SHARE_DECLINED', $id, $notification);
  260. } catch (ProviderDoesNotExistsException $e) {
  261. throw new OCSException('Server does not support federated cloud sharing', 503);
  262. } catch (ShareNotFound $e) {
  263. $this->logger->debug('Share not found: ' . $e->getMessage());
  264. } catch (\Exception $e) {
  265. $this->logger->debug('internal server error, can not process notification: ' . $e->getMessage());
  266. }
  267. return new Http\DataResponse();
  268. }
  269. /**
  270. * @NoCSRFRequired
  271. * @PublicPage
  272. *
  273. * remove server-to-server share if it was unshared by the owner
  274. *
  275. * @param int $id
  276. * @return Http\DataResponse
  277. * @throws OCSException
  278. */
  279. public function unshare($id) {
  280. if (!$this->isS2SEnabled()) {
  281. throw new OCSException('Server does not support federated cloud sharing', 503);
  282. }
  283. $token = isset($_POST['token']) ? $_POST['token'] : null;
  284. try {
  285. $provider = $this->cloudFederationProviderManager->getCloudFederationProvider('file');
  286. $notification = ['sharedSecret' => $token];
  287. $provider->notificationReceived('SHARE_UNSHARED', $id, $notification);
  288. } catch (\Exception $e) {
  289. $this->logger->debug('processing unshare notification failed: ' . $e->getMessage());
  290. }
  291. return new Http\DataResponse();
  292. }
  293. private function cleanupRemote($remote) {
  294. $remote = substr($remote, strpos($remote, '://') + 3);
  295. return rtrim($remote, '/');
  296. }
  297. /**
  298. * @NoCSRFRequired
  299. * @PublicPage
  300. *
  301. * federated share was revoked, either by the owner or the re-sharer
  302. *
  303. * @param int $id
  304. * @return Http\DataResponse
  305. * @throws OCSBadRequestException
  306. */
  307. public function revoke($id) {
  308. $token = $this->request->getParam('token');
  309. try {
  310. $provider = $this->cloudFederationProviderManager->getCloudFederationProvider('file');
  311. $notification = ['sharedSecret' => $token];
  312. $provider->notificationReceived('RESHARE_UNDO', $id, $notification);
  313. return new Http\DataResponse();
  314. } catch (\Exception $e) {
  315. throw new OCSBadRequestException();
  316. }
  317. }
  318. /**
  319. * check if server-to-server sharing is enabled
  320. *
  321. * @param bool $incoming
  322. * @return bool
  323. */
  324. private function isS2SEnabled($incoming = false) {
  325. $result = \OCP\App::isEnabled('files_sharing');
  326. if ($incoming) {
  327. $result = $result && $this->federatedShareProvider->isIncomingServer2serverShareEnabled();
  328. } else {
  329. $result = $result && $this->federatedShareProvider->isOutgoingServer2serverShareEnabled();
  330. }
  331. return $result;
  332. }
  333. /**
  334. * @NoCSRFRequired
  335. * @PublicPage
  336. *
  337. * update share information to keep federated re-shares in sync
  338. *
  339. * @param int $id
  340. * @return Http\DataResponse
  341. * @throws OCSBadRequestException
  342. */
  343. public function updatePermissions($id) {
  344. $token = $this->request->getParam('token', null);
  345. $ncPermissions = $this->request->getParam('permissions', null);
  346. try {
  347. $provider = $this->cloudFederationProviderManager->getCloudFederationProvider('file');
  348. $ocmPermissions = $this->ncPermissions2ocmPermissions((int)$ncPermissions);
  349. $notification = ['sharedSecret' => $token, 'permission' => $ocmPermissions];
  350. $provider->notificationReceived('RESHARE_CHANGE_PERMISSION', $id, $notification);
  351. } catch (\Exception $e) {
  352. $this->logger->debug($e->getMessage());
  353. throw new OCSBadRequestException();
  354. }
  355. return new Http\DataResponse();
  356. }
  357. /**
  358. * translate Nextcloud permissions to OCM Permissions
  359. *
  360. * @param $ncPermissions
  361. * @return array
  362. */
  363. protected function ncPermissions2ocmPermissions($ncPermissions) {
  364. $ocmPermissions = [];
  365. if ($ncPermissions & Constants::PERMISSION_SHARE) {
  366. $ocmPermissions[] = 'share';
  367. }
  368. if ($ncPermissions & Constants::PERMISSION_READ) {
  369. $ocmPermissions[] = 'read';
  370. }
  371. if (($ncPermissions & Constants::PERMISSION_CREATE) ||
  372. ($ncPermissions & Constants::PERMISSION_UPDATE)) {
  373. $ocmPermissions[] = 'write';
  374. }
  375. return $ocmPermissions;
  376. }
  377. /**
  378. * @NoCSRFRequired
  379. * @PublicPage
  380. *
  381. * change the owner of a server-to-server share
  382. *
  383. * @param int $id
  384. * @return Http\DataResponse
  385. * @throws \InvalidArgumentException
  386. * @throws OCSException
  387. */
  388. public function move($id) {
  389. if (!$this->isS2SEnabled()) {
  390. throw new OCSException('Server does not support federated cloud sharing', 503);
  391. }
  392. $token = $this->request->getParam('token');
  393. $remote = $this->request->getParam('remote');
  394. $newRemoteId = $this->request->getParam('remote_id', $id);
  395. $cloudId = $this->cloudIdManager->resolveCloudId($remote);
  396. $qb = $this->connection->getQueryBuilder();
  397. $query = $qb->update('share_external')
  398. ->set('remote', $qb->createNamedParameter($cloudId->getRemote()))
  399. ->set('owner', $qb->createNamedParameter($cloudId->getUser()))
  400. ->set('remote_id', $qb->createNamedParameter($newRemoteId))
  401. ->where($qb->expr()->eq('remote_id', $qb->createNamedParameter($id)))
  402. ->andWhere($qb->expr()->eq('share_token', $qb->createNamedParameter($token)));
  403. $affected = $query->execute();
  404. if ($affected > 0) {
  405. return new Http\DataResponse(['remote' => $cloudId->getRemote(), 'owner' => $cloudId->getUser()]);
  406. } else {
  407. throw new OCSBadRequestException('Share not found or token invalid');
  408. }
  409. }
  410. }