UserGlobalStoragesService.php 6.0 KB

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