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.

219 lines
5.5 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  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
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Share20;
  25. use OCP\Files\InvalidPathException;
  26. use OCP\Files\Node;
  27. use OCP\Files\NotFoundException;
  28. use OCP\Files\NotPermittedException;
  29. use OCP\Share\IManager;
  30. use OCP\Share\IShareHelper;
  31. class ShareHelper implements IShareHelper {
  32. /** @var IManager */
  33. private $shareManager;
  34. public function __construct(IManager $shareManager) {
  35. $this->shareManager = $shareManager;
  36. }
  37. /**
  38. * @param Node $node
  39. * @return array [ users => [Mapping $uid => $pathForUser], remotes => [Mapping $cloudId => $pathToMountRoot]]
  40. */
  41. public function getPathsForAccessList(Node $node) {
  42. $result = [
  43. 'users' => [],
  44. 'remotes' => [],
  45. ];
  46. $accessList = $this->shareManager->getAccessList($node, true, true);
  47. if (!empty($accessList['users'])) {
  48. $result['users'] = $this->getPathsForUsers($node, $accessList['users']);
  49. }
  50. if (!empty($accessList['remote'])) {
  51. $result['remotes'] = $this->getPathsForRemotes($node, $accessList['remote']);
  52. }
  53. return $result;
  54. }
  55. /**
  56. * Sample:
  57. * $users = [
  58. * 'test1' => ['node_id' => 16, 'node_path' => '/foo'],
  59. * 'test2' => ['node_id' => 23, 'node_path' => '/bar'],
  60. * 'test3' => ['node_id' => 42, 'node_path' => '/cat'],
  61. * 'test4' => ['node_id' => 48, 'node_path' => '/dog'],
  62. * ];
  63. *
  64. * Node tree:
  65. * - SixTeen is the parent of TwentyThree
  66. * - TwentyThree is the parent of FortyTwo
  67. * - FortyEight does not exist
  68. *
  69. * $return = [
  70. * 'test1' => '/foo/TwentyThree/FortyTwo',
  71. * 'test2' => '/bar/FortyTwo',
  72. * 'test3' => '/cat',
  73. * ],
  74. *
  75. * @param Node $node
  76. * @param array[] $users
  77. * @return array
  78. */
  79. protected function getPathsForUsers(Node $node, array $users) {
  80. /** @var array[] $byId */
  81. $byId = [];
  82. /** @var array[] $results */
  83. $results = [];
  84. foreach ($users as $uid => $info) {
  85. if (!isset($byId[$info['node_id']])) {
  86. $byId[$info['node_id']] = [];
  87. }
  88. $byId[$info['node_id']][$uid] = $info['node_path'];
  89. }
  90. try {
  91. if (isset($byId[$node->getId()])) {
  92. foreach ($byId[$node->getId()] as $uid => $path) {
  93. $results[$uid] = $path;
  94. }
  95. unset($byId[$node->getId()]);
  96. }
  97. } catch (NotFoundException $e) {
  98. return $results;
  99. } catch (InvalidPathException $e) {
  100. return $results;
  101. }
  102. if (empty($byId)) {
  103. return $results;
  104. }
  105. $item = $node;
  106. $appendix = '/' . $node->getName();
  107. while (!empty($byId)) {
  108. try {
  109. /** @var Node $item */
  110. $item = $item->getParent();
  111. if (!empty($byId[$item->getId()])) {
  112. foreach ($byId[$item->getId()] as $uid => $path) {
  113. $results[$uid] = $path . $appendix;
  114. }
  115. unset($byId[$item->getId()]);
  116. }
  117. $appendix = '/' . $item->getName() . $appendix;
  118. } catch (NotFoundException $e) {
  119. return $results;
  120. } catch (InvalidPathException $e) {
  121. return $results;
  122. } catch (NotPermittedException $e) {
  123. return $results;
  124. }
  125. }
  126. return $results;
  127. }
  128. /**
  129. * Sample:
  130. * $remotes = [
  131. * 'test1' => ['node_id' => 16, 'token' => 't1'],
  132. * 'test2' => ['node_id' => 23, 'token' => 't2'],
  133. * 'test3' => ['node_id' => 42, 'token' => 't3'],
  134. * 'test4' => ['node_id' => 48, 'token' => 't4'],
  135. * ];
  136. *
  137. * Node tree:
  138. * - SixTeen is the parent of TwentyThree
  139. * - TwentyThree is the parent of FortyTwo
  140. * - FortyEight does not exist
  141. *
  142. * $return = [
  143. * 'test1' => ['token' => 't1', 'node_path' => '/SixTeen'],
  144. * 'test2' => ['token' => 't2', 'node_path' => '/SixTeen/TwentyThree'],
  145. * 'test3' => ['token' => 't3', 'node_path' => '/SixTeen/TwentyThree/FortyTwo'],
  146. * ],
  147. *
  148. * @param Node $node
  149. * @param array[] $remotes
  150. * @return array
  151. */
  152. protected function getPathsForRemotes(Node $node, array $remotes) {
  153. /** @var array[] $byId */
  154. $byId = [];
  155. /** @var array[] $results */
  156. $results = [];
  157. foreach ($remotes as $cloudId => $info) {
  158. if (!isset($byId[$info['node_id']])) {
  159. $byId[$info['node_id']] = [];
  160. }
  161. $byId[$info['node_id']][$cloudId] = $info['token'];
  162. }
  163. $item = $node;
  164. while (!empty($byId)) {
  165. try {
  166. if (!empty($byId[$item->getId()])) {
  167. $path = $this->getMountedPath($item);
  168. foreach ($byId[$item->getId()] as $uid => $token) {
  169. $results[$uid] = [
  170. 'node_path' => $path,
  171. 'token' => $token,
  172. ];
  173. }
  174. unset($byId[$item->getId()]);
  175. }
  176. /** @var Node $item */
  177. $item = $item->getParent();
  178. } catch (NotFoundException $e) {
  179. return $results;
  180. } catch (InvalidPathException $e) {
  181. return $results;
  182. } catch (NotPermittedException $e) {
  183. return $results;
  184. }
  185. }
  186. return $results;
  187. }
  188. /**
  189. * @param Node $node
  190. * @return string
  191. */
  192. protected function getMountedPath(Node $node) {
  193. $path = $node->getPath();
  194. $sections = explode('/', $path, 4);
  195. return '/' . $sections[3];
  196. }
  197. }