1
0

ShareHelper.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Share20;
  7. use OCP\Files\InvalidPathException;
  8. use OCP\Files\Node;
  9. use OCP\Files\NotFoundException;
  10. use OCP\Files\NotPermittedException;
  11. use OCP\Share\IManager;
  12. use OCP\Share\IShareHelper;
  13. class ShareHelper implements IShareHelper {
  14. /** @var IManager */
  15. private $shareManager;
  16. public function __construct(IManager $shareManager) {
  17. $this->shareManager = $shareManager;
  18. }
  19. /**
  20. * @param Node $node
  21. * @return array [ users => [Mapping $uid => $pathForUser], remotes => [Mapping $cloudId => $pathToMountRoot]]
  22. */
  23. public function getPathsForAccessList(Node $node) {
  24. $result = [
  25. 'users' => [],
  26. 'remotes' => [],
  27. ];
  28. $accessList = $this->shareManager->getAccessList($node, true, true);
  29. if (!empty($accessList['users'])) {
  30. $result['users'] = $this->getPathsForUsers($node, $accessList['users']);
  31. }
  32. if (!empty($accessList['remote'])) {
  33. $result['remotes'] = $this->getPathsForRemotes($node, $accessList['remote']);
  34. }
  35. return $result;
  36. }
  37. /**
  38. * Sample:
  39. * $users = [
  40. * 'test1' => ['node_id' => 16, 'node_path' => '/foo'],
  41. * 'test2' => ['node_id' => 23, 'node_path' => '/bar'],
  42. * 'test3' => ['node_id' => 42, 'node_path' => '/cat'],
  43. * 'test4' => ['node_id' => 48, 'node_path' => '/dog'],
  44. * ];
  45. *
  46. * Node tree:
  47. * - SixTeen is the parent of TwentyThree
  48. * - TwentyThree is the parent of FortyTwo
  49. * - FortyEight does not exist
  50. *
  51. * $return = [
  52. * 'test1' => '/foo/TwentyThree/FortyTwo',
  53. * 'test2' => '/bar/FortyTwo',
  54. * 'test3' => '/cat',
  55. * ],
  56. *
  57. * @param Node $node
  58. * @param array[] $users
  59. * @return array
  60. */
  61. protected function getPathsForUsers(Node $node, array $users) {
  62. /** @var array[] $byId */
  63. $byId = [];
  64. /** @var array[] $results */
  65. $results = [];
  66. foreach ($users as $uid => $info) {
  67. if (!isset($byId[$info['node_id']])) {
  68. $byId[$info['node_id']] = [];
  69. }
  70. $byId[$info['node_id']][$uid] = $info['node_path'];
  71. }
  72. try {
  73. if (isset($byId[$node->getId()])) {
  74. foreach ($byId[$node->getId()] as $uid => $path) {
  75. $results[$uid] = $path;
  76. }
  77. unset($byId[$node->getId()]);
  78. }
  79. } catch (NotFoundException $e) {
  80. return $results;
  81. } catch (InvalidPathException $e) {
  82. return $results;
  83. }
  84. if (empty($byId)) {
  85. return $results;
  86. }
  87. $item = $node;
  88. $appendix = '/' . $node->getName();
  89. while (!empty($byId)) {
  90. try {
  91. /** @var Node $item */
  92. $item = $item->getParent();
  93. if (!empty($byId[$item->getId()])) {
  94. foreach ($byId[$item->getId()] as $uid => $path) {
  95. $results[$uid] = $path . $appendix;
  96. }
  97. unset($byId[$item->getId()]);
  98. }
  99. $appendix = '/' . $item->getName() . $appendix;
  100. } catch (NotFoundException $e) {
  101. return $results;
  102. } catch (InvalidPathException $e) {
  103. return $results;
  104. } catch (NotPermittedException $e) {
  105. return $results;
  106. }
  107. }
  108. return $results;
  109. }
  110. /**
  111. * Sample:
  112. * $remotes = [
  113. * 'test1' => ['node_id' => 16, 'token' => 't1'],
  114. * 'test2' => ['node_id' => 23, 'token' => 't2'],
  115. * 'test3' => ['node_id' => 42, 'token' => 't3'],
  116. * 'test4' => ['node_id' => 48, 'token' => 't4'],
  117. * ];
  118. *
  119. * Node tree:
  120. * - SixTeen is the parent of TwentyThree
  121. * - TwentyThree is the parent of FortyTwo
  122. * - FortyEight does not exist
  123. *
  124. * $return = [
  125. * 'test1' => ['token' => 't1', 'node_path' => '/SixTeen'],
  126. * 'test2' => ['token' => 't2', 'node_path' => '/SixTeen/TwentyThree'],
  127. * 'test3' => ['token' => 't3', 'node_path' => '/SixTeen/TwentyThree/FortyTwo'],
  128. * ],
  129. *
  130. * @param Node $node
  131. * @param array[] $remotes
  132. * @return array
  133. */
  134. protected function getPathsForRemotes(Node $node, array $remotes) {
  135. /** @var array[] $byId */
  136. $byId = [];
  137. /** @var array[] $results */
  138. $results = [];
  139. foreach ($remotes as $cloudId => $info) {
  140. if (!isset($byId[$info['node_id']])) {
  141. $byId[$info['node_id']] = [];
  142. }
  143. $byId[$info['node_id']][$cloudId] = $info['token'];
  144. }
  145. $item = $node;
  146. while (!empty($byId)) {
  147. try {
  148. if (!empty($byId[$item->getId()])) {
  149. $path = $this->getMountedPath($item);
  150. foreach ($byId[$item->getId()] as $uid => $token) {
  151. $results[$uid] = [
  152. 'node_path' => $path,
  153. 'token' => $token,
  154. ];
  155. }
  156. unset($byId[$item->getId()]);
  157. }
  158. /** @var Node $item */
  159. $item = $item->getParent();
  160. } catch (NotFoundException $e) {
  161. return $results;
  162. } catch (InvalidPathException $e) {
  163. return $results;
  164. } catch (NotPermittedException $e) {
  165. return $results;
  166. }
  167. }
  168. return $results;
  169. }
  170. /**
  171. * @param Node $node
  172. * @return string
  173. */
  174. protected function getMountedPath(Node $node) {
  175. $path = $node->getPath();
  176. $sections = explode('/', $path, 4);
  177. return '/' . $sections[3];
  178. }
  179. }