PermissionsMask.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Files\Storage\Wrapper;
  8. use OC\Files\Cache\Wrapper\CachePermissionsMask;
  9. use OCP\Constants;
  10. use OCP\Files\Storage\IStorage;
  11. /**
  12. * Mask the permissions of a storage
  13. *
  14. * This can be used to restrict update, create, delete and/or share permissions of a storage
  15. *
  16. * Note that the read permissions can't be masked
  17. */
  18. class PermissionsMask extends Wrapper {
  19. /**
  20. * @var int the permissions bits we want to keep
  21. */
  22. private $mask;
  23. /**
  24. * @param array $parameters ['storage' => $storage, 'mask' => $mask]
  25. *
  26. * $storage: The storage the permissions mask should be applied on
  27. * $mask: The permission bits that should be kept, a combination of the \OCP\Constant::PERMISSION_ constants
  28. */
  29. public function __construct(array $parameters) {
  30. parent::__construct($parameters);
  31. $this->mask = $parameters['mask'];
  32. }
  33. private function checkMask(int $permissions): bool {
  34. return ($this->mask & $permissions) === $permissions;
  35. }
  36. public function isUpdatable(string $path): bool {
  37. return $this->checkMask(Constants::PERMISSION_UPDATE) and parent::isUpdatable($path);
  38. }
  39. public function isCreatable(string $path): bool {
  40. return $this->checkMask(Constants::PERMISSION_CREATE) and parent::isCreatable($path);
  41. }
  42. public function isDeletable(string $path): bool {
  43. return $this->checkMask(Constants::PERMISSION_DELETE) and parent::isDeletable($path);
  44. }
  45. public function isSharable(string $path): bool {
  46. return $this->checkMask(Constants::PERMISSION_SHARE) and parent::isSharable($path);
  47. }
  48. public function getPermissions(string $path): int {
  49. return $this->storage->getPermissions($path) & $this->mask;
  50. }
  51. public function rename(string $source, string $target): bool {
  52. //This is a rename of the transfer file to the original file
  53. if (dirname($source) === dirname($target) && strpos($source, '.ocTransferId') > 0) {
  54. return $this->checkMask(Constants::PERMISSION_CREATE) and parent::rename($source, $target);
  55. }
  56. return $this->checkMask(Constants::PERMISSION_UPDATE) and parent::rename($source, $target);
  57. }
  58. public function copy(string $source, string $target): bool {
  59. return $this->checkMask(Constants::PERMISSION_CREATE) and parent::copy($source, $target);
  60. }
  61. public function touch(string $path, ?int $mtime = null): bool {
  62. $permissions = $this->file_exists($path) ? Constants::PERMISSION_UPDATE : Constants::PERMISSION_CREATE;
  63. return $this->checkMask($permissions) and parent::touch($path, $mtime);
  64. }
  65. public function mkdir(string $path): bool {
  66. return $this->checkMask(Constants::PERMISSION_CREATE) and parent::mkdir($path);
  67. }
  68. public function rmdir(string $path): bool {
  69. return $this->checkMask(Constants::PERMISSION_DELETE) and parent::rmdir($path);
  70. }
  71. public function unlink(string $path): bool {
  72. return $this->checkMask(Constants::PERMISSION_DELETE) and parent::unlink($path);
  73. }
  74. public function file_put_contents(string $path, mixed $data): int|float|false {
  75. $permissions = $this->file_exists($path) ? Constants::PERMISSION_UPDATE : Constants::PERMISSION_CREATE;
  76. return $this->checkMask($permissions) ? parent::file_put_contents($path, $data) : false;
  77. }
  78. public function fopen(string $path, string $mode) {
  79. if ($mode === 'r' or $mode === 'rb') {
  80. return parent::fopen($path, $mode);
  81. } else {
  82. $permissions = $this->file_exists($path) ? Constants::PERMISSION_UPDATE : Constants::PERMISSION_CREATE;
  83. return $this->checkMask($permissions) ? parent::fopen($path, $mode) : false;
  84. }
  85. }
  86. public function getCache(string $path = '', ?IStorage $storage = null): \OCP\Files\Cache\ICache {
  87. if (!$storage) {
  88. $storage = $this;
  89. }
  90. $sourceCache = parent::getCache($path, $storage);
  91. return new CachePermissionsMask($sourceCache, $this->mask);
  92. }
  93. public function getMetaData(string $path): ?array {
  94. $data = parent::getMetaData($path);
  95. if ($data && isset($data['permissions'])) {
  96. $data['scan_permissions'] = $data['scan_permissions'] ?? $data['permissions'];
  97. $data['permissions'] &= $this->mask;
  98. }
  99. return $data;
  100. }
  101. public function getScanner(string $path = '', ?IStorage $storage = null): \OCP\Files\Cache\IScanner {
  102. if (!$storage) {
  103. $storage = $this->storage;
  104. }
  105. return parent::getScanner($path, $storage);
  106. }
  107. public function getDirectoryContent(string $directory): \Traversable {
  108. foreach ($this->getWrapperStorage()->getDirectoryContent($directory) as $data) {
  109. $data['scan_permissions'] = $data['scan_permissions'] ?? $data['permissions'];
  110. $data['permissions'] &= $this->mask;
  111. yield $data;
  112. }
  113. }
  114. }