GlobalStoragesService.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Robin McCorkell <robin@mccorkell.me.uk>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Stefan Weil <sw@weilnetz.de>
  12. * @author szaimen <szaimen@e.mail.de>
  13. * @author Vincent Petry <vincent@nextcloud.com>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OCA\Files_External\Service;
  31. use OC\Files\Filesystem;
  32. use OCA\Files_External\Lib\StorageConfig;
  33. /**
  34. * Service class to manage global external storage
  35. */
  36. class GlobalStoragesService extends StoragesService {
  37. /**
  38. * Triggers $signal for all applicable users of the given
  39. * storage
  40. *
  41. * @param StorageConfig $storage storage data
  42. * @param string $signal signal to trigger
  43. */
  44. protected function triggerHooks(StorageConfig $storage, $signal) {
  45. // FIXME: Use as expression in empty once PHP 5.4 support is dropped
  46. $applicableUsers = $storage->getApplicableUsers();
  47. $applicableGroups = $storage->getApplicableGroups();
  48. if (empty($applicableUsers) && empty($applicableGroups)) {
  49. // raise for user "all"
  50. $this->triggerApplicableHooks(
  51. $signal,
  52. $storage->getMountPoint(),
  53. \OCA\Files_External\MountConfig::MOUNT_TYPE_USER,
  54. ['all']
  55. );
  56. return;
  57. }
  58. $this->triggerApplicableHooks(
  59. $signal,
  60. $storage->getMountPoint(),
  61. \OCA\Files_External\MountConfig::MOUNT_TYPE_USER,
  62. $applicableUsers
  63. );
  64. $this->triggerApplicableHooks(
  65. $signal,
  66. $storage->getMountPoint(),
  67. \OCA\Files_External\MountConfig::MOUNT_TYPE_GROUP,
  68. $applicableGroups
  69. );
  70. }
  71. /**
  72. * Triggers signal_create_mount or signal_delete_mount to
  73. * accommodate for additions/deletions in applicableUsers
  74. * and applicableGroups fields.
  75. *
  76. * @param StorageConfig $oldStorage old storage config
  77. * @param StorageConfig $newStorage new storage config
  78. */
  79. protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage) {
  80. // if mount point changed, it's like a deletion + creation
  81. if ($oldStorage->getMountPoint() !== $newStorage->getMountPoint()) {
  82. $this->triggerHooks($oldStorage, Filesystem::signal_delete_mount);
  83. $this->triggerHooks($newStorage, Filesystem::signal_create_mount);
  84. return;
  85. }
  86. $userAdditions = array_diff($newStorage->getApplicableUsers(), $oldStorage->getApplicableUsers());
  87. $userDeletions = array_diff($oldStorage->getApplicableUsers(), $newStorage->getApplicableUsers());
  88. $groupAdditions = array_diff($newStorage->getApplicableGroups(), $oldStorage->getApplicableGroups());
  89. $groupDeletions = array_diff($oldStorage->getApplicableGroups(), $newStorage->getApplicableGroups());
  90. // FIXME: Use as expression in empty once PHP 5.4 support is dropped
  91. // if no applicable were set, raise a signal for "all"
  92. $oldApplicableUsers = $oldStorage->getApplicableUsers();
  93. $oldApplicableGroups = $oldStorage->getApplicableGroups();
  94. if (empty($oldApplicableUsers) && empty($oldApplicableGroups)) {
  95. $this->triggerApplicableHooks(
  96. Filesystem::signal_delete_mount,
  97. $oldStorage->getMountPoint(),
  98. \OCA\Files_External\MountConfig::MOUNT_TYPE_USER,
  99. ['all']
  100. );
  101. }
  102. // trigger delete for removed users
  103. $this->triggerApplicableHooks(
  104. Filesystem::signal_delete_mount,
  105. $oldStorage->getMountPoint(),
  106. \OCA\Files_External\MountConfig::MOUNT_TYPE_USER,
  107. $userDeletions
  108. );
  109. // trigger delete for removed groups
  110. $this->triggerApplicableHooks(
  111. Filesystem::signal_delete_mount,
  112. $oldStorage->getMountPoint(),
  113. \OCA\Files_External\MountConfig::MOUNT_TYPE_GROUP,
  114. $groupDeletions
  115. );
  116. // and now add the new users
  117. $this->triggerApplicableHooks(
  118. Filesystem::signal_create_mount,
  119. $newStorage->getMountPoint(),
  120. \OCA\Files_External\MountConfig::MOUNT_TYPE_USER,
  121. $userAdditions
  122. );
  123. // and now add the new groups
  124. $this->triggerApplicableHooks(
  125. Filesystem::signal_create_mount,
  126. $newStorage->getMountPoint(),
  127. \OCA\Files_External\MountConfig::MOUNT_TYPE_GROUP,
  128. $groupAdditions
  129. );
  130. // FIXME: Use as expression in empty once PHP 5.4 support is dropped
  131. // if no applicable, raise a signal for "all"
  132. $newApplicableUsers = $newStorage->getApplicableUsers();
  133. $newApplicableGroups = $newStorage->getApplicableGroups();
  134. if (empty($newApplicableUsers) && empty($newApplicableGroups)) {
  135. $this->triggerApplicableHooks(
  136. Filesystem::signal_create_mount,
  137. $newStorage->getMountPoint(),
  138. \OCA\Files_External\MountConfig::MOUNT_TYPE_USER,
  139. ['all']
  140. );
  141. }
  142. }
  143. /**
  144. * Get the visibility type for this controller, used in validation
  145. *
  146. * @return int BackendService::VISIBILITY_* constants
  147. */
  148. public function getVisibilityType() {
  149. return BackendService::VISIBILITY_ADMIN;
  150. }
  151. protected function isApplicable(StorageConfig $config) {
  152. return true;
  153. }
  154. /**
  155. * Get all configured admin and personal mounts
  156. *
  157. * @return StorageConfig[] map of storage id to storage config
  158. */
  159. public function getStorageForAllUsers() {
  160. $mounts = $this->dbConfig->getAllMounts();
  161. $configs = array_map([$this, 'getStorageConfigFromDBMount'], $mounts);
  162. $configs = array_filter($configs, function ($config) {
  163. return $config instanceof StorageConfig;
  164. });
  165. $keys = array_map(function (StorageConfig $config) {
  166. return $config->getId();
  167. }, $configs);
  168. return array_combine($keys, $configs);
  169. }
  170. }