You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
5.2 KiB

10 years ago
10 years ago
  1. <?php
  2. /**
  3. * @author Robin Appelman <icewind@owncloud.com>
  4. * @author Robin McCorkell <robin@mccorkell.me.uk>
  5. *
  6. * @copyright Copyright (c) 2016, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OCA\Files_External\Service;
  23. use OCP\Files\Config\IUserMountCache;
  24. use \OCP\IUserSession;
  25. use \OCP\IGroupManager;
  26. use \OCA\Files_External\Lib\StorageConfig;
  27. /**
  28. * Service class to read global storages applicable to the user
  29. * Read-only access available, attempting to write will throw DomainException
  30. */
  31. class UserGlobalStoragesService extends GlobalStoragesService {
  32. use UserTrait;
  33. /** @var IGroupManager */
  34. protected $groupManager;
  35. /**
  36. * @param BackendService $backendService
  37. * @param DBConfigService $dbConfig
  38. * @param IUserSession $userSession
  39. * @param IGroupManager $groupManager
  40. * @param IUserMountCache $userMountCache
  41. */
  42. public function __construct(
  43. BackendService $backendService,
  44. DBConfigService $dbConfig,
  45. IUserSession $userSession,
  46. IGroupManager $groupManager,
  47. IUserMountCache $userMountCache
  48. ) {
  49. parent::__construct($backendService, $dbConfig, $userMountCache);
  50. $this->userSession = $userSession;
  51. $this->groupManager = $groupManager;
  52. }
  53. /**
  54. * Replace config hash ID with real IDs, for migrating legacy storages
  55. *
  56. * @param StorageConfig[] $storages Storages with real IDs
  57. * @param StorageConfig[] $storagesWithConfigHash Storages with config hash IDs
  58. */
  59. protected function setRealStorageIds(array &$storages, array $storagesWithConfigHash) {
  60. // as a read-only view, storage IDs don't need to be real
  61. foreach ($storagesWithConfigHash as $storage) {
  62. $storages[$storage->getId()] = $storage;
  63. }
  64. }
  65. protected function readDBConfig() {
  66. $userMounts = $this->dbConfig->getAdminMountsFor(DBConfigService::APPLICABLE_TYPE_USER, $this->getUser()->getUID());
  67. $globalMounts = $this->dbConfig->getAdminMountsFor(DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
  68. $groups = $this->groupManager->getUserGroupIds($this->getUser());
  69. if (is_array($groups) && count($groups) !== 0) {
  70. $groupMounts = $this->dbConfig->getAdminMountsForMultiple(DBConfigService::APPLICABLE_TYPE_GROUP, $groups);
  71. } else {
  72. $groupMounts = [];
  73. }
  74. return array_merge($userMounts, $groupMounts, $globalMounts);
  75. }
  76. public function addStorage(StorageConfig $newStorage) {
  77. throw new \DomainException('UserGlobalStoragesService writing disallowed');
  78. }
  79. public function updateStorage(StorageConfig $updatedStorage) {
  80. throw new \DomainException('UserGlobalStoragesService writing disallowed');
  81. }
  82. /**
  83. * @param integer $id
  84. */
  85. public function removeStorage($id) {
  86. throw new \DomainException('UserGlobalStoragesService writing disallowed');
  87. }
  88. /**
  89. * Get unique storages, in case two are defined with the same mountpoint
  90. * Higher priority storages take precedence
  91. *
  92. * @return StorageConfig[]
  93. */
  94. public function getUniqueStorages() {
  95. $storages = $this->getStorages();
  96. $storagesByMountpoint = [];
  97. foreach ($storages as $storage) {
  98. $storagesByMountpoint[$storage->getMountPoint()][] = $storage;
  99. }
  100. $result = [];
  101. foreach ($storagesByMountpoint as $storageList) {
  102. $storage = array_reduce($storageList, function ($carry, $item) {
  103. if (isset($carry)) {
  104. $carryPriorityType = $this->getPriorityType($carry);
  105. $itemPriorityType = $this->getPriorityType($item);
  106. if ($carryPriorityType > $itemPriorityType) {
  107. return $carry;
  108. } elseif ($carryPriorityType === $itemPriorityType) {
  109. if ($carry->getPriority() > $item->getPriority()) {
  110. return $carry;
  111. }
  112. }
  113. }
  114. return $item;
  115. });
  116. $result[$storage->getID()] = $storage;
  117. }
  118. return $result;
  119. }
  120. /**
  121. * Get a priority 'type', where a bigger number means higher priority
  122. * user applicable > group applicable > 'all'
  123. *
  124. * @param StorageConfig $storage
  125. * @return int
  126. */
  127. protected function getPriorityType(StorageConfig $storage) {
  128. $applicableUsers = $storage->getApplicableUsers();
  129. $applicableGroups = $storage->getApplicableGroups();
  130. if ($applicableUsers && $applicableUsers[0] !== 'all') {
  131. return 2;
  132. }
  133. if ($applicableGroups) {
  134. return 1;
  135. }
  136. return 0;
  137. }
  138. protected function isApplicable(StorageConfig $config) {
  139. $applicableUsers = $config->getApplicableUsers();
  140. $applicableGroups = $config->getApplicableGroups();
  141. if (count($applicableUsers) === 0 && count($applicableGroups) === 0) {
  142. return true;
  143. }
  144. if (in_array($this->getUser()->getUID(), $applicableUsers, true)) {
  145. return true;
  146. }
  147. $groupIds = $this->groupManager->getUserGroupIds($this->getUser());
  148. foreach ($groupIds as $groupId) {
  149. if (in_array($groupId, $applicableGroups, true)) {
  150. return true;
  151. }
  152. }
  153. return false;
  154. }
  155. }