|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-only AND (AGPL-3.0-or-later OR AGPL-3.0-only) |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OC\Files\Storage\Wrapper; |
| 11 | + |
| 12 | +use OC\Files\Cache\Wrapper\CacheDirPermissionsMask; |
| 13 | +use OC\Files\Storage\Storage; |
| 14 | +use OCP\Files\Cache\ICache; |
| 15 | + |
| 16 | +/** |
| 17 | + * While PermissionMask can mask a whole storage this can |
| 18 | + * mask a certain directory inside a storage |
| 19 | + */ |
| 20 | +class DirPermissionsMask extends PermissionsMask { |
| 21 | + |
| 22 | + /** |
| 23 | + * @var string the dir that should be masked |
| 24 | + */ |
| 25 | + private readonly string $path; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var int remember length |
| 29 | + */ |
| 30 | + private readonly int $pathLength; |
| 31 | + |
| 32 | + /** |
| 33 | + * @param array{storage: Storage, mask: int, path: string, ...} $parameters |
| 34 | + * @psalm-suppress MoreSpecificImplementedParamType |
| 35 | + * |
| 36 | + * $storage: The storage the permissions mask should be applied on |
| 37 | + * $mask: The permission bits that should be kept, a combination of the \OCP\Constant::PERMISSION_ constants |
| 38 | + * $path: The path relative to the storage root that should be masked |
| 39 | + */ |
| 40 | + public function __construct($parameters) { |
| 41 | + parent::__construct($parameters); |
| 42 | + $this->path = rtrim((string)$parameters['path'], '/'); |
| 43 | + $this->pathLength = strlen((string)$parameters['path']); |
| 44 | + } |
| 45 | + |
| 46 | + protected function checkPath(string $path): bool { |
| 47 | + return $path === $this->path || substr($path, 0, $this->pathLength + 1) === $this->path . '/'; |
| 48 | + } |
| 49 | + |
| 50 | + public function isUpdatable($path): bool { |
| 51 | + if ($this->checkPath($path)) { |
| 52 | + return parent::isUpdatable($path); |
| 53 | + } |
| 54 | + |
| 55 | + return $this->storage->isUpdatable($path); |
| 56 | + } |
| 57 | + |
| 58 | + public function isCreatable($path): bool { |
| 59 | + if ($this->checkPath($path)) { |
| 60 | + return parent::isCreatable($path); |
| 61 | + } |
| 62 | + |
| 63 | + return $this->storage->isCreatable($path); |
| 64 | + } |
| 65 | + |
| 66 | + public function isDeletable($path): bool { |
| 67 | + if ($this->checkPath($path)) { |
| 68 | + return parent::isDeletable($path); |
| 69 | + } |
| 70 | + |
| 71 | + return $this->storage->isDeletable($path); |
| 72 | + } |
| 73 | + |
| 74 | + public function isSharable($path): bool { |
| 75 | + if ($this->checkPath($path)) { |
| 76 | + return parent::isSharable($path); |
| 77 | + } |
| 78 | + |
| 79 | + return $this->storage->isSharable($path); |
| 80 | + } |
| 81 | + |
| 82 | + public function getPermissions($path): int { |
| 83 | + if ($this->checkPath($path)) { |
| 84 | + return parent::getPermissions($path); |
| 85 | + } |
| 86 | + |
| 87 | + return $this->storage->getPermissions($path); |
| 88 | + } |
| 89 | + |
| 90 | + public function rename($source, $target): bool { |
| 91 | + if (!$this->isUpdatable($source)) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + if ($this->file_exists($target)) { |
| 96 | + if ($this->isUpdatable($target)) { |
| 97 | + return $this->storage->rename($source, $target); |
| 98 | + } |
| 99 | + } else { |
| 100 | + $parent = dirname($target); |
| 101 | + if ($parent === '.') { |
| 102 | + $parent = ''; |
| 103 | + } |
| 104 | + |
| 105 | + if ($this->isCreatable($parent)) { |
| 106 | + return $this->storage->rename($source, $target); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + public function copy($source, $target): bool { |
| 114 | + if (!$this->isReadable($source)) { |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + if ($this->file_exists($target)) { |
| 119 | + if ($this->isUpdatable($target)) { |
| 120 | + return $this->storage->copy($source, $target); |
| 121 | + } |
| 122 | + } else { |
| 123 | + $parent = dirname($target); |
| 124 | + if ($parent === '.') { |
| 125 | + $parent = ''; |
| 126 | + } |
| 127 | + |
| 128 | + if ($this->isCreatable($parent)) { |
| 129 | + return $this->storage->copy($source, $target); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + public function touch($path, $mtime = null): bool { |
| 137 | + if ($this->checkPath($path)) { |
| 138 | + return parent::touch($path); |
| 139 | + } |
| 140 | + |
| 141 | + return $this->storage->touch($path); |
| 142 | + } |
| 143 | + |
| 144 | + public function mkdir($path): bool { |
| 145 | + // Always allow creating the path of the dir mask. |
| 146 | + if ($path !== $this->path && $this->checkPath($path)) { |
| 147 | + return parent::mkdir($path); |
| 148 | + } |
| 149 | + |
| 150 | + return $this->storage->mkdir($path); |
| 151 | + } |
| 152 | + |
| 153 | + public function rmdir($path): bool { |
| 154 | + if ($this->checkPath($path)) { |
| 155 | + return parent::rmdir($path); |
| 156 | + } |
| 157 | + |
| 158 | + return $this->storage->rmdir($path); |
| 159 | + } |
| 160 | + |
| 161 | + public function unlink($path): bool { |
| 162 | + if ($this->checkPath($path)) { |
| 163 | + return parent::unlink($path); |
| 164 | + } |
| 165 | + |
| 166 | + return $this->storage->unlink($path); |
| 167 | + } |
| 168 | + |
| 169 | + public function file_put_contents($path, $data): int|float|false { |
| 170 | + if ($this->checkPath($path)) { |
| 171 | + return parent::file_put_contents($path, $data); |
| 172 | + } |
| 173 | + |
| 174 | + return $this->storage->file_put_contents($path, $data); |
| 175 | + } |
| 176 | + |
| 177 | + public function fopen($path, $mode) { |
| 178 | + if ($this->checkPath($path)) { |
| 179 | + return parent::fopen($path, $mode); |
| 180 | + } |
| 181 | + |
| 182 | + return $this->storage->fopen($path, $mode); |
| 183 | + } |
| 184 | + |
| 185 | + public function getCache($path = '', $storage = null): ICache { |
| 186 | + if (!$storage) { |
| 187 | + $storage = $this; |
| 188 | + } |
| 189 | + |
| 190 | + $sourceCache = $this->storage->getCache($path, $storage); |
| 191 | + return new CacheDirPermissionsMask($sourceCache, $this->mask, $this->checkPath(...)); |
| 192 | + } |
| 193 | +} |
0 commit comments