1
0

FederatedShareProvider.php 31 KB

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