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.

UserGlobalStoragesService.php 6.3KB

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