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

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