-
Notifications
You must be signed in to change notification settings - Fork 87
/
ACLStorageWrapper.php
295 lines (251 loc) Β· 9.34 KB
/
ACLStorageWrapper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php declare(strict_types=1);
/**
* @copyright Copyright (c) 2019 Robin Appelman <robin@icewind.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\GroupFolders\ACL;
use Icewind\Streams\IteratorDirectory;
use OC\Files\Storage\Wrapper\Wrapper;
use OCP\Constants;
class ACLStorageWrapper extends Wrapper {
/** @var ACLManager */
private $aclManager;
/** @var bool */
private $inShare;
public function __construct($arguments) {
parent::__construct($arguments);
$this->aclManager = $arguments['acl_manager'];
$this->inShare = $arguments['in_share'];
}
private function getACLPermissionsForPath(string $path) {
$permissions = $this->aclManager->getACLPermissionsForPath($path);
// if there is no read permissions, than deny everything
if ($this->inShare) {
$canRead = $permissions & (Constants::PERMISSION_READ + Constants::PERMISSION_SHARE);
} else {
$canRead = $permissions & Constants::PERMISSION_READ;
}
return $canRead ? $permissions : 0;
}
private function checkPermissions(string $path, int $permissions) {
return ($this->getACLPermissionsForPath($path) & $permissions) === $permissions;
}
public function isReadable($path) {
return $this->checkPermissions($path, Constants::PERMISSION_READ) && parent::isReadable($path);
}
public function isUpdatable($path) {
return $this->checkPermissions($path, Constants::PERMISSION_UPDATE) && parent::isUpdatable($path);
}
public function isCreatable($path) {
return $this->checkPermissions($path, Constants::PERMISSION_CREATE) && parent::isCreatable($path);
}
public function isDeletable($path) {
return $this->checkPermissions($path, Constants::PERMISSION_DELETE)
&& $this->canDeleteTree($path)
&& parent::isDeletable($path);
}
public function isSharable($path) {
return $this->checkPermissions($path, Constants::PERMISSION_SHARE) && parent::isSharable($path);
}
public function getPermissions($path) {
return $this->storage->getPermissions($path) & $this->getACLPermissionsForPath($path);
}
public function rename($path1, $path2) {
if (strpos($path1, $path2) === 0) {
$part = substr($path1, strlen($path2));
//This is a rename of the transfer file to the original file
if (strpos($part, '.ocTransferId') === 0) {
return $this->checkPermissions($path2, Constants::PERMISSION_CREATE) && parent::rename($path1, $path2);
}
}
$permissions = $this->file_exists($path2) ? Constants::PERMISSION_UPDATE : Constants::PERMISSION_CREATE;
$sourceParent = dirname($path1);
if ($sourceParent === '.') {
$sourceParent = '';
}
return $this->checkPermissions($sourceParent, Constants::PERMISSION_DELETE) &&
$this->checkPermissions($path1, Constants::PERMISSION_UPDATE & Constants::PERMISSION_READ) &&
$this->checkPermissions($path2, $permissions) &&
parent::rename($path1, $path2);
}
public function opendir($path) {
if (!$this->checkPermissions($path, Constants::PERMISSION_READ)) {
return false;
}
$handle = parent::opendir($path);
$items = [];
while ($file = readdir($handle)) {
if ($file !== '.' && $file !== '..') {
if ($this->checkPermissions(trim($path . '/' . $file, '/'), Constants::PERMISSION_READ)) {
$items[] = $file;
}
}
}
return IteratorDirectory::wrap($items);
}
public function copy($path1, $path2) {
$permissions = $this->file_exists($path2) ? Constants::PERMISSION_UPDATE : Constants::PERMISSION_CREATE;
return $this->checkPermissions($path2, $permissions) &&
$this->checkPermissions($path1, Constants::PERMISSION_READ) &&
parent::copy($path1, $path2);
}
public function touch($path, $mtime = null) {
$permissions = $this->file_exists($path) ? Constants::PERMISSION_UPDATE : Constants::PERMISSION_CREATE;
return $this->checkPermissions($path, $permissions) && parent::touch($path, $mtime);
}
public function mkdir($path) {
return $this->checkPermissions($path, Constants::PERMISSION_CREATE) && parent::mkdir($path);
}
public function rmdir($path) {
return $this->checkPermissions($path, Constants::PERMISSION_DELETE)
&& $this->canDeleteTree($path)
&& parent::rmdir($path);
}
public function unlink($path) {
return $this->checkPermissions($path, Constants::PERMISSION_DELETE)
&& $this->canDeleteTree($path)
&& parent::unlink($path);
}
/**
* When deleting we need to ensure that there is no file inside the folder being deleted that misses delete permissions
* This check is fairly expensive so we only do it for the actual delete and not metadata operations
*
* @param string $path
* @return int
*/
private function canDeleteTree(string $path): int {
return $this->aclManager->getPermissionsForTree($path) & Constants::PERMISSION_DELETE;
}
public function file_put_contents($path, $data) {
$permissions = $this->file_exists($path) ? Constants::PERMISSION_UPDATE : Constants::PERMISSION_CREATE;
return $this->checkPermissions($path, $permissions) ? parent::file_put_contents($path, $data) : false;
}
public function fopen($path, $mode) {
if ($mode === 'r' or $mode === 'rb') {
$permissions = Constants::PERMISSION_READ;
} else {
$permissions = $this->file_exists($path) ? Constants::PERMISSION_UPDATE : Constants::PERMISSION_CREATE;
}
return $this->checkPermissions($path, $permissions) ? parent::fopen($path, $mode) : false;
}
public function writeStream(string $path, $stream, int $size = null): int {
$permissions = $this->file_exists($path) ? Constants::PERMISSION_UPDATE : Constants::PERMISSION_CREATE;
return $this->checkPermissions($path, $permissions) ? parent::writeStream($path, $stream, $size) : 0;
}
/**
* get a cache instance for the storage
*
* @param string $path
* @param \OC\Files\Storage\Storage (optional) the storage to pass to the cache
* @return \OC\Files\Cache\Cache
*/
public function getCache($path = '', $storage = null) {
if (!$storage) {
$storage = $this;
}
$sourceCache = parent::getCache($path, $storage);
return new ACLCacheWrapper($sourceCache, $this->aclManager, $this->inShare);
}
public function getMetaData($path) {
$data = parent::getMetaData($path);
if ($data && isset($data['permissions'])) {
$data['scan_permissions'] = isset($data['scan_permissions']) ? $data['scan_permissions'] : $data['permissions'];
$data['permissions'] &= $this->getACLPermissionsForPath($path);
}
return $data;
}
public function getScanner($path = '', $storage = null) {
if (!$storage) {
$storage = $this->storage;
}
return parent::getScanner($path, $storage);
}
public function is_dir($path) {
return $this->checkPermissions($path, Constants::PERMISSION_READ) &&
parent::is_dir($path);
}
public function is_file($path) {
return $this->checkPermissions($path, Constants::PERMISSION_READ) &&
parent::is_file($path);
}
public function stat($path) {
if (!$this->checkPermissions($path, Constants::PERMISSION_READ)) {
return false;
}
return parent::stat($path);
}
public function filetype($path) {
if (!$this->checkPermissions($path, Constants::PERMISSION_READ)) {
return false;
}
return parent::filetype($path);
}
public function filesize($path) {
if (!$this->checkPermissions($path, Constants::PERMISSION_READ)) {
return false;
}
return parent::filesize($path);
}
public function file_exists($path) {
return $this->checkPermissions($path, Constants::PERMISSION_READ) &&
parent::file_exists($path);
}
public function filemtime($path) {
if (!$this->checkPermissions($path, Constants::PERMISSION_READ)) {
return false;
}
parent::filemtime($path);
}
public function file_get_contents($path) {
if (!$this->checkPermissions($path, Constants::PERMISSION_READ)) {
return false;
}
return parent::file_get_contents($path);
}
public function getMimeType($path) {
if (!$this->checkPermissions($path, Constants::PERMISSION_READ)) {
return false;
}
return parent::getMimeType($path);
}
public function hash($type, $path, $raw = false) {
if (!$this->checkPermissions($path, Constants::PERMISSION_READ)) {
return false;
}
parent::hash($type, $path, $raw);
}
public function getETag($path) {
if (!$this->checkPermissions($path, Constants::PERMISSION_READ)) {
return false;
}
return parent::getETag($path);
}
public function getDirectDownload($path) {
if (!$this->checkPermissions($path, Constants::PERMISSION_READ)) {
return false;
}
return parent::getDirectDownload($path);
}
public function getDirectoryContent($directory): \Traversable {
foreach ($this->getWrapperStorage()->getDirectoryContent($directory) as $data) {
$data['scan_permissions'] = isset($data['scan_permissions']) ? $data['scan_permissions'] : $data['permissions'];
$data['permissions'] &= $this->getACLPermissionsForPath(rtrim($directory, '/') . '/' . $data['name']);
yield $data;
}
}
}