GlobalStoragesService.php 5.7 KB

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