UserStoragesService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018-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. use OCA\Files_External\MountConfig;
  11. use OCA\Files_External\NotFoundException;
  12. use OCP\EventDispatcher\IEventDispatcher;
  13. use OCP\Files\Config\IUserMountCache;
  14. use OCP\IUserSession;
  15. /**
  16. * Service class to manage user external storage
  17. * (aka personal storages)
  18. */
  19. class UserStoragesService extends StoragesService {
  20. use UserTrait;
  21. /**
  22. * Create a user storages service
  23. *
  24. * @param BackendService $backendService
  25. * @param DBConfigService $dbConfig
  26. * @param IUserSession $userSession user session
  27. * @param IUserMountCache $userMountCache
  28. * @param IEventDispatcher $eventDispatcher
  29. */
  30. public function __construct(
  31. BackendService $backendService,
  32. DBConfigService $dbConfig,
  33. IUserSession $userSession,
  34. IUserMountCache $userMountCache,
  35. IEventDispatcher $eventDispatcher,
  36. ) {
  37. $this->userSession = $userSession;
  38. parent::__construct($backendService, $dbConfig, $userMountCache, $eventDispatcher);
  39. }
  40. protected function readDBConfig() {
  41. return $this->dbConfig->getUserMountsFor(DBConfigService::APPLICABLE_TYPE_USER, $this->getUser()->getUID());
  42. }
  43. /**
  44. * Triggers $signal for all applicable users of the given
  45. * storage
  46. *
  47. * @param StorageConfig $storage storage data
  48. * @param string $signal signal to trigger
  49. */
  50. protected function triggerHooks(StorageConfig $storage, $signal) {
  51. $user = $this->getUser()->getUID();
  52. // trigger hook for the current user
  53. $this->triggerApplicableHooks(
  54. $signal,
  55. $storage->getMountPoint(),
  56. MountConfig::MOUNT_TYPE_USER,
  57. [$user]
  58. );
  59. }
  60. /**
  61. * Triggers signal_create_mount or signal_delete_mount to
  62. * accommodate for additions/deletions in applicableUsers
  63. * and applicableGroups fields.
  64. *
  65. * @param StorageConfig $oldStorage old storage data
  66. * @param StorageConfig $newStorage new storage data
  67. */
  68. protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage) {
  69. // if mount point changed, it's like a deletion + creation
  70. if ($oldStorage->getMountPoint() !== $newStorage->getMountPoint()) {
  71. $this->triggerHooks($oldStorage, Filesystem::signal_delete_mount);
  72. $this->triggerHooks($newStorage, Filesystem::signal_create_mount);
  73. }
  74. }
  75. protected function getType() {
  76. return DBConfigService::MOUNT_TYPE_PERSONAL;
  77. }
  78. /**
  79. * Add new storage to the configuration
  80. *
  81. * @param StorageConfig $newStorage storage attributes
  82. *
  83. * @return StorageConfig storage config, with added id
  84. */
  85. public function addStorage(StorageConfig $newStorage) {
  86. $newStorage->setApplicableUsers([$this->getUser()->getUID()]);
  87. return parent::addStorage($newStorage);
  88. }
  89. /**
  90. * Update storage to the configuration
  91. *
  92. * @param StorageConfig $updatedStorage storage attributes
  93. *
  94. * @return StorageConfig storage config
  95. * @throws NotFoundException if the given storage does not exist in the config
  96. */
  97. public function updateStorage(StorageConfig $updatedStorage) {
  98. // verify ownership through $this->isApplicable() and otherwise throws an exception
  99. $this->getStorage($updatedStorage->getId());
  100. $updatedStorage->setApplicableUsers([$this->getUser()->getUID()]);
  101. return parent::updateStorage($updatedStorage);
  102. }
  103. /**
  104. * Get the visibility type for this controller, used in validation
  105. *
  106. * @return int BackendService::VISIBILITY_* constants
  107. */
  108. public function getVisibilityType() {
  109. return BackendService::VISIBILITY_PERSONAL;
  110. }
  111. protected function isApplicable(StorageConfig $config) {
  112. return ($config->getApplicableUsers() === [$this->getUser()->getUID()]) && $config->getType() === StorageConfig::MOUNT_TYPE_PERSONAL;
  113. }
  114. public function removeStorage($id) {
  115. // verify ownership through $this->isApplicable() and otherwise throws an exception
  116. $this->getStorage($id);
  117. parent::removeStorage($id);
  118. }
  119. }