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