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.

GlobalStoragesService.php 5.0KB

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