UserStoragesService.php 3.9 KB

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