PermissionsMask.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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($path1, $path2) {
  72. $p = strpos($path1, $path2);
  73. if ($p === 0) {
  74. $part = substr($path1, strlen($path2));
  75. //This is a rename of the transfer file to the original file
  76. if (strpos($part, '.ocTransferId') === 0) {
  77. return $this->checkMask(Constants::PERMISSION_CREATE) and parent::rename($path1, $path2);
  78. }
  79. }
  80. return $this->checkMask(Constants::PERMISSION_UPDATE) and parent::rename($path1, $path2);
  81. }
  82. public function copy($path1, $path2) {
  83. return $this->checkMask(Constants::PERMISSION_CREATE) and parent::copy($path1, $path2);
  84. }
  85. public function touch($path, $mtime = null) {
  86. $permissions = $this->file_exists($path) ? Constants::PERMISSION_UPDATE : Constants::PERMISSION_CREATE;
  87. return $this->checkMask($permissions) and parent::touch($path, $mtime);
  88. }
  89. public function mkdir($path) {
  90. return $this->checkMask(Constants::PERMISSION_CREATE) and parent::mkdir($path);
  91. }
  92. public function rmdir($path) {
  93. return $this->checkMask(Constants::PERMISSION_DELETE) and parent::rmdir($path);
  94. }
  95. public function unlink($path) {
  96. return $this->checkMask(Constants::PERMISSION_DELETE) and parent::unlink($path);
  97. }
  98. public function file_put_contents($path, $data) {
  99. $permissions = $this->file_exists($path) ? Constants::PERMISSION_UPDATE : Constants::PERMISSION_CREATE;
  100. return $this->checkMask($permissions) ? parent::file_put_contents($path, $data) : false;
  101. }
  102. public function fopen($path, $mode) {
  103. if ($mode === 'r' or $mode === 'rb') {
  104. return parent::fopen($path, $mode);
  105. } else {
  106. $permissions = $this->file_exists($path) ? Constants::PERMISSION_UPDATE : Constants::PERMISSION_CREATE;
  107. return $this->checkMask($permissions) ? parent::fopen($path, $mode) : false;
  108. }
  109. }
  110. /**
  111. * get a cache instance for the storage
  112. *
  113. * @param string $path
  114. * @param \OC\Files\Storage\Storage (optional) the storage to pass to the cache
  115. * @return \OC\Files\Cache\Cache
  116. */
  117. public function getCache($path = '', $storage = null) {
  118. if (!$storage) {
  119. $storage = $this;
  120. }
  121. $sourceCache = parent::getCache($path, $storage);
  122. return new CachePermissionsMask($sourceCache, $this->mask);
  123. }
  124. public function getMetaData($path) {
  125. $data = parent::getMetaData($path);
  126. if ($data && isset($data['permissions'])) {
  127. $data['scan_permissions'] = isset($data['scan_permissions']) ? $data['scan_permissions'] : $data['permissions'];
  128. $data['permissions'] &= $this->mask;
  129. }
  130. return $data;
  131. }
  132. public function getScanner($path = '', $storage = null) {
  133. if (!$storage) {
  134. $storage = $this->storage;
  135. }
  136. return parent::getScanner($path, $storage);
  137. }
  138. }