UserGlobalStoragesService.php 5.6 KB

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