PermissionsMask.php 4.7 KB

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