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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * @author Vincent Petry <pvince81@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OCA\Files_external\Service;
  22. use \OCP\IUserSession;
  23. use \OC\Files\Filesystem;
  24. use \OCA\Files_external\Lib\StorageConfig;
  25. use \OCA\Files_external\NotFoundException;
  26. /**
  27. * Service class to manage global external storages
  28. */
  29. class GlobalStoragesService extends StoragesService {
  30. /**
  31. * Write the storages to the configuration.
  32. *
  33. * @param array $storages map of storage id to storage config
  34. */
  35. public function writeConfig($storages) {
  36. // let the horror begin
  37. $mountPoints = [];
  38. foreach ($storages as $storageConfig) {
  39. $mountPoint = $storageConfig->getMountPoint();
  40. $oldBackendOptions = $storageConfig->getBackendOptions();
  41. $storageConfig->setBackendOptions(
  42. \OC_Mount_Config::encryptPasswords(
  43. $oldBackendOptions
  44. )
  45. );
  46. // system mount
  47. $rootMountPoint = '/$user/files/' . ltrim($mountPoint, '/');
  48. $applicableUsers = $storageConfig->getApplicableUsers();
  49. $applicableGroups = $storageConfig->getApplicableGroups();
  50. foreach ($applicableUsers as $applicable) {
  51. $this->addMountPoint(
  52. $mountPoints,
  53. \OC_Mount_Config::MOUNT_TYPE_USER,
  54. $applicable,
  55. $rootMountPoint,
  56. $storageConfig
  57. );
  58. }
  59. foreach ($applicableGroups as $applicable) {
  60. $this->addMountPoint(
  61. $mountPoints,
  62. \OC_Mount_Config::MOUNT_TYPE_GROUP,
  63. $applicable,
  64. $rootMountPoint,
  65. $storageConfig
  66. );
  67. }
  68. // if neither "applicableGroups" or "applicableUsers" were set, use "all" user
  69. if (empty($applicableUsers) && empty($applicableGroups)) {
  70. $this->addMountPoint(
  71. $mountPoints,
  72. \OC_Mount_Config::MOUNT_TYPE_USER,
  73. 'all',
  74. $rootMountPoint,
  75. $storageConfig
  76. );
  77. }
  78. // restore old backend options where the password was not encrypted,
  79. // because we don't want to change the state of the original object
  80. $storageConfig->setBackendOptions($oldBackendOptions);
  81. }
  82. \OC_Mount_Config::writeData(null, $mountPoints);
  83. }
  84. /**
  85. * Triggers $signal for all applicable users of the given
  86. * storage
  87. *
  88. * @param StorageConfig $storage storage data
  89. * @param string $signal signal to trigger
  90. */
  91. protected function triggerHooks(StorageConfig $storage, $signal) {
  92. $applicableUsers = $storage->getApplicableUsers();
  93. $applicableGroups = $storage->getApplicableGroups();
  94. if (empty($applicableUsers) && empty($applicableGroups)) {
  95. // raise for user "all"
  96. $this->triggerApplicableHooks(
  97. $signal,
  98. $storage->getMountPoint(),
  99. \OC_Mount_Config::MOUNT_TYPE_USER,
  100. ['all']
  101. );
  102. return;
  103. }
  104. $this->triggerApplicableHooks(
  105. $signal,
  106. $storage->getMountPoint(),
  107. \OC_Mount_Config::MOUNT_TYPE_USER,
  108. $applicableUsers
  109. );
  110. $this->triggerApplicableHooks(
  111. $signal,
  112. $storage->getMountPoint(),
  113. \OC_Mount_Config::MOUNT_TYPE_GROUP,
  114. $applicableGroups
  115. );
  116. }
  117. /**
  118. * Triggers signal_create_mount or signal_delete_mount to
  119. * accomodate for additions/deletions in applicableUsers
  120. * and applicableGroups fields.
  121. *
  122. * @param StorageConfig $oldStorage old storage config
  123. * @param StorageConfig $newStorage new storage config
  124. */
  125. protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage) {
  126. // if mount point changed, it's like a deletion + creation
  127. if ($oldStorage->getMountPoint() !== $newStorage->getMountPoint()) {
  128. $this->triggerHooks($oldStorage, Filesystem::signal_delete_mount);
  129. $this->triggerHooks($newStorage, Filesystem::signal_create_mount);
  130. return;
  131. }
  132. $userAdditions = array_diff($newStorage->getApplicableUsers(), $oldStorage->getApplicableUsers());
  133. $userDeletions = array_diff($oldStorage->getApplicableUsers(), $newStorage->getApplicableUsers());
  134. $groupAdditions = array_diff($newStorage->getApplicableGroups(), $oldStorage->getApplicableGroups());
  135. $groupDeletions = array_diff($oldStorage->getApplicableGroups(), $newStorage->getApplicableGroups());
  136. // if no applicable were set, raise a signal for "all"
  137. if (empty($oldStorage->getApplicableUsers()) && empty($oldStorage->getApplicableGroups())) {
  138. $this->triggerApplicableHooks(
  139. Filesystem::signal_delete_mount,
  140. $oldStorage->getMountPoint(),
  141. \OC_Mount_Config::MOUNT_TYPE_USER,
  142. ['all']
  143. );
  144. }
  145. // trigger delete for removed users
  146. $this->triggerApplicableHooks(
  147. Filesystem::signal_delete_mount,
  148. $oldStorage->getMountPoint(),
  149. \OC_Mount_Config::MOUNT_TYPE_USER,
  150. $userDeletions
  151. );
  152. // trigger delete for removed groups
  153. $this->triggerApplicableHooks(
  154. Filesystem::signal_delete_mount,
  155. $oldStorage->getMountPoint(),
  156. \OC_Mount_Config::MOUNT_TYPE_GROUP,
  157. $groupDeletions
  158. );
  159. // and now add the new users
  160. $this->triggerApplicableHooks(
  161. Filesystem::signal_create_mount,
  162. $newStorage->getMountPoint(),
  163. \OC_Mount_Config::MOUNT_TYPE_USER,
  164. $userAdditions
  165. );
  166. // and now add the new groups
  167. $this->triggerApplicableHooks(
  168. Filesystem::signal_create_mount,
  169. $newStorage->getMountPoint(),
  170. \OC_Mount_Config::MOUNT_TYPE_GROUP,
  171. $groupAdditions
  172. );
  173. // if no applicable, raise a signal for "all"
  174. if (empty($newStorage->getApplicableUsers()) && empty($newStorage->getApplicableGroups())) {
  175. $this->triggerApplicableHooks(
  176. Filesystem::signal_create_mount,
  177. $newStorage->getMountPoint(),
  178. \OC_Mount_Config::MOUNT_TYPE_USER,
  179. ['all']
  180. );
  181. }
  182. }
  183. }