RequestHandlerController.php 14 KB

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