FederatedShareProvider.php 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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. $share->getShareType(),
  230. );
  231. return [$token, $remoteId];
  232. }
  233. /**
  234. * get federated share from the share_external table but exclude mounted link shares
  235. *
  236. * @param IShare $share
  237. * @return array
  238. * @throws ShareNotFound
  239. */
  240. protected function getShareFromExternalShareTable(IShare $share) {
  241. $query = $this->dbConnection->getQueryBuilder();
  242. $query->select('*')->from($this->externalShareTable)
  243. ->where($query->expr()->eq('user', $query->createNamedParameter($share->getShareOwner())))
  244. ->andWhere($query->expr()->eq('mountpoint', $query->createNamedParameter($share->getTarget())));
  245. $qResult = $query->executeQuery();
  246. $result = $qResult->fetchAll();
  247. $qResult->closeCursor();
  248. if (isset($result[0]) && (int)$result[0]['remote_id'] > 0) {
  249. return $result[0];
  250. }
  251. throw new ShareNotFound('share not found in share_external table');
  252. }
  253. /**
  254. * add share to the database and return the ID
  255. *
  256. * @param int $itemSource
  257. * @param string $itemType
  258. * @param string $shareWith
  259. * @param string $sharedBy
  260. * @param string $uidOwner
  261. * @param int $permissions
  262. * @param string $token
  263. * @param int $shareType
  264. * @param \DateTime $expirationDate
  265. * @return int
  266. */
  267. private function addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token, $shareType, $expirationDate) {
  268. $qb = $this->dbConnection->getQueryBuilder();
  269. $qb->insert('share')
  270. ->setValue('share_type', $qb->createNamedParameter($shareType))
  271. ->setValue('item_type', $qb->createNamedParameter($itemType))
  272. ->setValue('item_source', $qb->createNamedParameter($itemSource))
  273. ->setValue('file_source', $qb->createNamedParameter($itemSource))
  274. ->setValue('share_with', $qb->createNamedParameter($shareWith))
  275. ->setValue('uid_owner', $qb->createNamedParameter($uidOwner))
  276. ->setValue('uid_initiator', $qb->createNamedParameter($sharedBy))
  277. ->setValue('permissions', $qb->createNamedParameter($permissions))
  278. ->setValue('expiration', $qb->createNamedParameter($expirationDate, IQueryBuilder::PARAM_DATETIME_MUTABLE))
  279. ->setValue('token', $qb->createNamedParameter($token))
  280. ->setValue('stime', $qb->createNamedParameter(time()));
  281. /*
  282. * Added to fix https://github.com/owncloud/core/issues/22215
  283. * Can be removed once we get rid of ajax/share.php
  284. */
  285. $qb->setValue('file_target', $qb->createNamedParameter(''));
  286. $qb->executeStatement();
  287. return $qb->getLastInsertId();
  288. }
  289. /**
  290. * Update a share
  291. *
  292. * @param IShare $share
  293. * @return IShare The share object
  294. */
  295. public function update(IShare $share) {
  296. /*
  297. * We allow updating the permissions of federated shares
  298. */
  299. $qb = $this->dbConnection->getQueryBuilder();
  300. $qb->update('share')
  301. ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
  302. ->set('permissions', $qb->createNamedParameter($share->getPermissions()))
  303. ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
  304. ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
  305. ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATETIME_MUTABLE))
  306. ->executeStatement();
  307. // send the updated permission to the owner/initiator, if they are not the same
  308. if ($share->getShareOwner() !== $share->getSharedBy()) {
  309. $this->sendPermissionUpdate($share);
  310. }
  311. return $share;
  312. }
  313. /**
  314. * send the updated permission to the owner/initiator, if they are not the same
  315. *
  316. * @param IShare $share
  317. * @throws ShareNotFound
  318. * @throws HintException
  319. */
  320. protected function sendPermissionUpdate(IShare $share) {
  321. $remoteId = $this->getRemoteId($share);
  322. // if the local user is the owner we send the permission change to the initiator
  323. if ($this->userManager->userExists($share->getShareOwner())) {
  324. [, $remote] = $this->addressHandler->splitUserRemote($share->getSharedBy());
  325. } else { // ... if not we send the permission change to the owner
  326. [, $remote] = $this->addressHandler->splitUserRemote($share->getShareOwner());
  327. }
  328. $this->notifications->sendPermissionChange($remote, $remoteId, $share->getToken(), $share->getPermissions());
  329. }
  330. /**
  331. * update successful reShare with the correct token
  332. *
  333. * @param int $shareId
  334. * @param string $token
  335. */
  336. protected function updateSuccessfulReShare($shareId, $token) {
  337. $query = $this->dbConnection->getQueryBuilder();
  338. $query->update('share')
  339. ->where($query->expr()->eq('id', $query->createNamedParameter($shareId)))
  340. ->set('token', $query->createNamedParameter($token))
  341. ->executeStatement();
  342. }
  343. /**
  344. * store remote ID in federated reShare table
  345. *
  346. * @param $shareId
  347. * @param $remoteId
  348. */
  349. public function storeRemoteId(int $shareId, string $remoteId): void {
  350. $query = $this->dbConnection->getQueryBuilder();
  351. $query->insert('federated_reshares')
  352. ->values(
  353. [
  354. 'share_id' => $query->createNamedParameter($shareId),
  355. 'remote_id' => $query->createNamedParameter($remoteId),
  356. ]
  357. );
  358. $query->executeStatement();
  359. }
  360. /**
  361. * get share ID on remote server for federated re-shares
  362. *
  363. * @param IShare $share
  364. * @return string
  365. * @throws ShareNotFound
  366. */
  367. public function getRemoteId(IShare $share): string {
  368. $query = $this->dbConnection->getQueryBuilder();
  369. $query->select('remote_id')->from('federated_reshares')
  370. ->where($query->expr()->eq('share_id', $query->createNamedParameter((int)$share->getId())));
  371. $result = $query->executeQuery();
  372. $data = $result->fetch();
  373. $result->closeCursor();
  374. if (!is_array($data) || !isset($data['remote_id'])) {
  375. throw new ShareNotFound();
  376. }
  377. return (string)$data['remote_id'];
  378. }
  379. /**
  380. * @inheritdoc
  381. */
  382. public function move(IShare $share, $recipient) {
  383. /*
  384. * This function does nothing yet as it is just for outgoing
  385. * federated shares.
  386. */
  387. return $share;
  388. }
  389. /**
  390. * Get all children of this share
  391. *
  392. * @param IShare $parent
  393. * @return IShare[]
  394. */
  395. public function getChildren(IShare $parent) {
  396. $children = [];
  397. $qb = $this->dbConnection->getQueryBuilder();
  398. $qb->select('*')
  399. ->from('share')
  400. ->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId())))
  401. ->andWhere($qb->expr()->in('share_type', $qb->createNamedParameter($this->supportedShareType, IQueryBuilder::PARAM_INT_ARRAY)))
  402. ->orderBy('id');
  403. $cursor = $qb->executeQuery();
  404. while ($data = $cursor->fetch()) {
  405. $children[] = $this->createShareObject($data);
  406. }
  407. $cursor->closeCursor();
  408. return $children;
  409. }
  410. /**
  411. * Delete a share (owner unShares the file)
  412. *
  413. * @param IShare $share
  414. * @throws ShareNotFound
  415. * @throws HintException
  416. */
  417. public function delete(IShare $share) {
  418. [, $remote] = $this->addressHandler->splitUserRemote($share->getSharedWith());
  419. // if the local user is the owner we can send the unShare request directly...
  420. if ($this->userManager->userExists($share->getShareOwner())) {
  421. $this->notifications->sendRemoteUnShare($remote, $share->getId(), $share->getToken());
  422. $this->revokeShare($share, true);
  423. } else { // ... if not we need to correct ID for the unShare request
  424. $remoteId = $this->getRemoteId($share);
  425. $this->notifications->sendRemoteUnShare($remote, $remoteId, $share->getToken());
  426. $this->revokeShare($share, false);
  427. }
  428. // only remove the share when all messages are send to not lose information
  429. // about the share to early
  430. $this->removeShareFromTable($share);
  431. }
  432. /**
  433. * in case of a re-share we need to send the other use (initiator or owner)
  434. * a message that the file was unshared
  435. *
  436. * @param IShare $share
  437. * @param bool $isOwner the user can either be the owner or the user who re-sahred it
  438. * @throws ShareNotFound
  439. * @throws HintException
  440. */
  441. protected function revokeShare($share, $isOwner) {
  442. if ($this->userManager->userExists($share->getShareOwner()) && $this->userManager->userExists($share->getSharedBy())) {
  443. // If both the owner and the initiator of the share are local users we don't have to notify anybody else
  444. return;
  445. }
  446. // also send a unShare request to the initiator, if this is a different user than the owner
  447. if ($share->getShareOwner() !== $share->getSharedBy()) {
  448. if ($isOwner) {
  449. [, $remote] = $this->addressHandler->splitUserRemote($share->getSharedBy());
  450. } else {
  451. [, $remote] = $this->addressHandler->splitUserRemote($share->getShareOwner());
  452. }
  453. $remoteId = $this->getRemoteId($share);
  454. $this->notifications->sendRevokeShare($remote, $remoteId, $share->getToken());
  455. }
  456. }
  457. /**
  458. * remove share from table
  459. *
  460. * @param IShare $share
  461. */
  462. public function removeShareFromTable(IShare $share) {
  463. $this->removeShareFromTableById($share->getId());
  464. }
  465. /**
  466. * remove share from table
  467. *
  468. * @param string $shareId
  469. */
  470. private function removeShareFromTableById($shareId) {
  471. $qb = $this->dbConnection->getQueryBuilder();
  472. $qb->delete('share')
  473. ->where($qb->expr()->eq('id', $qb->createNamedParameter($shareId)))
  474. ->andWhere($qb->expr()->neq('share_type', $qb->createNamedParameter(IShare::TYPE_CIRCLE)));
  475. $qb->executeStatement();
  476. $qb = $this->dbConnection->getQueryBuilder();
  477. $qb->delete('federated_reshares')
  478. ->where($qb->expr()->eq('share_id', $qb->createNamedParameter($shareId)));
  479. $qb->executeStatement();
  480. }
  481. /**
  482. * @inheritdoc
  483. */
  484. public function deleteFromSelf(IShare $share, $recipient) {
  485. // nothing to do here. Technically deleteFromSelf in the context of federated
  486. // shares is a umount of an external storage. This is handled here
  487. // apps/files_sharing/lib/external/manager.php
  488. // TODO move this code over to this app
  489. }
  490. public function restore(IShare $share, string $recipient): IShare {
  491. throw new GenericShareException('not implemented');
  492. }
  493. public function getSharesInFolder($userId, Folder $node, $reshares, $shallow = true) {
  494. if (!$shallow) {
  495. throw new \Exception('non-shallow getSharesInFolder is no longer supported');
  496. }
  497. $qb = $this->dbConnection->getQueryBuilder();
  498. $qb->select('*')
  499. ->from('share', 's')
  500. ->andWhere($qb->expr()->orX(
  501. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  502. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  503. ))
  504. ->andWhere(
  505. $qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_REMOTE))
  506. );
  507. /**
  508. * Reshares for this user are shares where they are the owner.
  509. */
  510. if ($reshares === false) {
  511. $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
  512. } else {
  513. $qb->andWhere(
  514. $qb->expr()->orX(
  515. $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
  516. $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
  517. )
  518. );
  519. }
  520. $qb->innerJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
  521. $qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));
  522. $qb->orderBy('id');
  523. $cursor = $qb->executeQuery();
  524. $shares = [];
  525. while ($data = $cursor->fetch()) {
  526. $shares[$data['fileid']][] = $this->createShareObject($data);
  527. }
  528. $cursor->closeCursor();
  529. return $shares;
  530. }
  531. /**
  532. * @inheritdoc
  533. */
  534. public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) {
  535. $qb = $this->dbConnection->getQueryBuilder();
  536. $qb->select('*')
  537. ->from('share');
  538. $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter($shareType)));
  539. /**
  540. * Reshares for this user are shares where they are the owner.
  541. */
  542. if ($reshares === false) {
  543. //Special case for old shares created via the web UI
  544. $or1 = $qb->expr()->andX(
  545. $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
  546. $qb->expr()->isNull('uid_initiator')
  547. );
  548. $qb->andWhere(
  549. $qb->expr()->orX(
  550. $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)),
  551. $or1
  552. )
  553. );
  554. } else {
  555. $qb->andWhere(
  556. $qb->expr()->orX(
  557. $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
  558. $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
  559. )
  560. );
  561. }
  562. if ($node !== null) {
  563. $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
  564. }
  565. if ($limit !== -1) {
  566. $qb->setMaxResults($limit);
  567. }
  568. $qb->setFirstResult($offset);
  569. $qb->orderBy('id');
  570. $cursor = $qb->executeQuery();
  571. $shares = [];
  572. while ($data = $cursor->fetch()) {
  573. $shares[] = $this->createShareObject($data);
  574. }
  575. $cursor->closeCursor();
  576. return $shares;
  577. }
  578. /**
  579. * @inheritdoc
  580. */
  581. public function getShareById($id, $recipientId = null) {
  582. $qb = $this->dbConnection->getQueryBuilder();
  583. $qb->select('*')
  584. ->from('share')
  585. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
  586. ->andWhere($qb->expr()->in('share_type', $qb->createNamedParameter($this->supportedShareType, IQueryBuilder::PARAM_INT_ARRAY)));
  587. $cursor = $qb->executeQuery();
  588. $data = $cursor->fetch();
  589. $cursor->closeCursor();
  590. if ($data === false) {
  591. throw new ShareNotFound('Can not find share with ID: ' . $id);
  592. }
  593. try {
  594. $share = $this->createShareObject($data);
  595. } catch (InvalidShare $e) {
  596. throw new ShareNotFound();
  597. }
  598. return $share;
  599. }
  600. /**
  601. * Get shares for a given path
  602. *
  603. * @param Node $path
  604. * @return IShare[]
  605. */
  606. public function getSharesByPath(Node $path) {
  607. $qb = $this->dbConnection->getQueryBuilder();
  608. // get federated user shares
  609. $cursor = $qb->select('*')
  610. ->from('share')
  611. ->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId())))
  612. ->andWhere($qb->expr()->in('share_type', $qb->createNamedParameter($this->supportedShareType, IQueryBuilder::PARAM_INT_ARRAY)))
  613. ->executeQuery();
  614. $shares = [];
  615. while ($data = $cursor->fetch()) {
  616. $shares[] = $this->createShareObject($data);
  617. }
  618. $cursor->closeCursor();
  619. return $shares;
  620. }
  621. /**
  622. * @inheritdoc
  623. */
  624. public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
  625. /** @var IShare[] $shares */
  626. $shares = [];
  627. //Get shares directly with this user
  628. $qb = $this->dbConnection->getQueryBuilder();
  629. $qb->select('*')
  630. ->from('share');
  631. // Order by id
  632. $qb->orderBy('id');
  633. // Set limit and offset
  634. if ($limit !== -1) {
  635. $qb->setMaxResults($limit);
  636. }
  637. $qb->setFirstResult($offset);
  638. $qb->where($qb->expr()->in('share_type', $qb->createNamedParameter($this->supportedShareType, IQueryBuilder::PARAM_INT_ARRAY)));
  639. $qb->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)));
  640. // Filter by node if provided
  641. if ($node !== null) {
  642. $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
  643. }
  644. $cursor = $qb->executeQuery();
  645. while ($data = $cursor->fetch()) {
  646. $shares[] = $this->createShareObject($data);
  647. }
  648. $cursor->closeCursor();
  649. return $shares;
  650. }
  651. /**
  652. * Get a share by token
  653. *
  654. * @param string $token
  655. * @return IShare
  656. * @throws ShareNotFound
  657. */
  658. public function getShareByToken($token) {
  659. $qb = $this->dbConnection->getQueryBuilder();
  660. $cursor = $qb->select('*')
  661. ->from('share')
  662. ->where($qb->expr()->in('share_type', $qb->createNamedParameter($this->supportedShareType, IQueryBuilder::PARAM_INT_ARRAY)))
  663. ->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token)))
  664. ->executeQuery();
  665. $data = $cursor->fetch();
  666. if ($data === false) {
  667. throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
  668. }
  669. try {
  670. $share = $this->createShareObject($data);
  671. } catch (InvalidShare $e) {
  672. throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
  673. }
  674. return $share;
  675. }
  676. /**
  677. * get database row of a give share
  678. *
  679. * @param $id
  680. * @return array
  681. * @throws ShareNotFound
  682. */
  683. private function getRawShare($id) {
  684. // Now fetch the inserted share and create a complete share object
  685. $qb = $this->dbConnection->getQueryBuilder();
  686. $qb->select('*')
  687. ->from('share')
  688. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
  689. $cursor = $qb->executeQuery();
  690. $data = $cursor->fetch();
  691. $cursor->closeCursor();
  692. if ($data === false) {
  693. throw new ShareNotFound;
  694. }
  695. return $data;
  696. }
  697. /**
  698. * Create a share object from an database row
  699. *
  700. * @param array $data
  701. * @return IShare
  702. * @throws InvalidShare
  703. * @throws ShareNotFound
  704. */
  705. private function createShareObject($data) {
  706. $share = new Share($this->rootFolder, $this->userManager);
  707. $share->setId((int)$data['id'])
  708. ->setShareType((int)$data['share_type'])
  709. ->setPermissions((int)$data['permissions'])
  710. ->setTarget($data['file_target'])
  711. ->setMailSend((bool)$data['mail_send'])
  712. ->setToken($data['token']);
  713. $shareTime = new \DateTime();
  714. $shareTime->setTimestamp((int)$data['stime']);
  715. $share->setShareTime($shareTime);
  716. $share->setSharedWith($data['share_with']);
  717. if ($data['uid_initiator'] !== null) {
  718. $share->setShareOwner($data['uid_owner']);
  719. $share->setSharedBy($data['uid_initiator']);
  720. } else {
  721. //OLD SHARE
  722. $share->setSharedBy($data['uid_owner']);
  723. $path = $this->getNode($share->getSharedBy(), (int)$data['file_source']);
  724. $owner = $path->getOwner();
  725. $share->setShareOwner($owner->getUID());
  726. }
  727. $share->setNodeId((int)$data['file_source']);
  728. $share->setNodeType($data['item_type']);
  729. $share->setProviderId($this->identifier());
  730. if ($data['expiration'] !== null) {
  731. $expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']);
  732. $share->setExpirationDate($expiration);
  733. }
  734. return $share;
  735. }
  736. /**
  737. * Get the node with file $id for $user
  738. *
  739. * @param string $userId
  740. * @param int $id
  741. * @return Node
  742. * @throws InvalidShare
  743. */
  744. private function getNode($userId, $id) {
  745. try {
  746. $userFolder = $this->rootFolder->getUserFolder($userId);
  747. } catch (NotFoundException $e) {
  748. throw new InvalidShare();
  749. }
  750. $node = $userFolder->getFirstNodeById($id);
  751. if (!$node) {
  752. throw new InvalidShare();
  753. }
  754. return $node;
  755. }
  756. /**
  757. * A user is deleted from the system
  758. * So clean up the relevant shares.
  759. *
  760. * @param string $uid
  761. * @param int $shareType
  762. */
  763. public function userDeleted($uid, $shareType) {
  764. //TODO: probably a good idea to send unshare info to remote servers
  765. $qb = $this->dbConnection->getQueryBuilder();
  766. $qb->delete('share')
  767. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_REMOTE)))
  768. ->andWhere($qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)))
  769. ->executeStatement();
  770. }
  771. /**
  772. * This provider does not handle groups
  773. *
  774. * @param string $gid
  775. */
  776. public function groupDeleted($gid) {
  777. // We don't handle groups here
  778. }
  779. /**
  780. * This provider does not handle groups
  781. *
  782. * @param string $uid
  783. * @param string $gid
  784. */
  785. public function userDeletedFromGroup($uid, $gid) {
  786. // We don't handle groups here
  787. }
  788. /**
  789. * check if users from other Nextcloud instances are allowed to mount public links share by this instance
  790. *
  791. * @return bool
  792. */
  793. public function isOutgoingServer2serverShareEnabled() {
  794. if ($this->gsConfig->onlyInternalFederation()) {
  795. return false;
  796. }
  797. $result = $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes');
  798. return ($result === 'yes');
  799. }
  800. /**
  801. * check if users are allowed to mount public links from other Nextclouds
  802. *
  803. * @return bool
  804. */
  805. public function isIncomingServer2serverShareEnabled() {
  806. if ($this->gsConfig->onlyInternalFederation()) {
  807. return false;
  808. }
  809. $result = $this->config->getAppValue('files_sharing', 'incoming_server2server_share_enabled', 'yes');
  810. return ($result === 'yes');
  811. }
  812. /**
  813. * check if users from other Nextcloud instances are allowed to send federated group shares
  814. *
  815. * @return bool
  816. */
  817. public function isOutgoingServer2serverGroupShareEnabled() {
  818. if ($this->gsConfig->onlyInternalFederation()) {
  819. return false;
  820. }
  821. $result = $this->config->getAppValue('files_sharing', 'outgoing_server2server_group_share_enabled', 'no');
  822. return ($result === 'yes');
  823. }
  824. /**
  825. * check if users are allowed to receive federated group shares
  826. *
  827. * @return bool
  828. */
  829. public function isIncomingServer2serverGroupShareEnabled() {
  830. if ($this->gsConfig->onlyInternalFederation()) {
  831. return false;
  832. }
  833. $result = $this->config->getAppValue('files_sharing', 'incoming_server2server_group_share_enabled', 'no');
  834. return ($result === 'yes');
  835. }
  836. /**
  837. * check if federated group sharing is supported, therefore the OCM API need to be enabled
  838. *
  839. * @return bool
  840. */
  841. public function isFederatedGroupSharingSupported() {
  842. return $this->cloudFederationProviderManager->isReady();
  843. }
  844. /**
  845. * Check if querying sharees on the lookup server is enabled
  846. *
  847. * @return bool
  848. */
  849. public function isLookupServerQueriesEnabled() {
  850. // in a global scale setup we should always query the lookup server
  851. if ($this->gsConfig->isGlobalScaleEnabled()) {
  852. return true;
  853. }
  854. $result = $this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'yes');
  855. return ($result === 'yes');
  856. }
  857. /**
  858. * Check if it is allowed to publish user specific data to the lookup server
  859. *
  860. * @return bool
  861. */
  862. public function isLookupServerUploadEnabled() {
  863. // in a global scale setup the admin is responsible to keep the lookup server up-to-date
  864. if ($this->gsConfig->isGlobalScaleEnabled()) {
  865. return false;
  866. }
  867. $result = $this->config->getAppValue('files_sharing', 'lookupServerUploadEnabled', 'yes');
  868. return ($result === 'yes');
  869. }
  870. /**
  871. * @inheritdoc
  872. */
  873. public function getAccessList($nodes, $currentAccess) {
  874. $ids = [];
  875. foreach ($nodes as $node) {
  876. $ids[] = $node->getId();
  877. }
  878. $qb = $this->dbConnection->getQueryBuilder();
  879. $qb->select('share_with', 'token', 'file_source')
  880. ->from('share')
  881. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_REMOTE)))
  882. ->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
  883. ->andWhere($qb->expr()->orX(
  884. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  885. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  886. ));
  887. $cursor = $qb->executeQuery();
  888. if ($currentAccess === false) {
  889. $remote = $cursor->fetch() !== false;
  890. $cursor->closeCursor();
  891. return ['remote' => $remote];
  892. }
  893. $remote = [];
  894. while ($row = $cursor->fetch()) {
  895. $remote[$row['share_with']] = [
  896. 'node_id' => $row['file_source'],
  897. 'token' => $row['token'],
  898. ];
  899. }
  900. $cursor->closeCursor();
  901. return ['remote' => $remote];
  902. }
  903. public function getAllShares(): iterable {
  904. $qb = $this->dbConnection->getQueryBuilder();
  905. $qb->select('*')
  906. ->from('share')
  907. ->where(
  908. $qb->expr()->orX(
  909. $qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_REMOTE)),
  910. $qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_REMOTE_GROUP))
  911. )
  912. );
  913. $cursor = $qb->executeQuery();
  914. while ($data = $cursor->fetch()) {
  915. try {
  916. $share = $this->createShareObject($data);
  917. } catch (InvalidShare $e) {
  918. continue;
  919. } catch (ShareNotFound $e) {
  920. continue;
  921. }
  922. yield $share;
  923. }
  924. $cursor->closeCursor();
  925. }
  926. }