PermissionsMask.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Robin McCorkell <robin@mccorkell.me.uk>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Stefan Weil <sw@weilnetz.de>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC\Files\Storage\Wrapper;
  29. use OC\Files\Cache\Wrapper\CachePermissionsMask;
  30. use OCP\Constants;
  31. /**
  32. * Mask the permissions of a storage
  33. *
  34. * This can be used to restrict update, create, delete and/or share permissions of a storage
  35. *
  36. * Note that the read permissions can't be masked
  37. */
  38. class PermissionsMask extends Wrapper {
  39. /**
  40. * @var int the permissions bits we want to keep
  41. */
  42. private $mask;
  43. /**
  44. * @param array $arguments ['storage' => $storage, 'mask' => $mask]
  45. *
  46. * $storage: The storage the permissions mask should be applied on
  47. * $mask: The permission bits that should be kept, a combination of the \OCP\Constant::PERMISSION_ constants
  48. */
  49. public function __construct($arguments) {
  50. parent::__construct($arguments);
  51. $this->mask = $arguments['mask'];
  52. }
  53. private function checkMask($permissions) {
  54. return ($this->mask & $permissions) === $permissions;
  55. }
  56. public function isUpdatable($path) {
  57. return $this->checkMask(Constants::PERMISSION_UPDATE) and parent::isUpdatable($path);
  58. }
  59. public function isCreatable($path) {
  60. return $this->checkMask(Constants::PERMISSION_CREATE) and parent::isCreatable($path);
  61. }
  62. public function isDeletable($path) {
  63. return $this->checkMask(Constants::PERMISSION_DELETE) and parent::isDeletable($path);
  64. }
  65. public function isSharable($path) {
  66. return $this->checkMask(Constants::PERMISSION_SHARE) and parent::isSharable($path);
  67. }
  68. public function getPermissions($path) {
  69. return $this->storage->getPermissions($path) & $this->mask;
  70. }
  71. public function rename($source, $target) {
  72. //This is a rename of the transfer file to the original file
  73. if (dirname($source) === dirname($target) && strpos($source, '.ocTransferId') > 0) {
  74. return $this->checkMask(Constants::PERMISSION_CREATE) and parent::rename($source, $target);
  75. }
  76. return $this->checkMask(Constants::PERMISSION_UPDATE) and parent::rename($source, $target);
  77. }
  78. public function copy($source, $target) {
  79. return $this->checkMask(Constants::PERMISSION_CREATE) and parent::copy($source, $target);
  80. }
  81. public function touch($path, $mtime = null) {
  82. $permissions = $this->file_exists($path) ? Constants::PERMISSION_UPDATE : Constants::PERMISSION_CREATE;
  83. return $this->checkMask($permissions) and parent::touch($path, $mtime);
  84. }
  85. public function mkdir($path) {
  86. return $this->checkMask(Constants::PERMISSION_CREATE) and parent::mkdir($path);
  87. }
  88. public function rmdir($path) {
  89. return $this->checkMask(Constants::PERMISSION_DELETE) and parent::rmdir($path);
  90. }
  91. public function unlink($path) {
  92. return $this->checkMask(Constants::PERMISSION_DELETE) and parent::unlink($path);
  93. }
  94. public function file_put_contents($path, $data) {
  95. $permissions = $this->file_exists($path) ? Constants::PERMISSION_UPDATE : Constants::PERMISSION_CREATE;
  96. return $this->checkMask($permissions) ? parent::file_put_contents($path, $data) : false;
  97. }
  98. public function fopen($path, $mode) {
  99. if ($mode === 'r' or $mode === 'rb') {
  100. return parent::fopen($path, $mode);
  101. } else {
  102. $permissions = $this->file_exists($path) ? Constants::PERMISSION_UPDATE : Constants::PERMISSION_CREATE;
  103. return $this->checkMask($permissions) ? parent::fopen($path, $mode) : false;
  104. }
  105. }
  106. /**
  107. * get a cache instance for the storage
  108. *
  109. * @param string $path
  110. * @param \OC\Files\Storage\Storage (optional) the storage to pass to the cache
  111. * @return \OC\Files\Cache\Cache
  112. */
  113. public function getCache($path = '', $storage = null) {
  114. if (!$storage) {
  115. $storage = $this;
  116. }
  117. $sourceCache = parent::getCache($path, $storage);
  118. return new CachePermissionsMask($sourceCache, $this->mask);
  119. }
  120. public function getMetaData($path) {
  121. $data = parent::getMetaData($path);
  122. if ($data && isset($data['permissions'])) {
  123. $data['scan_permissions'] = isset($data['scan_permissions']) ? $data['scan_permissions'] : $data['permissions'];
  124. $data['permissions'] &= $this->mask;
  125. }
  126. return $data;
  127. }
  128. public function getScanner($path = '', $storage = null) {
  129. if (!$storage) {
  130. $storage = $this->storage;
  131. }
  132. return parent::getScanner($path, $storage);
  133. }
  134. public function getDirectoryContent($directory): \Traversable {
  135. foreach ($this->getWrapperStorage()->getDirectoryContent($directory) as $data) {
  136. $data['scan_permissions'] = isset($data['scan_permissions']) ? $data['scan_permissions'] : $data['permissions'];
  137. $data['permissions'] &= $this->mask;
  138. yield $data;
  139. }
  140. }
  141. }