You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FederatedShareProvider.php 32KB

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