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.

SharesPlugin.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Tobias Kaminsky <tobias@kaminsky.me>
  12. * @author Vincent Petry <vincent@nextcloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\DAV\Connector\Sabre;
  30. use OCA\DAV\Connector\Sabre\Node as DavNode;
  31. use OCP\Files\Folder;
  32. use OCP\Files\Node;
  33. use OCP\Files\NotFoundException;
  34. use OCP\IUserSession;
  35. use OCP\Share\IManager;
  36. use OCP\Share\IShare;
  37. use Sabre\DAV\PropFind;
  38. use Sabre\DAV\Server;
  39. use Sabre\DAV\Tree;
  40. /**
  41. * Sabre Plugin to provide share-related properties
  42. */
  43. class SharesPlugin extends \Sabre\DAV\ServerPlugin {
  44. public const NS_OWNCLOUD = 'http://owncloud.org/ns';
  45. public const NS_NEXTCLOUD = 'http://nextcloud.org/ns';
  46. public const SHARETYPES_PROPERTYNAME = '{http://owncloud.org/ns}share-types';
  47. public const SHAREES_PROPERTYNAME = '{http://nextcloud.org/ns}sharees';
  48. /**
  49. * Reference to main server object
  50. *
  51. * @var \Sabre\DAV\Server
  52. */
  53. private $server;
  54. private IManager $shareManager;
  55. private Tree $tree;
  56. private string $userId;
  57. private Folder $userFolder;
  58. /** @var IShare[][] */
  59. private array $cachedShares = [];
  60. /** @var string[] */
  61. private array $cachedFolders = [];
  62. public function __construct(
  63. Tree $tree,
  64. IUserSession $userSession,
  65. Folder $userFolder,
  66. IManager $shareManager
  67. ) {
  68. $this->tree = $tree;
  69. $this->shareManager = $shareManager;
  70. $this->userFolder = $userFolder;
  71. $this->userId = $userSession->getUser()->getUID();
  72. }
  73. /**
  74. * This initializes the plugin.
  75. *
  76. * This function is called by \Sabre\DAV\Server, after
  77. * addPlugin is called.
  78. *
  79. * This method should set up the required event subscriptions.
  80. *
  81. * @return void
  82. */
  83. public function initialize(Server $server) {
  84. $server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc';
  85. $server->xml->elementMap[self::SHARETYPES_PROPERTYNAME] = ShareTypeList::class;
  86. $server->protectedProperties[] = self::SHARETYPES_PROPERTYNAME;
  87. $server->protectedProperties[] = self::SHAREES_PROPERTYNAME;
  88. $this->server = $server;
  89. $this->server->on('propFind', [$this, 'handleGetProperties']);
  90. }
  91. /**
  92. * @param Node $node
  93. * @return IShare[]
  94. */
  95. private function getShare(Node $node): array {
  96. $result = [];
  97. $requestedShareTypes = [
  98. IShare::TYPE_USER,
  99. IShare::TYPE_GROUP,
  100. IShare::TYPE_LINK,
  101. IShare::TYPE_REMOTE,
  102. IShare::TYPE_EMAIL,
  103. IShare::TYPE_ROOM,
  104. IShare::TYPE_CIRCLE,
  105. IShare::TYPE_DECK,
  106. IShare::TYPE_SCIENCEMESH,
  107. ];
  108. foreach ($requestedShareTypes as $requestedShareType) {
  109. $shares = $this->shareManager->getSharesBy(
  110. $this->userId,
  111. $requestedShareType,
  112. $node,
  113. false,
  114. -1
  115. );
  116. foreach ($shares as $share) {
  117. $result[] = $share;
  118. }
  119. }
  120. return $result;
  121. }
  122. /**
  123. * @param Folder $node
  124. * @return IShare[][]
  125. */
  126. private function getSharesFolder(Folder $node): array {
  127. return $this->shareManager->getSharesInFolder(
  128. $this->userId,
  129. $node,
  130. true
  131. );
  132. }
  133. /**
  134. * @param DavNode $sabreNode
  135. * @return IShare[]
  136. */
  137. private function getShares(DavNode $sabreNode): array {
  138. if (isset($this->cachedShares[$sabreNode->getId()])) {
  139. $shares = $this->cachedShares[$sabreNode->getId()];
  140. } else {
  141. [$parentPath,] = \Sabre\Uri\split($sabreNode->getPath());
  142. if ($parentPath === '') {
  143. $parentPath = '/';
  144. }
  145. // if we already cached the folder this file is in we know there are no shares for this file
  146. if (array_search($parentPath, $this->cachedFolders) === false) {
  147. try {
  148. $node = $sabreNode->getNode();
  149. } catch (NotFoundException $e) {
  150. return [];
  151. }
  152. $shares = $this->getShare($node);
  153. $this->cachedShares[$sabreNode->getId()] = $shares;
  154. } else {
  155. return [];
  156. }
  157. }
  158. return $shares;
  159. }
  160. /**
  161. * Adds shares to propfind response
  162. *
  163. * @param PropFind $propFind propfind object
  164. * @param \Sabre\DAV\INode $sabreNode sabre node
  165. */
  166. public function handleGetProperties(
  167. PropFind $propFind,
  168. \Sabre\DAV\INode $sabreNode
  169. ) {
  170. if (!($sabreNode instanceof DavNode)) {
  171. return;
  172. }
  173. // need prefetch ?
  174. if ($sabreNode instanceof Directory
  175. && $propFind->getDepth() !== 0
  176. && (
  177. !is_null($propFind->getStatus(self::SHARETYPES_PROPERTYNAME)) ||
  178. !is_null($propFind->getStatus(self::SHAREES_PROPERTYNAME))
  179. )
  180. ) {
  181. $folderNode = $sabreNode->getNode();
  182. $this->cachedFolders[] = $sabreNode->getPath();
  183. $childShares = $this->getSharesFolder($folderNode);
  184. foreach ($childShares as $id => $shares) {
  185. $this->cachedShares[$id] = $shares;
  186. }
  187. }
  188. $propFind->handle(self::SHARETYPES_PROPERTYNAME, function () use ($sabreNode): ShareTypeList {
  189. $shares = $this->getShares($sabreNode);
  190. $shareTypes = array_unique(array_map(function (IShare $share) {
  191. return $share->getShareType();
  192. }, $shares));
  193. return new ShareTypeList($shareTypes);
  194. });
  195. $propFind->handle(self::SHAREES_PROPERTYNAME, function () use ($sabreNode): ShareeList {
  196. $shares = $this->getShares($sabreNode);
  197. return new ShareeList($shares);
  198. });
  199. }
  200. }