RequestHandlerController.php 15 KB

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