2014-10-31 10:41:07 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2024-06-06 07:55:47 +00:00
|
|
|
* SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2014-10-31 10:41:07 +00:00
|
|
|
*/
|
2016-05-13 09:22:28 +00:00
|
|
|
namespace OCA\Files_External\Service;
|
2014-10-31 10:41:07 +00:00
|
|
|
|
2019-11-22 19:52:10 +00:00
|
|
|
use OC\Files\Filesystem;
|
2016-05-13 09:56:47 +00:00
|
|
|
use OCA\Files_External\Lib\StorageConfig;
|
2024-10-10 10:40:31 +00:00
|
|
|
use OCA\Files_External\MountConfig;
|
2016-05-13 09:56:47 +00:00
|
|
|
use OCA\Files_External\NotFoundException;
|
2022-03-17 17:24:18 +00:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2019-11-22 19:52:10 +00:00
|
|
|
use OCP\Files\Config\IUserMountCache;
|
|
|
|
use OCP\IUserSession;
|
|
|
|
|
2014-10-31 10:41:07 +00:00
|
|
|
/**
|
2021-05-20 10:13:04 +00:00
|
|
|
* Service class to manage user external storage
|
2014-10-31 10:41:07 +00:00
|
|
|
* (aka personal storages)
|
|
|
|
*/
|
|
|
|
class UserStoragesService extends StoragesService {
|
2015-08-12 13:22:27 +00:00
|
|
|
use UserTrait;
|
2014-10-31 10:41:07 +00:00
|
|
|
|
2015-03-17 10:42:52 +00:00
|
|
|
/**
|
|
|
|
* Create a user storages service
|
|
|
|
*
|
2015-08-11 17:45:07 +00:00
|
|
|
* @param BackendService $backendService
|
2015-11-02 12:13:06 +00:00
|
|
|
* @param DBConfigService $dbConfig
|
2015-03-17 10:42:52 +00:00
|
|
|
* @param IUserSession $userSession user session
|
2016-01-13 14:29:53 +00:00
|
|
|
* @param IUserMountCache $userMountCache
|
2022-03-17 17:24:18 +00:00
|
|
|
* @param IEventDispatcher $eventDispatcher
|
2015-03-17 10:42:52 +00:00
|
|
|
*/
|
2014-10-31 10:41:07 +00:00
|
|
|
public function __construct(
|
2015-08-11 17:45:07 +00:00
|
|
|
BackendService $backendService,
|
2015-11-02 12:13:06 +00:00
|
|
|
DBConfigService $dbConfig,
|
2016-01-13 14:29:53 +00:00
|
|
|
IUserSession $userSession,
|
2022-03-17 17:24:18 +00:00
|
|
|
IUserMountCache $userMountCache,
|
|
|
|
IEventDispatcher $eventDispatcher,
|
2014-10-31 10:41:07 +00:00
|
|
|
) {
|
|
|
|
$this->userSession = $userSession;
|
2022-03-17 17:24:18 +00:00
|
|
|
parent::__construct($backendService, $dbConfig, $userMountCache, $eventDispatcher);
|
2014-10-31 10:41:07 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 12:13:06 +00:00
|
|
|
protected function readDBConfig() {
|
|
|
|
return $this->dbConfig->getUserMountsFor(DBConfigService::APPLICABLE_TYPE_USER, $this->getUser()->getUID());
|
2014-10-31 10:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Triggers $signal for all applicable users of the given
|
|
|
|
* storage
|
|
|
|
*
|
|
|
|
* @param StorageConfig $storage storage data
|
|
|
|
* @param string $signal signal to trigger
|
|
|
|
*/
|
|
|
|
protected function triggerHooks(StorageConfig $storage, $signal) {
|
2015-08-12 13:22:27 +00:00
|
|
|
$user = $this->getUser()->getUID();
|
2014-10-31 10:41:07 +00:00
|
|
|
|
|
|
|
// trigger hook for the current user
|
|
|
|
$this->triggerApplicableHooks(
|
|
|
|
$signal,
|
|
|
|
$storage->getMountPoint(),
|
2024-10-10 10:40:31 +00:00
|
|
|
MountConfig::MOUNT_TYPE_USER,
|
2014-10-31 10:41:07 +00:00
|
|
|
[$user]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Triggers signal_create_mount or signal_delete_mount to
|
2016-03-30 11:27:12 +00:00
|
|
|
* accommodate for additions/deletions in applicableUsers
|
2014-10-31 10:41:07 +00:00
|
|
|
* and applicableGroups fields.
|
|
|
|
*
|
|
|
|
* @param StorageConfig $oldStorage old storage data
|
|
|
|
* @param StorageConfig $newStorage new storage data
|
|
|
|
*/
|
|
|
|
protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage) {
|
|
|
|
// if mount point changed, it's like a deletion + creation
|
|
|
|
if ($oldStorage->getMountPoint() !== $newStorage->getMountPoint()) {
|
|
|
|
$this->triggerHooks($oldStorage, Filesystem::signal_delete_mount);
|
|
|
|
$this->triggerHooks($newStorage, Filesystem::signal_create_mount);
|
|
|
|
}
|
|
|
|
}
|
2015-09-23 14:35:17 +00:00
|
|
|
|
2015-11-02 12:13:06 +00:00
|
|
|
protected function getType() {
|
2023-07-10 13:31:47 +00:00
|
|
|
return DBConfigService::MOUNT_TYPE_PERSONAL;
|
2015-11-02 12:13:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add new storage to the configuration
|
|
|
|
*
|
|
|
|
* @param StorageConfig $newStorage storage attributes
|
|
|
|
*
|
|
|
|
* @return StorageConfig storage config, with added id
|
|
|
|
*/
|
|
|
|
public function addStorage(StorageConfig $newStorage) {
|
2015-12-15 12:30:08 +00:00
|
|
|
$newStorage->setApplicableUsers([$this->getUser()->getUID()]);
|
2018-01-25 23:02:03 +00:00
|
|
|
return parent::addStorage($newStorage);
|
2015-11-02 12:13:06 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 12:29:56 +00:00
|
|
|
/**
|
|
|
|
* Update storage to the configuration
|
|
|
|
*
|
|
|
|
* @param StorageConfig $updatedStorage storage attributes
|
|
|
|
*
|
|
|
|
* @return StorageConfig storage config
|
|
|
|
* @throws NotFoundException if the given storage does not exist in the config
|
|
|
|
*/
|
|
|
|
public function updateStorage(StorageConfig $updatedStorage) {
|
2023-10-17 13:22:21 +00:00
|
|
|
// verify ownership through $this->isApplicable() and otherwise throws an exception
|
|
|
|
$this->getStorage($updatedStorage->getId());
|
|
|
|
|
2016-01-05 12:29:56 +00:00
|
|
|
$updatedStorage->setApplicableUsers([$this->getUser()->getUID()]);
|
|
|
|
return parent::updateStorage($updatedStorage);
|
|
|
|
}
|
|
|
|
|
2015-09-23 14:35:17 +00:00
|
|
|
/**
|
|
|
|
* Get the visibility type for this controller, used in validation
|
|
|
|
*
|
2022-10-16 22:59:08 +00:00
|
|
|
* @return int BackendService::VISIBILITY_* constants
|
2015-09-23 14:35:17 +00:00
|
|
|
*/
|
|
|
|
public function getVisibilityType() {
|
|
|
|
return BackendService::VISIBILITY_PERSONAL;
|
|
|
|
}
|
2016-01-05 15:40:34 +00:00
|
|
|
|
|
|
|
protected function isApplicable(StorageConfig $config) {
|
2023-07-10 13:31:47 +00:00
|
|
|
return ($config->getApplicableUsers() === [$this->getUser()->getUID()]) && $config->getType() === StorageConfig::MOUNT_TYPE_PERSONAL;
|
2016-01-05 15:40:34 +00:00
|
|
|
}
|
2023-07-11 18:36:17 +00:00
|
|
|
|
|
|
|
public function removeStorage($id) {
|
|
|
|
// verify ownership through $this->isApplicable() and otherwise throws an exception
|
|
|
|
$this->getStorage($id);
|
|
|
|
parent::removeStorage($id);
|
|
|
|
}
|
2014-10-31 10:41:07 +00:00
|
|
|
}
|