PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/files/cache/permissions.php

https://github.com/sezuan/core
PHP | 143 lines | 80 code | 12 blank | 51 comment | 12 complexity | 669e2bdfd70ace139651a5f5129852f8 MD5 | raw file
Possible License(s): AGPL-3.0, AGPL-1.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Files\Cache;
  9. class Permissions {
  10. /**
  11. * @var string $storageId
  12. */
  13. private $storageId;
  14. /**
  15. * @param \OC\Files\Storage\Storage|string $storage
  16. */
  17. public function __construct($storage) {
  18. if ($storage instanceof \OC\Files\Storage\Storage) {
  19. $this->storageId = $storage->getId();
  20. } else {
  21. $this->storageId = $storage;
  22. }
  23. }
  24. /**
  25. * get the permissions for a single file
  26. *
  27. * @param int $fileId
  28. * @param string $user
  29. * @return int (-1 if file no permissions set)
  30. */
  31. public function get($fileId, $user) {
  32. $sql = 'SELECT `permissions` FROM `*PREFIX*permissions` WHERE `user` = ? AND `fileid` = ?';
  33. $result = \OC_DB::executeAudited($sql, array($user, $fileId));
  34. if ($row = $result->fetchRow()) {
  35. return $row['permissions'];
  36. } else {
  37. return -1;
  38. }
  39. }
  40. /**
  41. * set the permissions of a file
  42. *
  43. * @param int $fileId
  44. * @param string $user
  45. * @param int $permissions
  46. */
  47. public function set($fileId, $user, $permissions) {
  48. if (self::get($fileId, $user) !== -1) {
  49. $sql = 'UPDATE `*PREFIX*permissions` SET `permissions` = ? WHERE `user` = ? AND `fileid` = ?';
  50. } else {
  51. $sql = 'INSERT INTO `*PREFIX*permissions`(`permissions`, `user`, `fileid`) VALUES(?, ?,? )';
  52. }
  53. \OC_DB::executeAudited($sql, array($permissions, $user, $fileId));
  54. }
  55. /**
  56. * get the permissions of multiply files
  57. *
  58. * @param int[] $fileIds
  59. * @param string $user
  60. * @return int[]
  61. */
  62. public function getMultiple($fileIds, $user) {
  63. if (count($fileIds) === 0) {
  64. return array();
  65. }
  66. $params = $fileIds;
  67. $params[] = $user;
  68. $inPart = implode(', ', array_fill(0, count($fileIds), '?'));
  69. $sql = 'SELECT `fileid`, `permissions` FROM `*PREFIX*permissions`'
  70. . ' WHERE `fileid` IN (' . $inPart . ') AND `user` = ?';
  71. $result = \OC_DB::executeAudited($sql, $params);
  72. $filePermissions = array();
  73. while ($row = $result->fetchRow()) {
  74. $filePermissions[$row['fileid']] = $row['permissions'];
  75. }
  76. return $filePermissions;
  77. }
  78. /**
  79. * get the permissions for all files in a folder
  80. *
  81. * @param int $parentId
  82. * @param string $user
  83. * @return int[]
  84. */
  85. public function getDirectoryPermissions($parentId, $user) {
  86. $sql = 'SELECT `*PREFIX*permissions`.`fileid`, `permissions`
  87. FROM `*PREFIX*permissions`
  88. INNER JOIN `*PREFIX*filecache` ON `*PREFIX*permissions`.`fileid` = `*PREFIX*filecache`.`fileid`
  89. WHERE `*PREFIX*filecache`.`parent` = ? AND `*PREFIX*permissions`.`user` = ?';
  90. $result = \OC_DB::executeAudited($sql, array($parentId, $user));
  91. $filePermissions = array();
  92. while ($row = $result->fetchRow()) {
  93. $filePermissions[$row['fileid']] = $row['permissions'];
  94. }
  95. return $filePermissions;
  96. }
  97. /**
  98. * remove the permissions for a file
  99. *
  100. * @param int $fileId
  101. * @param string $user
  102. */
  103. public function remove($fileId, $user = null) {
  104. if (is_null($user)) {
  105. \OC_DB::executeAudited('DELETE FROM `*PREFIX*permissions` WHERE `fileid` = ?', array($fileId));
  106. } else {
  107. $sql = 'DELETE FROM `*PREFIX*permissions` WHERE `fileid` = ? AND `user` = ?';
  108. \OC_DB::executeAudited($sql, array($fileId, $user));
  109. }
  110. }
  111. public function removeMultiple($fileIds, $user) {
  112. $query = \OC_DB::prepare('DELETE FROM `*PREFIX*permissions` WHERE `fileid` = ? AND `user` = ?');
  113. foreach ($fileIds as $fileId) {
  114. \OC_DB::executeAudited($query, array($fileId, $user));
  115. }
  116. }
  117. /**
  118. * get the list of users which have permissions stored for a file
  119. *
  120. * @param int $fileId
  121. */
  122. public function getUsers($fileId) {
  123. $sql = 'SELECT `user` FROM `*PREFIX*permissions` WHERE `fileid` = ?';
  124. $result = \OC_DB::executeAudited($sql, array($fileId));
  125. $users = array();
  126. while ($row = $result->fetchRow()) {
  127. $users[] = $row['user'];
  128. }
  129. return $users;
  130. }
  131. }