GlobalStoragesService.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-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 OC\Files\Filesystem;
  9. use OCA\Files_External\Lib\StorageConfig;
  10. use OCA\Files_External\MountConfig;
  11. /**
  12. * Service class to manage global external storage
  13. */
  14. class GlobalStoragesService extends StoragesService {
  15. /**
  16. * Triggers $signal for all applicable users of the given
  17. * storage
  18. *
  19. * @param StorageConfig $storage storage data
  20. * @param string $signal signal to trigger
  21. */
  22. protected function triggerHooks(StorageConfig $storage, $signal) {
  23. // FIXME: Use as expression in empty once PHP 5.4 support is dropped
  24. $applicableUsers = $storage->getApplicableUsers();
  25. $applicableGroups = $storage->getApplicableGroups();
  26. if (empty($applicableUsers) && empty($applicableGroups)) {
  27. // raise for user "all"
  28. $this->triggerApplicableHooks(
  29. $signal,
  30. $storage->getMountPoint(),
  31. MountConfig::MOUNT_TYPE_USER,
  32. ['all']
  33. );
  34. return;
  35. }
  36. $this->triggerApplicableHooks(
  37. $signal,
  38. $storage->getMountPoint(),
  39. MountConfig::MOUNT_TYPE_USER,
  40. $applicableUsers
  41. );
  42. $this->triggerApplicableHooks(
  43. $signal,
  44. $storage->getMountPoint(),
  45. MountConfig::MOUNT_TYPE_GROUP,
  46. $applicableGroups
  47. );
  48. }
  49. /**
  50. * Triggers signal_create_mount or signal_delete_mount to
  51. * accommodate for additions/deletions in applicableUsers
  52. * and applicableGroups fields.
  53. *
  54. * @param StorageConfig $oldStorage old storage config
  55. * @param StorageConfig $newStorage new storage config
  56. */
  57. protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage) {
  58. // if mount point changed, it's like a deletion + creation
  59. if ($oldStorage->getMountPoint() !== $newStorage->getMountPoint()) {
  60. $this->triggerHooks($oldStorage, Filesystem::signal_delete_mount);
  61. $this->triggerHooks($newStorage, Filesystem::signal_create_mount);
  62. return;
  63. }
  64. $userAdditions = array_diff($newStorage->getApplicableUsers(), $oldStorage->getApplicableUsers());
  65. $userDeletions = array_diff($oldStorage->getApplicableUsers(), $newStorage->getApplicableUsers());
  66. $groupAdditions = array_diff($newStorage->getApplicableGroups(), $oldStorage->getApplicableGroups());
  67. $groupDeletions = array_diff($oldStorage->getApplicableGroups(), $newStorage->getApplicableGroups());
  68. // FIXME: Use as expression in empty once PHP 5.4 support is dropped
  69. // if no applicable were set, raise a signal for "all"
  70. $oldApplicableUsers = $oldStorage->getApplicableUsers();
  71. $oldApplicableGroups = $oldStorage->getApplicableGroups();
  72. if (empty($oldApplicableUsers) && empty($oldApplicableGroups)) {
  73. $this->triggerApplicableHooks(
  74. Filesystem::signal_delete_mount,
  75. $oldStorage->getMountPoint(),
  76. MountConfig::MOUNT_TYPE_USER,
  77. ['all']
  78. );
  79. }
  80. // trigger delete for removed users
  81. $this->triggerApplicableHooks(
  82. Filesystem::signal_delete_mount,
  83. $oldStorage->getMountPoint(),
  84. MountConfig::MOUNT_TYPE_USER,
  85. $userDeletions
  86. );
  87. // trigger delete for removed groups
  88. $this->triggerApplicableHooks(
  89. Filesystem::signal_delete_mount,
  90. $oldStorage->getMountPoint(),
  91. MountConfig::MOUNT_TYPE_GROUP,
  92. $groupDeletions
  93. );
  94. // and now add the new users
  95. $this->triggerApplicableHooks(
  96. Filesystem::signal_create_mount,
  97. $newStorage->getMountPoint(),
  98. MountConfig::MOUNT_TYPE_USER,
  99. $userAdditions
  100. );
  101. // and now add the new groups
  102. $this->triggerApplicableHooks(
  103. Filesystem::signal_create_mount,
  104. $newStorage->getMountPoint(),
  105. MountConfig::MOUNT_TYPE_GROUP,
  106. $groupAdditions
  107. );
  108. // FIXME: Use as expression in empty once PHP 5.4 support is dropped
  109. // if no applicable, raise a signal for "all"
  110. $newApplicableUsers = $newStorage->getApplicableUsers();
  111. $newApplicableGroups = $newStorage->getApplicableGroups();
  112. if (empty($newApplicableUsers) && empty($newApplicableGroups)) {
  113. $this->triggerApplicableHooks(
  114. Filesystem::signal_create_mount,
  115. $newStorage->getMountPoint(),
  116. MountConfig::MOUNT_TYPE_USER,
  117. ['all']
  118. );
  119. }
  120. }
  121. /**
  122. * Get the visibility type for this controller, used in validation
  123. *
  124. * @return int BackendService::VISIBILITY_* constants
  125. */
  126. public function getVisibilityType() {
  127. return BackendService::VISIBILITY_ADMIN;
  128. }
  129. protected function isApplicable(StorageConfig $config) {
  130. return true;
  131. }
  132. /**
  133. * Get all configured admin and personal mounts
  134. *
  135. * @return StorageConfig[] map of storage id to storage config
  136. */
  137. public function getStorageForAllUsers() {
  138. $mounts = $this->dbConfig->getAllMounts();
  139. $configs = array_map([$this, 'getStorageConfigFromDBMount'], $mounts);
  140. $configs = array_filter($configs, function ($config) {
  141. return $config instanceof StorageConfig;
  142. });
  143. $keys = array_map(function (StorageConfig $config) {
  144. return $config->getId();
  145. }, $configs);
  146. return array_combine($keys, $configs);
  147. }
  148. }