FederatedShareProvider.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\FederatedFileSharing;
  8. use OC\Share20\Exception\InvalidShare;
  9. use OC\Share20\Share;
  10. use OCP\Constants;
  11. use OCP\DB\QueryBuilder\IQueryBuilder;
  12. use OCP\Federation\ICloudFederationProviderManager;
  13. use OCP\Federation\ICloudIdManager;
  14. use OCP\Files\Folder;
  15. use OCP\Files\IRootFolder;
  16. use OCP\Files\Node;
  17. use OCP\Files\NotFoundException;
  18. use OCP\IConfig;
  19. use OCP\IDBConnection;
  20. use OCP\IL10N;
  21. use OCP\IUserManager;
  22. use OCP\Share\Exceptions\GenericShareException;
  23. use OCP\Share\Exceptions\ShareNotFound;
  24. use OCP\Share\IShare;
  25. use OCP\Share\IShareProvider;
  26. use Psr\Log\LoggerInterface;
  27. /**
  28. * Class FederatedShareProvider
  29. *
  30. * @package OCA\FederatedFileSharing
  31. */
  32. class FederatedShareProvider implements IShareProvider {
  33. public const SHARE_TYPE_REMOTE = 6;
  34. /** @var string */
  35. private $externalShareTable = 'share_external';
  36. /** @var array list of supported share types */
  37. private $supportedShareType = [IShare::TYPE_REMOTE_GROUP, IShare::TYPE_REMOTE, IShare::TYPE_CIRCLE];
  38. /**
  39. * DefaultShareProvider constructor.
  40. */
  41. public function __construct(
  42. private IDBConnection $dbConnection,
  43. private AddressHandler $addressHandler,
  44. private Notifications $notifications,
  45. private TokenHandler $tokenHandler,
  46. private IL10N $l,
  47. private IRootFolder $rootFolder,
  48. private IConfig $config,
  49. private IUserManager $userManager,
  50. private ICloudIdManager $cloudIdManager,
  51. private \OCP\GlobalScale\IConfig $gsConfig,
  52. private ICloudFederationProviderManager $cloudFederationProviderManager,
  53. private LoggerInterface $logger,
  54. ) {
  55. }
  56. /**
  57. * Return the identifier of this provider.
  58. *
  59. * @return string Containing only [a-zA-Z0-9]
  60. */
  61. public function identifier() {
  62. return 'ocFederatedSharing';
  63. }
  64. /**
  65. * Share a path
  66. *
  67. * @param IShare $share
  68. * @return IShare The share object
  69. * @throws ShareNotFound
  70. * @throws \Exception
  71. */
  72. public function create(IShare $share) {
  73. $shareWith = $share->getSharedWith();
  74. $itemSource = $share->getNodeId();
  75. $itemType = $share->getNodeType();
  76. $permissions = $share->getPermissions();
  77. $sharedBy = $share->getSharedBy();
  78. $shareType = $share->getShareType();
  79. $expirationDate = $share->getExpirationDate();
  80. if ($shareType === IShare::TYPE_REMOTE_GROUP &&
  81. !$this->isOutgoingServer2serverGroupShareEnabled()
  82. ) {
  83. $message = 'It is not allowed to send federated group shares from this server.';
  84. $message_t = $this->l->t('It is not allowed to send federated group shares from this server.');
  85. $this->logger->debug($message, ['app' => 'Federated File Sharing']);
  86. throw new \Exception($message_t);
  87. }
  88. /*
  89. * Check if file is not already shared with the remote user
  90. */
  91. $alreadyShared = $this->getSharedWith($shareWith, IShare::TYPE_REMOTE, $share->getNode(), 1, 0);
  92. $alreadySharedGroup = $this->getSharedWith($shareWith, IShare::TYPE_REMOTE_GROUP, $share->getNode(), 1, 0);
  93. if (!empty($alreadyShared) || !empty($alreadySharedGroup)) {
  94. $message = 'Sharing %1$s failed, because this item is already shared with %2$s';
  95. $message_t = $this->l->t('Sharing %1$s failed, because this item is already shared with the account %2$s', [$share->getNode()->getName(), $shareWith]);
  96. $this->logger->debug(sprintf($message, $share->getNode()->getName(), $shareWith), ['app' => 'Federated File Sharing']);
  97. throw new \Exception($message_t);
  98. }
  99. // don't allow federated shares if source and target server are the same
  100. $cloudId = $this->cloudIdManager->resolveCloudId($shareWith);
  101. $currentServer = $this->addressHandler->generateRemoteURL();
  102. $currentUser = $sharedBy;
  103. if ($this->addressHandler->compareAddresses($cloudId->getUser(), $cloudId->getRemote(), $currentUser, $currentServer)) {
  104. $message = 'Not allowed to create a federated share to the same account.';
  105. $message_t = $this->l->t('Not allowed to create a federated share to the same account');
  106. $this->logger->debug($message, ['app' => 'Federated File Sharing']);
  107. throw new \Exception($message_t);
  108. }
  109. // Federated shares always have read permissions
  110. if (($share->getPermissions() & Constants::PERMISSION_READ) === 0) {
  111. $message = 'Federated shares require read permissions';
  112. $message_t = $this->l->t('Federated shares require read permissions');
  113. $this->logger->debug($message, ['app' => 'Federated File Sharing']);
  114. throw new \Exception($message_t);
  115. }
  116. $share->setSharedWith($cloudId->getId());
  117. try {
  118. $remoteShare = $this->getShareFromExternalShareTable($share);
  119. } catch (ShareNotFound $e) {
  120. $remoteShare = null;
  121. }
  122. if ($remoteShare) {
  123. try {
  124. $ownerCloudId = $this->cloudIdManager->getCloudId($remoteShare['owner'], $remoteShare['remote']);
  125. $shareId = $this->addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $ownerCloudId->getId(), $permissions, 'tmp_token_' . time(), $shareType, $expirationDate);
  126. $share->setId($shareId);
  127. [$token, $remoteId] = $this->askOwnerToReShare($shareWith, $share, $shareId);
  128. // remote share was create successfully if we get a valid token as return
  129. $send = is_string($token) && $token !== '';
  130. } catch (\Exception $e) {
  131. // fall back to old re-share behavior if the remote server
  132. // doesn't support flat re-shares (was introduced with Nextcloud 9.1)
  133. $this->removeShareFromTable($share);
  134. $shareId = $this->createFederatedShare($share);
  135. }
  136. if ($send) {
  137. $this->updateSuccessfulReshare($shareId, $token);
  138. $this->storeRemoteId($shareId, $remoteId);
  139. } else {
  140. $this->removeShareFromTable($share);
  141. $message_t = $this->l->t('File is already shared with %s', [$shareWith]);
  142. throw new \Exception($message_t);
  143. }
  144. } else {
  145. $shareId = $this->createFederatedShare($share);
  146. }
  147. $data = $this->getRawShare($shareId);
  148. return $this->createShareObject($data);
  149. }
  150. /**
  151. * create federated share and inform the recipient
  152. *
  153. * @param IShare $share
  154. * @return int
  155. * @throws ShareNotFound
  156. * @throws \Exception
  157. */
  158. protected function createFederatedShare(IShare $share) {
  159. $token = $this->tokenHandler->generateToken();
  160. $shareId = $this->addShareToDB(
  161. $share->getNodeId(),
  162. $share->getNodeType(),
  163. $share->getSharedWith(),
  164. $share->getSharedBy(),
  165. $share->getShareOwner(),
  166. $share->getPermissions(),
  167. $token,
  168. $share->getShareType(),
  169. $share->getExpirationDate()
  170. );
  171. $failure = false;
  172. try {
  173. $sharedByFederatedId = $share->getSharedBy();
  174. if ($this->userManager->userExists($sharedByFederatedId)) {
  175. $cloudId = $this->cloudIdManager->getCloudId($sharedByFederatedId, $this->addressHandler->generateRemoteURL());
  176. $sharedByFederatedId = $cloudId->getId();
  177. }
  178. $ownerCloudId = $this->cloudIdManager->getCloudId($share->getShareOwner(), $this->addressHandler->generateRemoteURL());
  179. $send = $this->notifications->sendRemoteShare(
  180. $token,
  181. $share->getSharedWith(),
  182. $share->getNode()->getName(),
  183. $shareId,
  184. $share->getShareOwner(),
  185. $ownerCloudId->getId(),
  186. $share->getSharedBy(),
  187. $sharedByFederatedId,
  188. $share->getShareType()
  189. );
  190. if ($send === false) {
  191. $failure = true;
  192. }
  193. } catch (\Exception $e) {
  194. $this->logger->error('Failed to notify remote server of federated share, removing share.', [
  195. 'app' => 'federatedfilesharing',
  196. 'exception' => $e,
  197. ]);
  198. $failure = true;
  199. }
  200. if ($failure) {
  201. $this->removeShareFromTableById($shareId);
  202. $message_t = $this->l->t('Sharing %1$s failed, could not find %2$s, maybe the server is currently unreachable or uses a self-signed certificate.',
  203. [$share->getNode()->getName(), $share->getSharedWith()]);
  204. throw new \Exception($message_t);
  205. }
  206. return $shareId;
  207. }
  208. /**
  209. * @param string $shareWith
  210. * @param IShare $share
  211. * @param string $shareId internal share Id
  212. * @return array
  213. * @throws \Exception
  214. */
  215. protected function askOwnerToReShare($shareWith, IShare $share, $shareId) {
  216. $remoteShare = $this->getShareFromExternalShareTable($share);
  217. $token = $remoteShare['share_token'];
  218. $remoteId = $remoteShare['remote_id'];
  219. $remote = $remoteShare['remote'];
  220. [$token, $remoteId] = $this->notifications->requestReShare(
  221. $token,
  222. $remoteId,
  223. $shareId,
  224. $remote,
  225. $shareWith,
  226. $share->getPermissions(),
  227. $share->getNode()->getName()
  228. );
  229. return [$token, $remoteId];
  230. }
  231. /**
  232. * get federated share from the share_external table but exclude mounted link shares
  233. *
  234. * @param IShare $share
  235. * @return array
  236. * @throws ShareNotFound
  237. */
  238. protected function getShareFromExternalShareTable(IShare $share) {
  239. $query = $this->dbConnection->getQueryBuilder();
  240. $query->select('*')->from($this->externalShareTable)
  241. ->where($query->expr()->eq('user', $query->createNamedParameter($share->getShareOwner())))
  242. ->andWhere($query->expr()->eq('mountpoint', $query->createNamedParameter($share->getTarget())));
  243. $qResult = $query->execute();
  244. $result = $qResult->fetchAll();
  245. $qResult->closeCursor();
  246. if (isset($result[0]) && (int)$result[0]['remote_id'] > 0) {
  247. return $result[0];
  248. }
  249. throw new ShareNotFound('share not found in share_external table');
  250. }
  251. /**
  252. * add share to the database and return the ID
  253. *
  254. * @param int $itemSource
  255. * @param string $itemType
  256. * @param string $shareWith
  257. * @param string $sharedBy
  258. * @param string $uidOwner
  259. * @param int $permissions
  260. * @param string $token
  261. * @param int $shareType
  262. * @param \DateTime $expirationDate
  263. * @return int
  264. */
  265. private function addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token, $shareType, $expirationDate) {
  266. $qb = $this->dbConnection->getQueryBuilder();
  267. $qb->insert('share')
  268. ->setValue('share_type', $qb->createNamedParameter($shareType))
  269. ->setValue('item_type', $qb->createNamedParameter($itemType))
  270. ->setValue('item_source', $qb->createNamedParameter($itemSource))
  271. ->setValue('file_source', $qb->createNamedParameter($itemSource))
  272. ->setValue('share_with', $qb->createNamedParameter($shareWith))
  273. ->setValue('uid_owner', $qb->createNamedParameter($uidOwner))
  274. ->setValue('uid_initiator', $qb->createNamedParameter($sharedBy))
  275. ->setValue('permissions', $qb->createNamedParameter($permissions))
  276. ->setValue('expiration', $qb->createNamedParameter($expirationDate, IQueryBuilder::PARAM_DATE))
  277. ->setValue('token', $qb->createNamedParameter($token))
  278. ->setValue('stime', $qb->createNamedParameter(time()));
  279. /*
  280. * Added to fix https://github.com/owncloud/core/issues/22215
  281. * Can be removed once we get rid of ajax/share.php
  282. */
  283. $qb->setValue('file_target', $qb->createNamedParameter(''));
  284. $qb->execute();
  285. return $qb->getLastInsertId();
  286. }
  287. /**
  288. * Update a share
  289. *
  290. * @param IShare $share
  291. * @return IShare The share object
  292. */
  293. public function update(IShare $share) {
  294. /*
  295. * We allow updating the permissions of federated shares
  296. */
  297. $qb = $this->dbConnection->getQueryBuilder();
  298. $qb->update('share')
  299. ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
  300. ->set('permissions', $qb->createNamedParameter($share->getPermissions()))
  301. ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
  302. ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
  303. ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
  304. ->execute();
  305. // send the updated permission to the owner/initiator, if they are not the same
  306. if ($share->getShareOwner() !== $share->getSharedBy()) {
  307. $this->sendPermissionUpdate($share);
  308. }
  309. return $share;
  310. }
  311. /**
  312. * send the updated permission to the owner/initiator, if they are not the same
  313. *
  314. * @param IShare $share
  315. * @throws ShareNotFound
  316. * @throws \OCP\HintException
  317. */
  318. protected function sendPermissionUpdate(IShare $share) {
  319. $remoteId = $this->getRemoteId($share);
  320. // if the local user is the owner we send the permission change to the initiator
  321. if ($this->userManager->userExists($share->getShareOwner())) {
  322. [, $remote] = $this->addressHandler->splitUserRemote($share->getSharedBy());
  323. } else { // ... if not we send the permission change to the owner
  324. [, $remote] = $this->addressHandler->splitUserRemote($share->getShareOwner());
  325. }
  326. $this->notifications->sendPermissionChange($remote, $remoteId, $share->getToken(), $share->getPermissions());
  327. }
  328. /**
  329. * update successful reShare with the correct token
  330. *
  331. * @param int $shareId
  332. * @param string $token
  333. */
  334. protected function updateSuccessfulReShare($shareId, $token) {
  335. $query = $this->dbConnection->getQueryBuilder();
  336. $query->update('share')
  337. ->where($query->expr()->eq('id', $query->createNamedParameter($shareId)))
  338. ->set('token', $query->createNamedParameter($token))
  339. ->execute();
  340. }
  341. /**
  342. * store remote ID in federated reShare table
  343. *
  344. * @param $shareId
  345. * @param $remoteId
  346. */
  347. public function storeRemoteId(int $shareId, string $remoteId): void {
  348. $query = $this->dbConnection->getQueryBuilder();
  349. $query->insert('federated_reshares')
  350. ->values(
  351. [
  352. 'share_id' => $query->createNamedParameter($shareId),
  353. 'remote_id' => $query->createNamedParameter($remoteId),
  354. ]
  355. );
  356. $query->execute();
  357. }
  358. /**
  359. * get share ID on remote server for federated re-shares
  360. *
  361. * @param IShare $share
  362. * @return string
  363. * @throws ShareNotFound
  364. */
  365. public function getRemoteId(IShare $share): string {
  366. $query = $this->dbConnection->getQueryBuilder();
  367. $query->select('remote_id')->from('federated_reshares')
  368. ->where($query->expr()->eq('share_id', $query->createNamedParameter((int)$share->getId())));
  369. $result = $query->execute();
  370. $data = $result->fetch();
  371. $result->closeCursor();
  372. if (!is_array($data) || !isset($data['remote_id'])) {
  373. throw new ShareNotFound();
  374. }
  375. return (string)$data['remote_id'];
  376. }
  377. /**
  378. * @inheritdoc
  379. */
  380. public function move(IShare $share, $recipient) {
  381. /*
  382. * This function does nothing yet as it is just for outgoing
  383. * federated shares.
  384. */
  385. return $share;
  386. }
  387. /**
  388. * Get all children of this share
  389. *
  390. * @param IShare $parent
  391. * @return IShare[]
  392. */
  393. public function getChildren(IShare $parent) {
  394. $children = [];
  395. $qb = $this->dbConnection->getQueryBuilder();
  396. $qb->select('*')
  397. ->from('share')
  398. ->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId())))
  399. ->andWhere($qb->expr()->in('share_type', $qb->createNamedParameter($this->supportedShareType, IQueryBuilder::PARAM_INT_ARRAY)))
  400. ->orderBy('id');
  401. $cursor = $qb->execute();
  402. while ($data = $cursor->fetch()) {
  403. $children[] = $this->createShareObject($data);
  404. }
  405. $cursor->closeCursor();
  406. return $children;
  407. }
  408. /**
  409. * Delete a share (owner unShares the file)
  410. *
  411. * @param IShare $share
  412. * @throws ShareNotFound
  413. * @throws \OCP\HintException
  414. */
  415. public function delete(IShare $share) {
  416. [, $remote] = $this->addressHandler->splitUserRemote($share->getSharedWith());
  417. // if the local user is the owner we can send the unShare request directly...
  418. if ($this->userManager->userExists($share->getShareOwner())) {
  419. $this->notifications->sendRemoteUnShare($remote, $share->getId(), $share->getToken());
  420. $this->revokeShare($share, true);
  421. } else { // ... if not we need to correct ID for the unShare request
  422. $remoteId = $this->getRemoteId($share);
  423. $this->notifications->sendRemoteUnShare($remote, $remoteId, $share->getToken());
  424. $this->revokeShare($share, false);
  425. }
  426. // only remove the share when all messages are send to not lose information
  427. // about the share to early
  428. $this->removeShareFromTable($share);
  429. }
  430. /**
  431. * in case of a re-share we need to send the other use (initiator or owner)
  432. * a message that the file was unshared
  433. *
  434. * @param IShare $share
  435. * @param bool $isOwner the user can either be the owner or the user who re-sahred it
  436. * @throws ShareNotFound
  437. * @throws \OCP\HintException
  438. */
  439. protected function revokeShare($share, $isOwner) {
  440. if ($this->userManager->userExists($share->getShareOwner()) && $this->userManager->userExists($share->getSharedBy())) {
  441. // If both the owner and the initiator of the share are local users we don't have to notify anybody else
  442. return;
  443. }
  444. // also send a unShare request to the initiator, if this is a different user than the owner
  445. if ($share->getShareOwner() !== $share->getSharedBy()) {
  446. if ($isOwner) {
  447. [, $remote] = $this->addressHandler->splitUserRemote($share->getSharedBy());
  448. } else {
  449. [, $remote] = $this->addressHandler->splitUserRemote($share->getShareOwner());
  450. }
  451. $remoteId = $this->getRemoteId($share);
  452. $this->notifications->sendRevokeShare($remote, $remoteId, $share->getToken());
  453. }
  454. }
  455. /**
  456. * remove share from table
  457. *
  458. * @param IShare $share
  459. */
  460. public function removeShareFromTable(IShare $share) {
  461. $this->removeShareFromTableById($share->getId());
  462. }
  463. /**
  464. * remove share from table
  465. *
  466. * @param string $shareId
  467. */
  468. private function removeShareFromTableById($shareId) {
  469. $qb = $this->dbConnection->getQueryBuilder();
  470. $qb->delete('share')
  471. ->where($qb->expr()->eq('id', $qb->createNamedParameter($shareId)))
  472. ->andWhere($qb->expr()->neq('share_type', $qb->createNamedParameter(IShare::TYPE_CIRCLE)));
  473. $qb->execute();
  474. $qb = $this->dbConnection->getQueryBuilder();
  475. $qb->delete('federated_reshares')
  476. ->where($qb->expr()->eq('share_id', $qb->createNamedParameter($shareId)));
  477. $qb->execute();
  478. }
  479. /**
  480. * @inheritdoc
  481. */
  482. public function deleteFromSelf(IShare $share, $recipient) {
  483. // nothing to do here. Technically deleteFromSelf in the context of federated
  484. // shares is a umount of an external storage. This is handled here
  485. // apps/files_sharing/lib/external/manager.php
  486. // TODO move this code over to this app
  487. }
  488. public function restore(IShare $share, string $recipient): IShare {
  489. throw new GenericShareException('not implemented');
  490. }
  491. public function getSharesInFolder($userId, Folder $node, $reshares, $shallow = true) {
  492. if (!$shallow) {
  493. throw new \Exception('non-shallow getSharesInFolder is no longer supported');
  494. }
  495. $qb = $this->dbConnection->getQueryBuilder();
  496. $qb->select('*')
  497. ->from('share', 's')
  498. ->andWhere($qb->expr()->orX(
  499. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  500. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  501. ))
  502. ->andWhere(
  503. $qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_REMOTE))
  504. );
  505. /**
  506. * Reshares for this user are shares where they are the owner.
  507. */
  508. if ($reshares === false) {
  509. $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
  510. } else {
  511. $qb->andWhere(
  512. $qb->expr()->orX(
  513. $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
  514. $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
  515. )
  516. );
  517. }
  518. $qb->innerJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
  519. $qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));
  520. $qb->orderBy('id');
  521. $cursor = $qb->execute();
  522. $shares = [];
  523. while ($data = $cursor->fetch()) {
  524. $shares[$data['fileid']][] = $this->createShareObject($data);
  525. }
  526. $cursor->closeCursor();
  527. return $shares;
  528. }
  529. /**
  530. * @inheritdoc
  531. */
  532. public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) {
  533. $qb = $this->dbConnection->getQueryBuilder();
  534. $qb->select('*')
  535. ->from('share');
  536. $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter($shareType)));
  537. /**
  538. * Reshares for this user are shares where they are the owner.
  539. */
  540. if ($reshares === false) {
  541. //Special case for old shares created via the web UI
  542. $or1 = $qb->expr()->andX(
  543. $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
  544. $qb->expr()->isNull('uid_initiator')
  545. );
  546. $qb->andWhere(
  547. $qb->expr()->orX(
  548. $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)),
  549. $or1
  550. )
  551. );
  552. } else {
  553. $qb->andWhere(
  554. $qb->expr()->orX(
  555. $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
  556. $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
  557. )
  558. );
  559. }
  560. if ($node !== null) {
  561. $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
  562. }
  563. if ($limit !== -1) {
  564. $qb->setMaxResults($limit);
  565. }
  566. $qb->setFirstResult($offset);
  567. $qb->orderBy('id');
  568. $cursor = $qb->execute();
  569. $shares = [];
  570. while ($data = $cursor->fetch()) {
  571. $shares[] = $this->createShareObject($data);
  572. }
  573. $cursor->closeCursor();
  574. return $shares;
  575. }
  576. /**
  577. * @inheritdoc
  578. */
  579. public function getShareById($id, $recipientId = null) {
  580. $qb = $this->dbConnection->getQueryBuilder();
  581. $qb->select('*')
  582. ->from('share')
  583. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
  584. ->andWhere($qb->expr()->in('share_type', $qb->createNamedParameter($this->supportedShareType, IQueryBuilder::PARAM_INT_ARRAY)));
  585. $cursor = $qb->execute();
  586. $data = $cursor->fetch();
  587. $cursor->closeCursor();
  588. if ($data === false) {
  589. throw new ShareNotFound('Can not find share with ID: ' . $id);
  590. }
  591. try {
  592. $share = $this->createShareObject($data);
  593. } catch (InvalidShare $e) {
  594. throw new ShareNotFound();
  595. }
  596. return $share;
  597. }
  598. /**
  599. * Get shares for a given path
  600. *
  601. * @param \OCP\Files\Node $path
  602. * @return IShare[]
  603. */
  604. public function getSharesByPath(Node $path) {
  605. $qb = $this->dbConnection->getQueryBuilder();
  606. // get federated user shares
  607. $cursor = $qb->select('*')
  608. ->from('share')
  609. ->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId())))
  610. ->andWhere($qb->expr()->in('share_type', $qb->createNamedParameter($this->supportedShareType, IQueryBuilder::PARAM_INT_ARRAY)))
  611. ->execute();
  612. $shares = [];
  613. while ($data = $cursor->fetch()) {
  614. $shares[] = $this->createShareObject($data);
  615. }
  616. $cursor->closeCursor();
  617. return $shares;
  618. }
  619. /**
  620. * @inheritdoc
  621. */
  622. public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
  623. /** @var IShare[] $shares */
  624. $shares = [];
  625. //Get shares directly with this user
  626. $qb = $this->dbConnection->getQueryBuilder();
  627. $qb->select('*')
  628. ->from('share');
  629. // Order by id
  630. $qb->orderBy('id');
  631. // Set limit and offset
  632. if ($limit !== -1) {
  633. $qb->setMaxResults($limit);
  634. }
  635. $qb->setFirstResult($offset);
  636. $qb->where($qb->expr()->in('share_type', $qb->createNamedParameter($this->supportedShareType, IQueryBuilder::PARAM_INT_ARRAY)));
  637. $qb->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)));
  638. // Filter by node if provided
  639. if ($node !== null) {
  640. $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
  641. }
  642. $cursor = $qb->execute();
  643. while ($data = $cursor->fetch()) {
  644. $shares[] = $this->createShareObject($data);
  645. }
  646. $cursor->closeCursor();
  647. return $shares;
  648. }
  649. /**
  650. * Get a share by token
  651. *
  652. * @param string $token
  653. * @return IShare
  654. * @throws ShareNotFound
  655. */
  656. public function getShareByToken($token) {
  657. $qb = $this->dbConnection->getQueryBuilder();
  658. $cursor = $qb->select('*')
  659. ->from('share')
  660. ->where($qb->expr()->in('share_type', $qb->createNamedParameter($this->supportedShareType, IQueryBuilder::PARAM_INT_ARRAY)))
  661. ->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token)))
  662. ->execute();
  663. $data = $cursor->fetch();
  664. if ($data === false) {
  665. throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
  666. }
  667. try {
  668. $share = $this->createShareObject($data);
  669. } catch (InvalidShare $e) {
  670. throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
  671. }
  672. return $share;
  673. }
  674. /**
  675. * get database row of a give share
  676. *
  677. * @param $id
  678. * @return array
  679. * @throws ShareNotFound
  680. */
  681. private function getRawShare($id) {
  682. // Now fetch the inserted share and create a complete share object
  683. $qb = $this->dbConnection->getQueryBuilder();
  684. $qb->select('*')
  685. ->from('share')
  686. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
  687. $cursor = $qb->execute();
  688. $data = $cursor->fetch();
  689. $cursor->closeCursor();
  690. if ($data === false) {
  691. throw new ShareNotFound;
  692. }
  693. return $data;
  694. }
  695. /**
  696. * Create a share object from an database row
  697. *
  698. * @param array $data
  699. * @return IShare
  700. * @throws InvalidShare
  701. * @throws ShareNotFound
  702. */
  703. private function createShareObject($data) {
  704. $share = new Share($this->rootFolder, $this->userManager);
  705. $share->setId((int)$data['id'])
  706. ->setShareType((int)$data['share_type'])
  707. ->setPermissions((int)$data['permissions'])
  708. ->setTarget($data['file_target'])
  709. ->setMailSend((bool)$data['mail_send'])
  710. ->setToken($data['token']);
  711. $shareTime = new \DateTime();
  712. $shareTime->setTimestamp((int)$data['stime']);
  713. $share->setShareTime($shareTime);
  714. $share->setSharedWith($data['share_with']);
  715. if ($data['uid_initiator'] !== null) {
  716. $share->setShareOwner($data['uid_owner']);
  717. $share->setSharedBy($data['uid_initiator']);
  718. } else {
  719. //OLD SHARE
  720. $share->setSharedBy($data['uid_owner']);
  721. $path = $this->getNode($share->getSharedBy(), (int)$data['file_source']);
  722. $owner = $path->getOwner();
  723. $share->setShareOwner($owner->getUID());
  724. }
  725. $share->setNodeId((int)$data['file_source']);
  726. $share->setNodeType($data['item_type']);
  727. $share->setProviderId($this->identifier());
  728. if ($data['expiration'] !== null) {
  729. $expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']);
  730. $share->setExpirationDate($expiration);
  731. }
  732. return $share;
  733. }
  734. /**
  735. * Get the node with file $id for $user
  736. *
  737. * @param string $userId
  738. * @param int $id
  739. * @return \OCP\Files\Node
  740. * @throws InvalidShare
  741. */
  742. private function getNode($userId, $id) {
  743. try {
  744. $userFolder = $this->rootFolder->getUserFolder($userId);
  745. } catch (NotFoundException $e) {
  746. throw new InvalidShare();
  747. }
  748. $node = $userFolder->getFirstNodeById($id);
  749. if (!$node) {
  750. throw new InvalidShare();
  751. }
  752. return $node;
  753. }
  754. /**
  755. * A user is deleted from the system
  756. * So clean up the relevant shares.
  757. *
  758. * @param string $uid
  759. * @param int $shareType
  760. */
  761. public function userDeleted($uid, $shareType) {
  762. //TODO: probably a good idea to send unshare info to remote servers
  763. $qb = $this->dbConnection->getQueryBuilder();
  764. $qb->delete('share')
  765. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_REMOTE)))
  766. ->andWhere($qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)))
  767. ->execute();
  768. }
  769. /**
  770. * This provider does not handle groups
  771. *
  772. * @param string $gid
  773. */
  774. public function groupDeleted($gid) {
  775. // We don't handle groups here
  776. }
  777. /**
  778. * This provider does not handle groups
  779. *
  780. * @param string $uid
  781. * @param string $gid
  782. */
  783. public function userDeletedFromGroup($uid, $gid) {
  784. // We don't handle groups here
  785. }
  786. /**
  787. * check if users from other Nextcloud instances are allowed to mount public links share by this instance
  788. *
  789. * @return bool
  790. */
  791. public function isOutgoingServer2serverShareEnabled() {
  792. if ($this->gsConfig->onlyInternalFederation()) {
  793. return false;
  794. }
  795. $result = $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes');
  796. return ($result === 'yes');
  797. }
  798. /**
  799. * check if users are allowed to mount public links from other Nextclouds
  800. *
  801. * @return bool
  802. */
  803. public function isIncomingServer2serverShareEnabled() {
  804. if ($this->gsConfig->onlyInternalFederation()) {
  805. return false;
  806. }
  807. $result = $this->config->getAppValue('files_sharing', 'incoming_server2server_share_enabled', 'yes');
  808. return ($result === 'yes');
  809. }
  810. /**
  811. * check if users from other Nextcloud instances are allowed to send federated group shares
  812. *
  813. * @return bool
  814. */
  815. public function isOutgoingServer2serverGroupShareEnabled() {
  816. if ($this->gsConfig->onlyInternalFederation()) {
  817. return false;
  818. }
  819. $result = $this->config->getAppValue('files_sharing', 'outgoing_server2server_group_share_enabled', 'no');
  820. return ($result === 'yes');
  821. }
  822. /**
  823. * check if users are allowed to receive federated group shares
  824. *
  825. * @return bool
  826. */
  827. public function isIncomingServer2serverGroupShareEnabled() {
  828. if ($this->gsConfig->onlyInternalFederation()) {
  829. return false;
  830. }
  831. $result = $this->config->getAppValue('files_sharing', 'incoming_server2server_group_share_enabled', 'no');
  832. return ($result === 'yes');
  833. }
  834. /**
  835. * check if federated group sharing is supported, therefore the OCM API need to be enabled
  836. *
  837. * @return bool
  838. */
  839. public function isFederatedGroupSharingSupported() {
  840. return $this->cloudFederationProviderManager->isReady();
  841. }
  842. /**
  843. * Check if querying sharees on the lookup server is enabled
  844. *
  845. * @return bool
  846. */
  847. public function isLookupServerQueriesEnabled() {
  848. // in a global scale setup we should always query the lookup server
  849. if ($this->gsConfig->isGlobalScaleEnabled()) {
  850. return true;
  851. }
  852. $result = $this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'yes');
  853. return ($result === 'yes');
  854. }
  855. /**
  856. * Check if it is allowed to publish user specific data to the lookup server
  857. *
  858. * @return bool
  859. */
  860. public function isLookupServerUploadEnabled() {
  861. // in a global scale setup the admin is responsible to keep the lookup server up-to-date
  862. if ($this->gsConfig->isGlobalScaleEnabled()) {
  863. return false;
  864. }
  865. $result = $this->config->getAppValue('files_sharing', 'lookupServerUploadEnabled', 'yes');
  866. return ($result === 'yes');
  867. }
  868. /**
  869. * @inheritdoc
  870. */
  871. public function getAccessList($nodes, $currentAccess) {
  872. $ids = [];
  873. foreach ($nodes as $node) {
  874. $ids[] = $node->getId();
  875. }
  876. $qb = $this->dbConnection->getQueryBuilder();
  877. $qb->select('share_with', 'token', 'file_source')
  878. ->from('share')
  879. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_REMOTE)))
  880. ->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
  881. ->andWhere($qb->expr()->orX(
  882. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  883. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  884. ));
  885. $cursor = $qb->execute();
  886. if ($currentAccess === false) {
  887. $remote = $cursor->fetch() !== false;
  888. $cursor->closeCursor();
  889. return ['remote' => $remote];
  890. }
  891. $remote = [];
  892. while ($row = $cursor->fetch()) {
  893. $remote[$row['share_with']] = [
  894. 'node_id' => $row['file_source'],
  895. 'token' => $row['token'],
  896. ];
  897. }
  898. $cursor->closeCursor();
  899. return ['remote' => $remote];
  900. }
  901. public function getAllShares(): iterable {
  902. $qb = $this->dbConnection->getQueryBuilder();
  903. $qb->select('*')
  904. ->from('share')
  905. ->where(
  906. $qb->expr()->orX(
  907. $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_REMOTE)),
  908. $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share\IShare::TYPE_REMOTE_GROUP))
  909. )
  910. );
  911. $cursor = $qb->execute();
  912. while ($data = $cursor->fetch()) {
  913. try {
  914. $share = $this->createShareObject($data);
  915. } catch (InvalidShare $e) {
  916. continue;
  917. } catch (ShareNotFound $e) {
  918. continue;
  919. }
  920. yield $share;
  921. }
  922. $cursor->closeCursor();
  923. }
  924. }