SharesPlugin.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 OC\Share20\Exception\BackendError;
  31. use OCA\DAV\Connector\Sabre\Node as DavNode;
  32. use OCP\Files\Folder;
  33. use OCP\Files\Node;
  34. use OCP\Files\NotFoundException;
  35. use OCP\IUserSession;
  36. use OCP\Share\IManager;
  37. use OCP\Share\IShare;
  38. use Sabre\DAV\PropFind;
  39. use Sabre\DAV\Server;
  40. use Sabre\DAV\Tree;
  41. /**
  42. * Sabre Plugin to provide share-related properties
  43. */
  44. class SharesPlugin extends \Sabre\DAV\ServerPlugin {
  45. public const NS_OWNCLOUD = 'http://owncloud.org/ns';
  46. public const NS_NEXTCLOUD = 'http://nextcloud.org/ns';
  47. public const SHARETYPES_PROPERTYNAME = '{http://owncloud.org/ns}share-types';
  48. public const SHAREES_PROPERTYNAME = '{http://nextcloud.org/ns}sharees';
  49. /**
  50. * Reference to main server object
  51. *
  52. * @var \Sabre\DAV\Server
  53. */
  54. private $server;
  55. private string $userId;
  56. /** @var IShare[][] */
  57. private array $cachedShares = [];
  58. /** @var string[] */
  59. private array $cachedFolders = [];
  60. public function __construct(
  61. private Tree $tree,
  62. private IUserSession $userSession,
  63. private Folder $userFolder,
  64. private IManager $shareManager,
  65. ) {
  66. $this->userId = $userSession->getUser()->getUID();
  67. }
  68. /**
  69. * This initializes the plugin.
  70. *
  71. * This function is called by \Sabre\DAV\Server, after
  72. * addPlugin is called.
  73. *
  74. * This method should set up the required event subscriptions.
  75. *
  76. * @return void
  77. */
  78. public function initialize(Server $server) {
  79. $server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc';
  80. $server->xml->elementMap[self::SHARETYPES_PROPERTYNAME] = ShareTypeList::class;
  81. $server->protectedProperties[] = self::SHARETYPES_PROPERTYNAME;
  82. $server->protectedProperties[] = self::SHAREES_PROPERTYNAME;
  83. $this->server = $server;
  84. $this->server->on('propFind', [$this, 'handleGetProperties']);
  85. }
  86. /**
  87. * @param Node $node
  88. * @return IShare[]
  89. */
  90. private function getShare(Node $node): array {
  91. $result = [];
  92. $requestedShareTypes = [
  93. IShare::TYPE_USER,
  94. IShare::TYPE_GROUP,
  95. IShare::TYPE_LINK,
  96. IShare::TYPE_REMOTE,
  97. IShare::TYPE_EMAIL,
  98. IShare::TYPE_ROOM,
  99. IShare::TYPE_CIRCLE,
  100. IShare::TYPE_DECK,
  101. IShare::TYPE_SCIENCEMESH,
  102. ];
  103. foreach ($requestedShareTypes as $requestedShareType) {
  104. $result = array_merge($result, $this->shareManager->getSharesBy(
  105. $this->userId,
  106. $requestedShareType,
  107. $node,
  108. false,
  109. -1
  110. ));
  111. // Also check for shares where the user is the recipient
  112. try {
  113. $result = array_merge($result, $this->shareManager->getSharedWith(
  114. $this->userId,
  115. $requestedShareType,
  116. $node,
  117. -1
  118. ));
  119. } catch (BackendError $e) {
  120. // ignore
  121. }
  122. }
  123. return $result;
  124. }
  125. /**
  126. * @param Folder $node
  127. * @return IShare[][]
  128. */
  129. private function getSharesFolder(Folder $node): array {
  130. return $this->shareManager->getSharesInFolder(
  131. $this->userId,
  132. $node,
  133. true
  134. );
  135. }
  136. /**
  137. * @param DavNode $sabreNode
  138. * @return IShare[]
  139. */
  140. private function getShares(DavNode $sabreNode): array {
  141. if (isset($this->cachedShares[$sabreNode->getId()])) {
  142. return $this->cachedShares[$sabreNode->getId()];
  143. }
  144. [$parentPath,] = \Sabre\Uri\split($sabreNode->getPath());
  145. if ($parentPath === '') {
  146. $parentPath = '/';
  147. }
  148. // if we already cached the folder containing this file
  149. // then we already know there are no shares here.
  150. if (array_search($parentPath, $this->cachedFolders) === false) {
  151. try {
  152. $node = $sabreNode->getNode();
  153. } catch (NotFoundException $e) {
  154. return [];
  155. }
  156. $shares = $this->getShare($node);
  157. $this->cachedShares[$sabreNode->getId()] = $shares;
  158. return $shares;
  159. }
  160. return [];
  161. }
  162. /**
  163. * Adds shares to propfind response
  164. *
  165. * @param PropFind $propFind propfind object
  166. * @param \Sabre\DAV\INode $sabreNode sabre node
  167. */
  168. public function handleGetProperties(
  169. PropFind $propFind,
  170. \Sabre\DAV\INode $sabreNode
  171. ) {
  172. if (!($sabreNode instanceof DavNode)) {
  173. return;
  174. }
  175. // If the node is a directory and we are requesting share types or sharees
  176. // then we get all the shares in the folder and cache them.
  177. // This is more performant than iterating each files afterwards.
  178. if ($sabreNode instanceof Directory
  179. && $propFind->getDepth() !== 0
  180. && (
  181. !is_null($propFind->getStatus(self::SHARETYPES_PROPERTYNAME)) ||
  182. !is_null($propFind->getStatus(self::SHAREES_PROPERTYNAME))
  183. )
  184. ) {
  185. $folderNode = $sabreNode->getNode();
  186. $this->cachedFolders[] = $sabreNode->getPath();
  187. $childShares = $this->getSharesFolder($folderNode);
  188. foreach ($childShares as $id => $shares) {
  189. $this->cachedShares[$id] = $shares;
  190. }
  191. }
  192. $propFind->handle(self::SHARETYPES_PROPERTYNAME, function () use ($sabreNode): ShareTypeList {
  193. $shares = $this->getShares($sabreNode);
  194. $shareTypes = array_unique(array_map(function (IShare $share) {
  195. return $share->getShareType();
  196. }, $shares));
  197. return new ShareTypeList($shareTypes);
  198. });
  199. $propFind->handle(self::SHAREES_PROPERTYNAME, function () use ($sabreNode): ShareeList {
  200. $shares = $this->getShares($sabreNode);
  201. return new ShareeList($shares);
  202. });
  203. }
  204. }