UserStoragesService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Robin McCorkell <robin@mccorkell.me.uk>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Stefan Weil <sw@weilnetz.de>
  11. * @author szaimen <szaimen@e.mail.de>
  12. * @author Vincent Petry <vincent@nextcloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\Files_External\Service;
  30. use OC\Files\Filesystem;
  31. use OCA\Files_External\Lib\StorageConfig;
  32. use OCA\Files_External\NotFoundException;
  33. use OCP\EventDispatcher\IEventDispatcher;
  34. use OCP\Files\Config\IUserMountCache;
  35. use OCP\IUserSession;
  36. /**
  37. * Service class to manage user external storage
  38. * (aka personal storages)
  39. */
  40. class UserStoragesService extends StoragesService {
  41. use UserTrait;
  42. /**
  43. * Create a user storages service
  44. *
  45. * @param BackendService $backendService
  46. * @param DBConfigService $dbConfig
  47. * @param IUserSession $userSession user session
  48. * @param IUserMountCache $userMountCache
  49. * @param IEventDispatcher $eventDispatcher
  50. */
  51. public function __construct(
  52. BackendService $backendService,
  53. DBConfigService $dbConfig,
  54. IUserSession $userSession,
  55. IUserMountCache $userMountCache,
  56. IEventDispatcher $eventDispatcher
  57. ) {
  58. $this->userSession = $userSession;
  59. parent::__construct($backendService, $dbConfig, $userMountCache, $eventDispatcher);
  60. }
  61. protected function readDBConfig() {
  62. return $this->dbConfig->getUserMountsFor(DBConfigService::APPLICABLE_TYPE_USER, $this->getUser()->getUID());
  63. }
  64. /**
  65. * Triggers $signal for all applicable users of the given
  66. * storage
  67. *
  68. * @param StorageConfig $storage storage data
  69. * @param string $signal signal to trigger
  70. */
  71. protected function triggerHooks(StorageConfig $storage, $signal) {
  72. $user = $this->getUser()->getUID();
  73. // trigger hook for the current user
  74. $this->triggerApplicableHooks(
  75. $signal,
  76. $storage->getMountPoint(),
  77. \OCA\Files_External\MountConfig::MOUNT_TYPE_USER,
  78. [$user]
  79. );
  80. }
  81. /**
  82. * Triggers signal_create_mount or signal_delete_mount to
  83. * accommodate for additions/deletions in applicableUsers
  84. * and applicableGroups fields.
  85. *
  86. * @param StorageConfig $oldStorage old storage data
  87. * @param StorageConfig $newStorage new storage data
  88. */
  89. protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage) {
  90. // if mount point changed, it's like a deletion + creation
  91. if ($oldStorage->getMountPoint() !== $newStorage->getMountPoint()) {
  92. $this->triggerHooks($oldStorage, Filesystem::signal_delete_mount);
  93. $this->triggerHooks($newStorage, Filesystem::signal_create_mount);
  94. }
  95. }
  96. protected function getType() {
  97. return DBConfigService::MOUNT_TYPE_PERSONAL;
  98. }
  99. /**
  100. * Add new storage to the configuration
  101. *
  102. * @param StorageConfig $newStorage storage attributes
  103. *
  104. * @return StorageConfig storage config, with added id
  105. */
  106. public function addStorage(StorageConfig $newStorage) {
  107. $newStorage->setApplicableUsers([$this->getUser()->getUID()]);
  108. return parent::addStorage($newStorage);
  109. }
  110. /**
  111. * Update storage to the configuration
  112. *
  113. * @param StorageConfig $updatedStorage storage attributes
  114. *
  115. * @return StorageConfig storage config
  116. * @throws NotFoundException if the given storage does not exist in the config
  117. */
  118. public function updateStorage(StorageConfig $updatedStorage) {
  119. // verify ownership through $this->isApplicable() and otherwise throws an exception
  120. $this->getStorage($updatedStorage->getId());
  121. $updatedStorage->setApplicableUsers([$this->getUser()->getUID()]);
  122. return parent::updateStorage($updatedStorage);
  123. }
  124. /**
  125. * Get the visibility type for this controller, used in validation
  126. *
  127. * @return int BackendService::VISIBILITY_* constants
  128. */
  129. public function getVisibilityType() {
  130. return BackendService::VISIBILITY_PERSONAL;
  131. }
  132. protected function isApplicable(StorageConfig $config) {
  133. return ($config->getApplicableUsers() === [$this->getUser()->getUID()]) && $config->getType() === StorageConfig::MOUNT_TYPE_PERSONAL;
  134. }
  135. public function removeStorage($id) {
  136. // verify ownership through $this->isApplicable() and otherwise throws an exception
  137. $this->getStorage($id);
  138. parent::removeStorage($id);
  139. }
  140. }