userstoragesservice.php 4.3 KB

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