PermissionsMask.php 4.4 KB

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