GlobalStoragesService.php 5.7 KB

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