UserGlobalStoragesService.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Service;
  8. use OCA\Files_External\Lib\StorageConfig;
  9. use OCP\EventDispatcher\IEventDispatcher;
  10. use OCP\Files\Config\IUserMountCache;
  11. use OCP\IGroupManager;
  12. use OCP\IUser;
  13. use OCP\IUserSession;
  14. /**
  15. * Service class to read global storages applicable to the user
  16. * Read-only access available, attempting to write will throw DomainException
  17. */
  18. class UserGlobalStoragesService extends GlobalStoragesService {
  19. use UserTrait;
  20. /** @var IGroupManager */
  21. protected $groupManager;
  22. /**
  23. * @param BackendService $backendService
  24. * @param DBConfigService $dbConfig
  25. * @param IUserSession $userSession
  26. * @param IGroupManager $groupManager
  27. * @param IUserMountCache $userMountCache
  28. * @param IEventDispatcher $eventDispatcher
  29. */
  30. public function __construct(
  31. BackendService $backendService,
  32. DBConfigService $dbConfig,
  33. IUserSession $userSession,
  34. IGroupManager $groupManager,
  35. IUserMountCache $userMountCache,
  36. IEventDispatcher $eventDispatcher
  37. ) {
  38. parent::__construct($backendService, $dbConfig, $userMountCache, $eventDispatcher);
  39. $this->userSession = $userSession;
  40. $this->groupManager = $groupManager;
  41. }
  42. /**
  43. * Replace config hash ID with real IDs, for migrating legacy storages
  44. *
  45. * @param StorageConfig[] $storages Storages with real IDs
  46. * @param StorageConfig[] $storagesWithConfigHash Storages with config hash IDs
  47. */
  48. protected function setRealStorageIds(array &$storages, array $storagesWithConfigHash) {
  49. // as a read-only view, storage IDs don't need to be real
  50. foreach ($storagesWithConfigHash as $storage) {
  51. $storages[$storage->getId()] = $storage;
  52. }
  53. }
  54. protected function readDBConfig() {
  55. $userMounts = $this->dbConfig->getAdminMountsFor(DBConfigService::APPLICABLE_TYPE_USER, $this->getUser()->getUID());
  56. $globalMounts = $this->dbConfig->getAdminMountsFor(DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
  57. $groups = $this->groupManager->getUserGroupIds($this->getUser());
  58. if (count($groups) !== 0) {
  59. $groupMounts = $this->dbConfig->getAdminMountsForMultiple(DBConfigService::APPLICABLE_TYPE_GROUP, $groups);
  60. } else {
  61. $groupMounts = [];
  62. }
  63. return array_merge($userMounts, $groupMounts, $globalMounts);
  64. }
  65. public function addStorage(StorageConfig $newStorage) {
  66. throw new \DomainException('UserGlobalStoragesService writing disallowed');
  67. }
  68. public function updateStorage(StorageConfig $updatedStorage) {
  69. throw new \DomainException('UserGlobalStoragesService writing disallowed');
  70. }
  71. /**
  72. * @param integer $id
  73. */
  74. public function removeStorage($id) {
  75. throw new \DomainException('UserGlobalStoragesService writing disallowed');
  76. }
  77. /**
  78. * Get unique storages, in case two are defined with the same mountpoint
  79. * Higher priority storages take precedence
  80. *
  81. * @return StorageConfig[]
  82. */
  83. public function getUniqueStorages() {
  84. $storages = $this->getStorages();
  85. $storagesByMountpoint = [];
  86. foreach ($storages as $storage) {
  87. $storagesByMountpoint[$storage->getMountPoint()][] = $storage;
  88. }
  89. $result = [];
  90. foreach ($storagesByMountpoint as $storageList) {
  91. $storage = array_reduce($storageList, function ($carry, $item) {
  92. if (isset($carry)) {
  93. $carryPriorityType = $this->getPriorityType($carry);
  94. $itemPriorityType = $this->getPriorityType($item);
  95. if ($carryPriorityType > $itemPriorityType) {
  96. return $carry;
  97. } elseif ($carryPriorityType === $itemPriorityType) {
  98. if ($carry->getPriority() > $item->getPriority()) {
  99. return $carry;
  100. }
  101. }
  102. }
  103. return $item;
  104. });
  105. $result[$storage->getID()] = $storage;
  106. }
  107. return $result;
  108. }
  109. /**
  110. * Get a priority 'type', where a bigger number means higher priority
  111. * user applicable > group applicable > 'all'
  112. *
  113. * @param StorageConfig $storage
  114. * @return int
  115. */
  116. protected function getPriorityType(StorageConfig $storage) {
  117. $applicableUsers = $storage->getApplicableUsers();
  118. $applicableGroups = $storage->getApplicableGroups();
  119. if ($applicableUsers && $applicableUsers[0] !== 'all') {
  120. return 2;
  121. }
  122. if ($applicableGroups) {
  123. return 1;
  124. }
  125. return 0;
  126. }
  127. protected function isApplicable(StorageConfig $config) {
  128. $applicableUsers = $config->getApplicableUsers();
  129. $applicableGroups = $config->getApplicableGroups();
  130. if (count($applicableUsers) === 0 && count($applicableGroups) === 0) {
  131. return true;
  132. }
  133. if (in_array($this->getUser()->getUID(), $applicableUsers, true)) {
  134. return true;
  135. }
  136. $groupIds = $this->groupManager->getUserGroupIds($this->getUser());
  137. foreach ($groupIds as $groupId) {
  138. if (in_array($groupId, $applicableGroups, true)) {
  139. return true;
  140. }
  141. }
  142. return false;
  143. }
  144. /**
  145. * Gets all storages for the user, admin, personal, global, etc
  146. *
  147. * @param IUser|null $user user to get the storages for, if not set the currently logged in user will be used
  148. * @return StorageConfig[] array of storage configs
  149. */
  150. public function getAllStoragesForUser(?IUser $user = null) {
  151. if (is_null($user)) {
  152. $user = $this->getUser();
  153. }
  154. if (is_null($user)) {
  155. return [];
  156. }
  157. $groupIds = $this->groupManager->getUserGroupIds($user);
  158. $mounts = $this->dbConfig->getMountsForUser($user->getUID(), $groupIds);
  159. $configs = array_map([$this, 'getStorageConfigFromDBMount'], $mounts);
  160. $configs = array_filter($configs, function ($config) {
  161. return $config instanceof StorageConfig;
  162. });
  163. $keys = array_map(function (StorageConfig $config) {
  164. return $config->getId();
  165. }, $configs);
  166. $storages = array_combine($keys, $configs);
  167. return array_filter($storages, [$this, 'validateStorage']);
  168. }
  169. }