DefaultShareProvider.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Share20;
  25. use OCP\Files\File;
  26. use OCP\Share\IShareProvider;
  27. use OC\Share20\Exception\InvalidShare;
  28. use OC\Share20\Exception\ProviderException;
  29. use OCP\Share\Exceptions\ShareNotFound;
  30. use OC\Share20\Exception\BackendError;
  31. use OCP\DB\QueryBuilder\IQueryBuilder;
  32. use OCP\IGroup;
  33. use OCP\IGroupManager;
  34. use OCP\IUserManager;
  35. use OCP\Files\IRootFolder;
  36. use OCP\IDBConnection;
  37. use OCP\Files\Node;
  38. /**
  39. * Class DefaultShareProvider
  40. *
  41. * @package OC\Share20
  42. */
  43. class DefaultShareProvider implements IShareProvider {
  44. // Special share type for user modified group shares
  45. const SHARE_TYPE_USERGROUP = 2;
  46. /** @var IDBConnection */
  47. private $dbConn;
  48. /** @var IUserManager */
  49. private $userManager;
  50. /** @var IGroupManager */
  51. private $groupManager;
  52. /** @var IRootFolder */
  53. private $rootFolder;
  54. /**
  55. * DefaultShareProvider constructor.
  56. *
  57. * @param IDBConnection $connection
  58. * @param IUserManager $userManager
  59. * @param IGroupManager $groupManager
  60. * @param IRootFolder $rootFolder
  61. */
  62. public function __construct(
  63. IDBConnection $connection,
  64. IUserManager $userManager,
  65. IGroupManager $groupManager,
  66. IRootFolder $rootFolder) {
  67. $this->dbConn = $connection;
  68. $this->userManager = $userManager;
  69. $this->groupManager = $groupManager;
  70. $this->rootFolder = $rootFolder;
  71. }
  72. /**
  73. * Return the identifier of this provider.
  74. *
  75. * @return string Containing only [a-zA-Z0-9]
  76. */
  77. public function identifier() {
  78. return 'ocinternal';
  79. }
  80. /**
  81. * Share a path
  82. *
  83. * @param \OCP\Share\IShare $share
  84. * @return \OCP\Share\IShare The share object
  85. * @throws ShareNotFound
  86. * @throws \Exception
  87. */
  88. public function create(\OCP\Share\IShare $share) {
  89. $qb = $this->dbConn->getQueryBuilder();
  90. $qb->insert('share');
  91. $qb->setValue('share_type', $qb->createNamedParameter($share->getShareType()));
  92. if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
  93. //Set the UID of the user we share with
  94. $qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()));
  95. } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
  96. //Set the GID of the group we share with
  97. $qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()));
  98. } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
  99. //Set the token of the share
  100. $qb->setValue('token', $qb->createNamedParameter($share->getToken()));
  101. //If a password is set store it
  102. if ($share->getPassword() !== null) {
  103. $qb->setValue('share_with', $qb->createNamedParameter($share->getPassword()));
  104. }
  105. //If an expiration date is set store it
  106. if ($share->getExpirationDate() !== null) {
  107. $qb->setValue('expiration', $qb->createNamedParameter($share->getExpirationDate(), 'datetime'));
  108. }
  109. if (method_exists($share, 'getParent')) {
  110. $qb->setValue('parent', $qb->createNamedParameter($share->getParent()));
  111. }
  112. } else {
  113. throw new \Exception('invalid share type!');
  114. }
  115. // Set what is shares
  116. $qb->setValue('item_type', $qb->createParameter('itemType'));
  117. if ($share->getNode() instanceof \OCP\Files\File) {
  118. $qb->setParameter('itemType', 'file');
  119. } else {
  120. $qb->setParameter('itemType', 'folder');
  121. }
  122. // Set the file id
  123. $qb->setValue('item_source', $qb->createNamedParameter($share->getNode()->getId()));
  124. $qb->setValue('file_source', $qb->createNamedParameter($share->getNode()->getId()));
  125. // set the permissions
  126. $qb->setValue('permissions', $qb->createNamedParameter($share->getPermissions()));
  127. // Set who created this share
  128. $qb->setValue('uid_initiator', $qb->createNamedParameter($share->getSharedBy()));
  129. // Set who is the owner of this file/folder (and this the owner of the share)
  130. $qb->setValue('uid_owner', $qb->createNamedParameter($share->getShareOwner()));
  131. // Set the file target
  132. $qb->setValue('file_target', $qb->createNamedParameter($share->getTarget()));
  133. // Set the time this share was created
  134. $qb->setValue('stime', $qb->createNamedParameter(time()));
  135. // insert the data and fetch the id of the share
  136. $this->dbConn->beginTransaction();
  137. $qb->execute();
  138. $id = $this->dbConn->lastInsertId('*PREFIX*share');
  139. $this->dbConn->commit();
  140. // Now fetch the inserted share and create a complete share object
  141. $qb = $this->dbConn->getQueryBuilder();
  142. $qb->select('*')
  143. ->from('share')
  144. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
  145. $cursor = $qb->execute();
  146. $data = $cursor->fetch();
  147. $cursor->closeCursor();
  148. if ($data === false) {
  149. throw new ShareNotFound();
  150. }
  151. $share = $this->createShare($data);
  152. return $share;
  153. }
  154. /**
  155. * Update a share
  156. *
  157. * @param \OCP\Share\IShare $share
  158. * @return \OCP\Share\IShare The share object
  159. */
  160. public function update(\OCP\Share\IShare $share) {
  161. if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
  162. /*
  163. * We allow updating the recipient on user shares.
  164. */
  165. $qb = $this->dbConn->getQueryBuilder();
  166. $qb->update('share')
  167. ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
  168. ->set('share_with', $qb->createNamedParameter($share->getSharedWith()))
  169. ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
  170. ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
  171. ->set('permissions', $qb->createNamedParameter($share->getPermissions()))
  172. ->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
  173. ->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
  174. ->execute();
  175. } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
  176. $qb = $this->dbConn->getQueryBuilder();
  177. $qb->update('share')
  178. ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
  179. ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
  180. ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
  181. ->set('permissions', $qb->createNamedParameter($share->getPermissions()))
  182. ->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
  183. ->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
  184. ->execute();
  185. /*
  186. * Update all user defined group shares
  187. */
  188. $qb = $this->dbConn->getQueryBuilder();
  189. $qb->update('share')
  190. ->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
  191. ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
  192. ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
  193. ->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
  194. ->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
  195. ->execute();
  196. /*
  197. * Now update the permissions for all children that have not set it to 0
  198. */
  199. $qb = $this->dbConn->getQueryBuilder();
  200. $qb->update('share')
  201. ->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
  202. ->andWhere($qb->expr()->neq('permissions', $qb->createNamedParameter(0)))
  203. ->set('permissions', $qb->createNamedParameter($share->getPermissions()))
  204. ->execute();
  205. } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
  206. $qb = $this->dbConn->getQueryBuilder();
  207. $qb->update('share')
  208. ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
  209. ->set('share_with', $qb->createNamedParameter($share->getPassword()))
  210. ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
  211. ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
  212. ->set('permissions', $qb->createNamedParameter($share->getPermissions()))
  213. ->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
  214. ->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
  215. ->set('token', $qb->createNamedParameter($share->getToken()))
  216. ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
  217. ->execute();
  218. }
  219. return $share;
  220. }
  221. /**
  222. * Get all children of this share
  223. * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in
  224. *
  225. * @param \OCP\Share\IShare $parent
  226. * @return \OCP\Share\IShare[]
  227. */
  228. public function getChildren(\OCP\Share\IShare $parent) {
  229. $children = [];
  230. $qb = $this->dbConn->getQueryBuilder();
  231. $qb->select('*')
  232. ->from('share')
  233. ->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId())))
  234. ->andWhere(
  235. $qb->expr()->in(
  236. 'share_type',
  237. $qb->createNamedParameter([
  238. \OCP\Share::SHARE_TYPE_USER,
  239. \OCP\Share::SHARE_TYPE_GROUP,
  240. \OCP\Share::SHARE_TYPE_LINK,
  241. ], IQueryBuilder::PARAM_INT_ARRAY)
  242. )
  243. )
  244. ->andWhere($qb->expr()->orX(
  245. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  246. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  247. ))
  248. ->orderBy('id');
  249. $cursor = $qb->execute();
  250. while($data = $cursor->fetch()) {
  251. $children[] = $this->createShare($data);
  252. }
  253. $cursor->closeCursor();
  254. return $children;
  255. }
  256. /**
  257. * Delete a share
  258. *
  259. * @param \OCP\Share\IShare $share
  260. */
  261. public function delete(\OCP\Share\IShare $share) {
  262. $qb = $this->dbConn->getQueryBuilder();
  263. $qb->delete('share')
  264. ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())));
  265. /*
  266. * If the share is a group share delete all possible
  267. * user defined groups shares.
  268. */
  269. if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
  270. $qb->orWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())));
  271. }
  272. $qb->execute();
  273. }
  274. /**
  275. * Unshare a share from the recipient. If this is a group share
  276. * this means we need a special entry in the share db.
  277. *
  278. * @param \OCP\Share\IShare $share
  279. * @param string $recipient UserId of recipient
  280. * @throws BackendError
  281. * @throws ProviderException
  282. */
  283. public function deleteFromSelf(\OCP\Share\IShare $share, $recipient) {
  284. if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
  285. $group = $this->groupManager->get($share->getSharedWith());
  286. $user = $this->userManager->get($recipient);
  287. if (!$group->inGroup($user)) {
  288. throw new ProviderException('Recipient not in receiving group');
  289. }
  290. // Try to fetch user specific share
  291. $qb = $this->dbConn->getQueryBuilder();
  292. $stmt = $qb->select('*')
  293. ->from('share')
  294. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
  295. ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient)))
  296. ->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
  297. ->andWhere($qb->expr()->orX(
  298. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  299. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  300. ))
  301. ->execute();
  302. $data = $stmt->fetch();
  303. /*
  304. * Check if there already is a user specific group share.
  305. * If there is update it (if required).
  306. */
  307. if ($data === false) {
  308. $qb = $this->dbConn->getQueryBuilder();
  309. $type = $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder';
  310. //Insert new share
  311. $qb->insert('share')
  312. ->values([
  313. 'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP),
  314. 'share_with' => $qb->createNamedParameter($recipient),
  315. 'uid_owner' => $qb->createNamedParameter($share->getShareOwner()),
  316. 'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()),
  317. 'parent' => $qb->createNamedParameter($share->getId()),
  318. 'item_type' => $qb->createNamedParameter($type),
  319. 'item_source' => $qb->createNamedParameter($share->getNode()->getId()),
  320. 'file_source' => $qb->createNamedParameter($share->getNode()->getId()),
  321. 'file_target' => $qb->createNamedParameter($share->getTarget()),
  322. 'permissions' => $qb->createNamedParameter(0),
  323. 'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()),
  324. ])->execute();
  325. } else if ($data['permissions'] !== 0) {
  326. // Update existing usergroup share
  327. $qb = $this->dbConn->getQueryBuilder();
  328. $qb->update('share')
  329. ->set('permissions', $qb->createNamedParameter(0))
  330. ->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id'])))
  331. ->execute();
  332. }
  333. } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
  334. if ($share->getSharedWith() !== $recipient) {
  335. throw new ProviderException('Recipient does not match');
  336. }
  337. // We can just delete user and link shares
  338. $this->delete($share);
  339. } else {
  340. throw new ProviderException('Invalid shareType');
  341. }
  342. }
  343. /**
  344. * @inheritdoc
  345. */
  346. public function move(\OCP\Share\IShare $share, $recipient) {
  347. if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
  348. // Just update the target
  349. $qb = $this->dbConn->getQueryBuilder();
  350. $qb->update('share')
  351. ->set('file_target', $qb->createNamedParameter($share->getTarget()))
  352. ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
  353. ->execute();
  354. } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
  355. // Check if there is a usergroup share
  356. $qb = $this->dbConn->getQueryBuilder();
  357. $stmt = $qb->select('id')
  358. ->from('share')
  359. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
  360. ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient)))
  361. ->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
  362. ->andWhere($qb->expr()->orX(
  363. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  364. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  365. ))
  366. ->setMaxResults(1)
  367. ->execute();
  368. $data = $stmt->fetch();
  369. $stmt->closeCursor();
  370. if ($data === false) {
  371. // No usergroup share yet. Create one.
  372. $qb = $this->dbConn->getQueryBuilder();
  373. $qb->insert('share')
  374. ->values([
  375. 'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP),
  376. 'share_with' => $qb->createNamedParameter($recipient),
  377. 'uid_owner' => $qb->createNamedParameter($share->getShareOwner()),
  378. 'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()),
  379. 'parent' => $qb->createNamedParameter($share->getId()),
  380. 'item_type' => $qb->createNamedParameter($share->getNode() instanceof File ? 'file' : 'folder'),
  381. 'item_source' => $qb->createNamedParameter($share->getNode()->getId()),
  382. 'file_source' => $qb->createNamedParameter($share->getNode()->getId()),
  383. 'file_target' => $qb->createNamedParameter($share->getTarget()),
  384. 'permissions' => $qb->createNamedParameter($share->getPermissions()),
  385. 'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()),
  386. ])->execute();
  387. } else {
  388. // Already a usergroup share. Update it.
  389. $qb = $this->dbConn->getQueryBuilder();
  390. $qb->update('share')
  391. ->set('file_target', $qb->createNamedParameter($share->getTarget()))
  392. ->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id'])))
  393. ->execute();
  394. }
  395. }
  396. return $share;
  397. }
  398. /**
  399. * @inheritdoc
  400. */
  401. public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) {
  402. $qb = $this->dbConn->getQueryBuilder();
  403. $qb->select('*')
  404. ->from('share')
  405. ->andWhere($qb->expr()->orX(
  406. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  407. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  408. ));
  409. $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter($shareType)));
  410. /**
  411. * Reshares for this user are shares where they are the owner.
  412. */
  413. if ($reshares === false) {
  414. $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
  415. } else {
  416. $qb->andWhere(
  417. $qb->expr()->orX(
  418. $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
  419. $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
  420. )
  421. );
  422. }
  423. if ($node !== null) {
  424. $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
  425. }
  426. if ($limit !== -1) {
  427. $qb->setMaxResults($limit);
  428. }
  429. $qb->setFirstResult($offset);
  430. $qb->orderBy('id');
  431. $cursor = $qb->execute();
  432. $shares = [];
  433. while($data = $cursor->fetch()) {
  434. $shares[] = $this->createShare($data);
  435. }
  436. $cursor->closeCursor();
  437. return $shares;
  438. }
  439. /**
  440. * @inheritdoc
  441. */
  442. public function getShareById($id, $recipientId = null) {
  443. $qb = $this->dbConn->getQueryBuilder();
  444. $qb->select('*')
  445. ->from('share')
  446. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
  447. ->andWhere(
  448. $qb->expr()->in(
  449. 'share_type',
  450. $qb->createNamedParameter([
  451. \OCP\Share::SHARE_TYPE_USER,
  452. \OCP\Share::SHARE_TYPE_GROUP,
  453. \OCP\Share::SHARE_TYPE_LINK,
  454. ], IQueryBuilder::PARAM_INT_ARRAY)
  455. )
  456. )
  457. ->andWhere($qb->expr()->orX(
  458. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  459. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  460. ));
  461. $cursor = $qb->execute();
  462. $data = $cursor->fetch();
  463. $cursor->closeCursor();
  464. if ($data === false) {
  465. throw new ShareNotFound();
  466. }
  467. try {
  468. $share = $this->createShare($data);
  469. } catch (InvalidShare $e) {
  470. throw new ShareNotFound();
  471. }
  472. // If the recipient is set for a group share resolve to that user
  473. if ($recipientId !== null && $share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
  474. $share = $this->resolveGroupShare($share, $recipientId);
  475. }
  476. return $share;
  477. }
  478. /**
  479. * Get shares for a given path
  480. *
  481. * @param \OCP\Files\Node $path
  482. * @return \OCP\Share\IShare[]
  483. */
  484. public function getSharesByPath(Node $path) {
  485. $qb = $this->dbConn->getQueryBuilder();
  486. $cursor = $qb->select('*')
  487. ->from('share')
  488. ->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId())))
  489. ->andWhere(
  490. $qb->expr()->orX(
  491. $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)),
  492. $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))
  493. )
  494. )
  495. ->andWhere($qb->expr()->orX(
  496. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  497. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  498. ))
  499. ->execute();
  500. $shares = [];
  501. while($data = $cursor->fetch()) {
  502. $shares[] = $this->createShare($data);
  503. }
  504. $cursor->closeCursor();
  505. return $shares;
  506. }
  507. /**
  508. * @inheritdoc
  509. */
  510. public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
  511. /** @var Share[] $shares */
  512. $shares = [];
  513. if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
  514. //Get shares directly with this user
  515. $qb = $this->dbConn->getQueryBuilder();
  516. $qb->select('*')
  517. ->from('share');
  518. // Order by id
  519. $qb->orderBy('id');
  520. // Set limit and offset
  521. if ($limit !== -1) {
  522. $qb->setMaxResults($limit);
  523. }
  524. $qb->setFirstResult($offset);
  525. $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)))
  526. ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)))
  527. ->andWhere($qb->expr()->orX(
  528. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  529. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  530. ));
  531. // Filter by node if provided
  532. if ($node !== null) {
  533. $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
  534. }
  535. $cursor = $qb->execute();
  536. while($data = $cursor->fetch()) {
  537. $shares[] = $this->createShare($data);
  538. }
  539. $cursor->closeCursor();
  540. } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
  541. $user = $this->userManager->get($userId);
  542. $allGroups = $this->groupManager->getUserGroups($user);
  543. /** @var Share[] $shares2 */
  544. $shares2 = [];
  545. $start = 0;
  546. while(true) {
  547. $groups = array_slice($allGroups, $start, 100);
  548. $start += 100;
  549. if ($groups === []) {
  550. break;
  551. }
  552. $qb = $this->dbConn->getQueryBuilder();
  553. $qb->select('*')
  554. ->from('share')
  555. ->orderBy('id')
  556. ->setFirstResult(0);
  557. if ($limit !== -1) {
  558. $qb->setMaxResults($limit - count($shares));
  559. }
  560. // Filter by node if provided
  561. if ($node !== null) {
  562. $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
  563. }
  564. $groups = array_map(function(IGroup $group) { return $group->getGID(); }, $groups);
  565. $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
  566. ->andWhere($qb->expr()->in('share_with', $qb->createNamedParameter(
  567. $groups,
  568. IQueryBuilder::PARAM_STR_ARRAY
  569. )))
  570. ->andWhere($qb->expr()->orX(
  571. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  572. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  573. ));
  574. $cursor = $qb->execute();
  575. while($data = $cursor->fetch()) {
  576. if ($offset > 0) {
  577. $offset--;
  578. continue;
  579. }
  580. $shares2[] = $this->createShare($data);
  581. }
  582. $cursor->closeCursor();
  583. }
  584. /*
  585. * Resolve all group shares to user specific shares
  586. * TODO: Optmize this!
  587. */
  588. foreach($shares2 as $share) {
  589. $shares[] = $this->resolveGroupShare($share, $userId);
  590. }
  591. } else {
  592. throw new BackendError('Invalid backend');
  593. }
  594. return $shares;
  595. }
  596. /**
  597. * Get a share by token
  598. *
  599. * @param string $token
  600. * @return \OCP\Share\IShare
  601. * @throws ShareNotFound
  602. */
  603. public function getShareByToken($token) {
  604. $qb = $this->dbConn->getQueryBuilder();
  605. $cursor = $qb->select('*')
  606. ->from('share')
  607. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK)))
  608. ->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token)))
  609. ->andWhere($qb->expr()->orX(
  610. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  611. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  612. ))
  613. ->execute();
  614. $data = $cursor->fetch();
  615. if ($data === false) {
  616. throw new ShareNotFound();
  617. }
  618. try {
  619. $share = $this->createShare($data);
  620. } catch (InvalidShare $e) {
  621. throw new ShareNotFound();
  622. }
  623. return $share;
  624. }
  625. /**
  626. * Create a share object from an database row
  627. *
  628. * @param mixed[] $data
  629. * @return \OCP\Share\IShare
  630. * @throws InvalidShare
  631. */
  632. private function createShare($data) {
  633. $share = new Share($this->rootFolder, $this->userManager);
  634. $share->setId((int)$data['id'])
  635. ->setShareType((int)$data['share_type'])
  636. ->setPermissions((int)$data['permissions'])
  637. ->setTarget($data['file_target'])
  638. ->setMailSend((bool)$data['mail_send']);
  639. $shareTime = new \DateTime();
  640. $shareTime->setTimestamp((int)$data['stime']);
  641. $share->setShareTime($shareTime);
  642. if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
  643. $share->setSharedWith($data['share_with']);
  644. } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
  645. $share->setSharedWith($data['share_with']);
  646. } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
  647. $share->setPassword($data['share_with']);
  648. $share->setToken($data['token']);
  649. }
  650. $share->setSharedBy($data['uid_initiator']);
  651. $share->setShareOwner($data['uid_owner']);
  652. $share->setNodeId((int)$data['file_source']);
  653. $share->setNodeType($data['item_type']);
  654. if ($data['expiration'] !== null) {
  655. $expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']);
  656. $share->setExpirationDate($expiration);
  657. }
  658. $share->setProviderId($this->identifier());
  659. return $share;
  660. }
  661. /**
  662. * Resolve a group share to a user specific share
  663. * Thus if the user moved their group share make sure this is properly reflected here.
  664. *
  665. * @param \OCP\Share\IShare $share
  666. * @param string $userId
  667. * @return Share Returns the updated share if one was found else return the original share.
  668. */
  669. private function resolveGroupShare(\OCP\Share\IShare $share, $userId) {
  670. $qb = $this->dbConn->getQueryBuilder();
  671. $stmt = $qb->select('*')
  672. ->from('share')
  673. ->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
  674. ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
  675. ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)))
  676. ->andWhere($qb->expr()->orX(
  677. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  678. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  679. ))
  680. ->setMaxResults(1)
  681. ->execute();
  682. $data = $stmt->fetch();
  683. $stmt->closeCursor();
  684. if ($data !== false) {
  685. $share->setPermissions((int)$data['permissions']);
  686. $share->setTarget($data['file_target']);
  687. }
  688. return $share;
  689. }
  690. /**
  691. * A user is deleted from the system
  692. * So clean up the relevant shares.
  693. *
  694. * @param string $uid
  695. * @param int $shareType
  696. */
  697. public function userDeleted($uid, $shareType) {
  698. $qb = $this->dbConn->getQueryBuilder();
  699. $qb->delete('share');
  700. if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
  701. /*
  702. * Delete all user shares that are owned by this user
  703. * or that are received by this user
  704. */
  705. $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)));
  706. $qb->andWhere(
  707. $qb->expr()->orX(
  708. $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)),
  709. $qb->expr()->eq('share_with', $qb->createNamedParameter($uid))
  710. )
  711. );
  712. } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
  713. /*
  714. * Delete all group shares that are owned by this user
  715. * Or special user group shares that are received by this user
  716. */
  717. $qb->where(
  718. $qb->expr()->andX(
  719. $qb->expr()->orX(
  720. $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)),
  721. $qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))
  722. ),
  723. $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid))
  724. )
  725. );
  726. $qb->orWhere(
  727. $qb->expr()->andX(
  728. $qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)),
  729. $qb->expr()->eq('share_with', $qb->createNamedParameter($uid))
  730. )
  731. );
  732. } else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) {
  733. /*
  734. * Delete all link shares owned by this user.
  735. * And all link shares initiated by this user (until #22327 is in)
  736. */
  737. $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK)));
  738. $qb->andWhere(
  739. $qb->expr()->orX(
  740. $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)),
  741. $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($uid))
  742. )
  743. );
  744. }
  745. $qb->execute();
  746. }
  747. /**
  748. * Delete all shares received by this group. As well as any custom group
  749. * shares for group members.
  750. *
  751. * @param string $gid
  752. */
  753. public function groupDeleted($gid) {
  754. /*
  755. * First delete all custom group shares for group members
  756. */
  757. $qb = $this->dbConn->getQueryBuilder();
  758. $qb->select('id')
  759. ->from('share')
  760. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
  761. ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid)));
  762. $cursor = $qb->execute();
  763. $ids = [];
  764. while($row = $cursor->fetch()) {
  765. $ids[] = (int)$row['id'];
  766. }
  767. $cursor->closeCursor();
  768. if (!empty($ids)) {
  769. $chunks = array_chunk($ids, 100);
  770. foreach ($chunks as $chunk) {
  771. $qb->delete('share')
  772. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
  773. ->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY)));
  774. $qb->execute();
  775. }
  776. }
  777. /*
  778. * Now delete all the group shares
  779. */
  780. $qb = $this->dbConn->getQueryBuilder();
  781. $qb->delete('share')
  782. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
  783. ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid)));
  784. $qb->execute();
  785. }
  786. /**
  787. * Delete custom group shares to this group for this user
  788. *
  789. * @param string $uid
  790. * @param string $gid
  791. */
  792. public function userDeletedFromGroup($uid, $gid) {
  793. /*
  794. * Get all group shares
  795. */
  796. $qb = $this->dbConn->getQueryBuilder();
  797. $qb->select('id')
  798. ->from('share')
  799. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
  800. ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid)));
  801. $cursor = $qb->execute();
  802. $ids = [];
  803. while($row = $cursor->fetch()) {
  804. $ids[] = (int)$row['id'];
  805. }
  806. $cursor->closeCursor();
  807. if (!empty($ids)) {
  808. $chunks = array_chunk($ids, 100);
  809. foreach ($chunks as $chunk) {
  810. /*
  811. * Delete all special shares wit this users for the found group shares
  812. */
  813. $qb->delete('share')
  814. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
  815. ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($uid)))
  816. ->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY)));
  817. $qb->execute();
  818. }
  819. }
  820. }
  821. }