-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathDirPermissionsMask.php
More file actions
193 lines (152 loc) · 4.42 KB
/
DirPermissionsMask.php
File metadata and controls
193 lines (152 loc) · 4.42 KB
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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only AND (AGPL-3.0-or-later OR AGPL-3.0-only)
*/
namespace OC\Files\Storage\Wrapper;
use OC\Files\Cache\Wrapper\CacheDirPermissionsMask;
use OC\Files\Storage\Storage;
use OCP\Files\Cache\ICache;
/**
* While PermissionMask can mask a whole storage this can
* mask a certain directory inside a storage
*/
class DirPermissionsMask extends PermissionsMask {
/**
* @var string the dir that should be masked
*/
private readonly string $path;
/**
* @var int remember length
*/
private readonly int $pathLength;
/**
* @param array{storage: Storage, mask: int, path: string, ...} $parameters
* @psalm-suppress MoreSpecificImplementedParamType
*
* $storage: The storage the permissions mask should be applied on
* $mask: The permission bits that should be kept, a combination of the \OCP\Constant::PERMISSION_ constants
* $path: The path relative to the storage root that should be masked
*/
public function __construct($parameters) {
parent::__construct($parameters);
$this->path = rtrim((string)$parameters['path'], '/');
$this->pathLength = strlen((string)$parameters['path']);
}
protected function checkPath(string $path): bool {
return $path === $this->path || substr($path, 0, $this->pathLength + 1) === $this->path . '/';
}
public function isUpdatable($path): bool {
if ($this->checkPath($path)) {
return parent::isUpdatable($path);
}
return $this->storage->isUpdatable($path);
}
public function isCreatable($path): bool {
if ($this->checkPath($path)) {
return parent::isCreatable($path);
}
return $this->storage->isCreatable($path);
}
public function isDeletable($path): bool {
if ($this->checkPath($path)) {
return parent::isDeletable($path);
}
return $this->storage->isDeletable($path);
}
public function isSharable($path): bool {
if ($this->checkPath($path)) {
return parent::isSharable($path);
}
return $this->storage->isSharable($path);
}
public function getPermissions($path): int {
if ($this->checkPath($path)) {
return parent::getPermissions($path);
}
return $this->storage->getPermissions($path);
}
public function rename($source, $target): bool {
if (!$this->isUpdatable($source)) {
return false;
}
if ($this->file_exists($target)) {
if ($this->isUpdatable($target)) {
return $this->storage->rename($source, $target);
}
} else {
$parent = dirname($target);
if ($parent === '.') {
$parent = '';
}
if ($this->isCreatable($parent)) {
return $this->storage->rename($source, $target);
}
}
return false;
}
public function copy($source, $target): bool {
if (!$this->isReadable($source)) {
return false;
}
if ($this->file_exists($target)) {
if ($this->isUpdatable($target)) {
return $this->storage->copy($source, $target);
}
} else {
$parent = dirname($target);
if ($parent === '.') {
$parent = '';
}
if ($this->isCreatable($parent)) {
return $this->storage->copy($source, $target);
}
}
return false;
}
public function touch($path, $mtime = null): bool {
if ($this->checkPath($path)) {
return parent::touch($path);
}
return $this->storage->touch($path);
}
public function mkdir($path): bool {
// Always allow creating the path of the dir mask.
if ($path !== $this->path && $this->checkPath($path)) {
return parent::mkdir($path);
}
return $this->storage->mkdir($path);
}
public function rmdir($path): bool {
if ($this->checkPath($path)) {
return parent::rmdir($path);
}
return $this->storage->rmdir($path);
}
public function unlink($path): bool {
if ($this->checkPath($path)) {
return parent::unlink($path);
}
return $this->storage->unlink($path);
}
public function file_put_contents($path, $data): int|float|false {
if ($this->checkPath($path)) {
return parent::file_put_contents($path, $data);
}
return $this->storage->file_put_contents($path, $data);
}
public function fopen($path, $mode) {
if ($this->checkPath($path)) {
return parent::fopen($path, $mode);
}
return $this->storage->fopen($path, $mode);
}
public function getCache($path = '', $storage = null): ICache {
if (!$storage) {
$storage = $this;
}
$sourceCache = $this->storage->getCache($path, $storage);
return new CacheDirPermissionsMask($sourceCache, $this->mask, $this->checkPath(...));
}
}