Merge pull request #20370 from owncloud/external-db-config
Move files_external mount config to the databaseremotes/origin/fix_emit_scanFiles
commit
e91eba4b5b
@ -0,0 +1,222 @@
|
||||
<database>
|
||||
<name>*dbname*</name>
|
||||
<create>true</create>
|
||||
<overwrite>false</overwrite>
|
||||
<charset>utf8</charset>
|
||||
<table>
|
||||
<name>*dbprefix*external_mounts</name>
|
||||
<declaration>
|
||||
<field>
|
||||
<name>mount_id</name>
|
||||
<type>integer</type>
|
||||
<default>0</default>
|
||||
<notnull>true</notnull>
|
||||
<autoincrement>1</autoincrement>
|
||||
<length>6</length>
|
||||
</field>
|
||||
<field>
|
||||
<name>mount_point</name>
|
||||
<type>text</type>
|
||||
<length>128</length>
|
||||
<notnull>true</notnull>
|
||||
</field>
|
||||
<field>
|
||||
<name>storage_backend</name>
|
||||
<type>text</type>
|
||||
<length>64</length>
|
||||
<notnull>true</notnull>
|
||||
</field>
|
||||
<field>
|
||||
<name>auth_backend</name>
|
||||
<type>text</type>
|
||||
<length>64</length>
|
||||
<notnull>true</notnull>
|
||||
</field>
|
||||
<field>
|
||||
<name>priority</name>
|
||||
<type>integer</type>
|
||||
<default>100</default>
|
||||
<length>4</length>
|
||||
<notnull>true</notnull>
|
||||
</field>
|
||||
<!-- admin = 1, personal = 2-->
|
||||
<field>
|
||||
<name>type</name>
|
||||
<type>integer</type>
|
||||
<length>4</length>
|
||||
<notnull>true</notnull>
|
||||
</field>
|
||||
</declaration>
|
||||
</table>
|
||||
<table>
|
||||
<name>*dbprefix*external_applicable</name>
|
||||
<declaration>
|
||||
<field>
|
||||
<name>applicable_id</name>
|
||||
<type>integer</type>
|
||||
<default>0</default>
|
||||
<notnull>true</notnull>
|
||||
<autoincrement>1</autoincrement>
|
||||
<length>6</length>
|
||||
</field>
|
||||
<field>
|
||||
<!--foreign key: external_mounts.mount_id-->
|
||||
<name>mount_id</name>
|
||||
<type>integer</type>
|
||||
<notnull>true</notnull>
|
||||
<length>6</length>
|
||||
</field>
|
||||
<field>
|
||||
<!-- possible mount types: global = 1, group = 2, user = 3 -->
|
||||
<name>type</name>
|
||||
<type>integer</type>
|
||||
<length>4</length>
|
||||
<notnull>true</notnull>
|
||||
</field>
|
||||
<field>
|
||||
<!-- user_id, group_id or null for global mounts -->
|
||||
<name>value</name>
|
||||
<type>text</type>
|
||||
<length>64</length>
|
||||
</field>
|
||||
<index>
|
||||
<name>mount_id_app_index</name>
|
||||
<field>
|
||||
<name>mount_id</name>
|
||||
<sorting>ascending</sorting>
|
||||
</field>
|
||||
</index>
|
||||
<index>
|
||||
<name>applicable_value_index</name>
|
||||
<field>
|
||||
<name>type</name>
|
||||
<sorting>ascending</sorting>
|
||||
</field>
|
||||
<field>
|
||||
<name>value</name>
|
||||
<sorting>ascending</sorting>
|
||||
</field>
|
||||
</index>
|
||||
<index>
|
||||
<name>applicable_value_mount_index</name>
|
||||
<unique>true</unique>
|
||||
<field>
|
||||
<name>type</name>
|
||||
<sorting>ascending</sorting>
|
||||
</field>
|
||||
<field>
|
||||
<name>value</name>
|
||||
<sorting>ascending</sorting>
|
||||
</field>
|
||||
<field>
|
||||
<name>mount_id</name>
|
||||
<sorting>ascending</sorting>
|
||||
</field>
|
||||
</index>
|
||||
</declaration>
|
||||
</table>
|
||||
<table>
|
||||
<name>*dbprefix*external_config</name>
|
||||
<declaration>
|
||||
<field>
|
||||
<name>config_id</name>
|
||||
<type>integer</type>
|
||||
<default>0</default>
|
||||
<notnull>true</notnull>
|
||||
<autoincrement>1</autoincrement>
|
||||
<length>6</length>
|
||||
</field>
|
||||
<field>
|
||||
<!--foreign key: external_mounts.mount_id-->
|
||||
<name>mount_id</name>
|
||||
<type>integer</type>
|
||||
<notnull>true</notnull>
|
||||
<length>6</length>
|
||||
</field>
|
||||
<field>
|
||||
<name>key</name>
|
||||
<type>text</type>
|
||||
<notnull>true</notnull>
|
||||
<length>64</length>
|
||||
</field>
|
||||
<field>
|
||||
<name>value</name>
|
||||
<type>text</type>
|
||||
<notnull>true</notnull>
|
||||
<length>4096</length>
|
||||
</field>
|
||||
|
||||
<index>
|
||||
<name>config_mount_id</name>
|
||||
<field>
|
||||
<name>mount_id</name>
|
||||
<sorting>ascending</sorting>
|
||||
</field>
|
||||
</index>
|
||||
<index>
|
||||
<name>config_mount_key</name>
|
||||
<unique>true</unique>
|
||||
<field>
|
||||
<name>mount_id</name>
|
||||
<sorting>ascending</sorting>
|
||||
</field>
|
||||
<field>
|
||||
<name>key</name>
|
||||
<sorting>ascending</sorting>
|
||||
</field>
|
||||
</index>
|
||||
</declaration>
|
||||
</table>
|
||||
<table>
|
||||
<name>*dbprefix*external_options</name>
|
||||
<declaration>
|
||||
<field>
|
||||
<name>option_id</name>
|
||||
<type>integer</type>
|
||||
<default>0</default>
|
||||
<notnull>true</notnull>
|
||||
<autoincrement>1</autoincrement>
|
||||
<length>6</length>
|
||||
</field>
|
||||
<field>
|
||||
<!--foreign key: external_mounts.mount_id-->
|
||||
<name>mount_id</name>
|
||||
<type>integer</type>
|
||||
<notnull>true</notnull>
|
||||
<length>6</length>
|
||||
</field>
|
||||
<field>
|
||||
<name>key</name>
|
||||
<type>text</type>
|
||||
<notnull>true</notnull>
|
||||
<length>64</length>
|
||||
</field>
|
||||
<field>
|
||||
<name>value</name>
|
||||
<type>text</type>
|
||||
<notnull>true</notnull>
|
||||
<length>256</length>
|
||||
</field>
|
||||
|
||||
<index>
|
||||
<name>option_mount_id</name>
|
||||
<field>
|
||||
<name>mount_id</name>
|
||||
<sorting>ascending</sorting>
|
||||
</field>
|
||||
</index>
|
||||
<index>
|
||||
<name>option_mount_key</name>
|
||||
<unique>true</unique>
|
||||
<field>
|
||||
<name>mount_id</name>
|
||||
<sorting>ascending</sorting>
|
||||
</field>
|
||||
<field>
|
||||
<name>key</name>
|
||||
<sorting>ascending</sorting>
|
||||
</field>
|
||||
</index>
|
||||
</declaration>
|
||||
</table>
|
||||
</database>
|
||||
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Robin Appelman <icewind@owncloud.com>
|
||||
*
|
||||
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
||||
* @license AGPL-3.0
|
||||
*
|
||||
* This code is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3,
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License, version 3,
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
|
||||
$installedVersion = \OC::$server->getConfig()->getAppValue('files_external', 'installed_version');
|
||||
|
||||
$app = new \OCA\Files_external\Appinfo\Application();
|
||||
|
||||
// Migration to db config
|
||||
if (version_compare($installedVersion, '0.5.0', '<')) {
|
||||
$migrator = $app->getContainer()->query('OCA\Files_external\Migration\StorageMigrator');
|
||||
$migrator->migrateGlobal();
|
||||
}
|
||||
@ -0,0 +1,136 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Robin Appelman <icewind@owncloud.com>
|
||||
*
|
||||
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
||||
* @license AGPL-3.0
|
||||
*
|
||||
* This code is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3,
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License, version 3,
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Files_external\Migration;
|
||||
|
||||
use OCA\Files_External\Service\BackendService;
|
||||
use OCA\Files_External\Service\DBConfigService;
|
||||
use OCA\Files_external\Service\GlobalLegacyStoragesService;
|
||||
use OCA\Files_external\Service\GlobalStoragesService;
|
||||
use OCA\Files_external\Service\LegacyStoragesService;
|
||||
use OCA\Files_external\Service\StoragesService;
|
||||
use OCA\Files_external\Service\UserLegacyStoragesService;
|
||||
use OCA\Files_external\Service\UserStoragesService;
|
||||
use OCP\IConfig;
|
||||
use OCP\IDBConnection;
|
||||
use OCP\ILogger;
|
||||
use OCP\IUserSession;
|
||||
|
||||
/**
|
||||
* Migrate mount config from mount.json to the database
|
||||
*/
|
||||
class StorageMigrator {
|
||||
/**
|
||||
* @var BackendService
|
||||
*/
|
||||
private $backendService;
|
||||
|
||||
/**
|
||||
* @var DBConfigService
|
||||
*/
|
||||
private $dbConfig;
|
||||
|
||||
/**
|
||||
* @var IUserSession
|
||||
*/
|
||||
private $userSession;
|
||||
|
||||
/**
|
||||
* @var IConfig
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @var IDBConnection
|
||||
*/
|
||||
private $connection;
|
||||
|
||||
/**
|
||||
* @var ILogger
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* StorageMigrator constructor.
|
||||
*
|
||||
* @param BackendService $backendService
|
||||
* @param DBConfigService $dbConfig
|
||||
* @param IUserSession $userSession
|
||||
* @param IConfig $config
|
||||
* @param IDBConnection $connection
|
||||
* @param ILogger $logger
|
||||
*/
|
||||
public function __construct(
|
||||
BackendService $backendService,
|
||||
DBConfigService $dbConfig,
|
||||
IUserSession $userSession,
|
||||
IConfig $config,
|
||||
IDBConnection $connection,
|
||||
ILogger $logger
|
||||
) {
|
||||
$this->backendService = $backendService;
|
||||
$this->dbConfig = $dbConfig;
|
||||
$this->userSession = $userSession;
|
||||
$this->config = $config;
|
||||
$this->connection = $connection;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
private function migrate(LegacyStoragesService $legacyService, StoragesService $storageService) {
|
||||
$existingStorage = $legacyService->getAllStorages();
|
||||
|
||||
$this->connection->beginTransaction();
|
||||
try {
|
||||
foreach ($existingStorage as $storage) {
|
||||
$storageService->addStorage($storage);
|
||||
}
|
||||
$this->connection->commit();
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->logException($e);
|
||||
$this->connection->rollBack();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Migrate admin configured storages
|
||||
*/
|
||||
public function migrateGlobal() {
|
||||
$legacyService = new GlobalLegacyStoragesService($this->backendService);
|
||||
$storageService = new GlobalStoragesService($this->backendService, $this->dbConfig);
|
||||
|
||||
$this->migrate($legacyService, $storageService);
|
||||
}
|
||||
|
||||
/**
|
||||
* Migrate personal storages configured by the current user
|
||||
*/
|
||||
public function migrateUser() {
|
||||
$userId = $this->userSession->getUser()->getUID();
|
||||
$userVersion = $this->config->getUserValue($userId, 'files_external', 'config_version', '0.0.0');
|
||||
if (version_compare($userVersion, '0.5.0', '<')) {
|
||||
$this->config->setUserValue($userId, 'files_external', 'config_version', '0.5.0');
|
||||
$legacyService = new UserLegacyStoragesService($this->backendService, $this->userSession);
|
||||
$storageService = new UserStoragesService($this->backendService, $this->dbConfig, $this->userSession);
|
||||
|
||||
$this->migrate($legacyService, $storageService);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,376 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Robin Appelman <icewind@owncloud.com>
|
||||
*
|
||||
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
||||
* @license AGPL-3.0
|
||||
*
|
||||
* This code is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3,
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License, version 3,
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Files_External\Service;
|
||||
|
||||
use OCP\DB\QueryBuilder\IQueryBuilder;
|
||||
use OCP\IDBConnection;
|
||||
|
||||
/**
|
||||
* Stores the mount config in the database
|
||||
*/
|
||||
class DBConfigService {
|
||||
const MOUNT_TYPE_ADMIN = 1;
|
||||
const MOUNT_TYPE_PERSONAl = 2;
|
||||
|
||||
const APPLICABLE_TYPE_GLOBAL = 1;
|
||||
const APPLICABLE_TYPE_GROUP = 2;
|
||||
const APPLICABLE_TYPE_USER = 3;
|
||||
|
||||
/**
|
||||
* @var IDBConnection
|
||||
*/
|
||||
private $connection;
|
||||
|
||||
/**
|
||||
* DBConfigService constructor.
|
||||
*
|
||||
* @param IDBConnection $connection
|
||||
*/
|
||||
public function __construct(IDBConnection $connection) {
|
||||
$this->connection = $connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $mountId
|
||||
* @return array
|
||||
*/
|
||||
public function getMountById($mountId) {
|
||||
$builder = $this->connection->getQueryBuilder();
|
||||
$query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type'])
|
||||
->from('external_mounts', 'm')
|
||||
->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, \PDO::PARAM_INT)));
|
||||
$mounts = $this->getMountsFromQuery($query);
|
||||
if (count($mounts) > 0) {
|
||||
return $mounts[0];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get admin defined mounts
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getAdminMounts() {
|
||||
$builder = $this->connection->getQueryBuilder();
|
||||
$query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type'])
|
||||
->from('external_mounts')
|
||||
->where($builder->expr()->eq('type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, \PDO::PARAM_INT)));
|
||||
return $this->getMountsFromQuery($query);
|
||||
}
|
||||
|
||||
protected function getForQuery(IQueryBuilder $builder, $type, $value) {
|
||||
$query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type'])
|
||||
->from('external_mounts', 'm')
|
||||
->innerJoin('m', 'external_applicable', 'a', 'm.mount_id = a.mount_id')
|
||||
->where($builder->expr()->eq('a.type', $builder->createNamedParameter($type, \PDO::PARAM_INT)));
|
||||
|
||||
if (is_null($value)) {
|
||||
$query = $query->andWhere($builder->expr()->isNull('a.value'));
|
||||
} else {
|
||||
$query = $query->andWhere($builder->expr()->eq('a.value', $builder->createNamedParameter($value)));
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get mounts by applicable
|
||||
*
|
||||
* @param int $type any of the self::APPLICABLE_TYPE_ constants
|
||||
* @param string|null $value user_id, group_id or null for global mounts
|
||||
* @return array
|
||||
*/
|
||||
public function getMountsFor($type, $value) {
|
||||
$builder = $this->connection->getQueryBuilder();
|
||||
$query = $this->getForQuery($builder, $type, $value);
|
||||
|
||||
return $this->getMountsFromQuery($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get admin defined mounts by applicable
|
||||
*
|
||||
* @param int $type any of the self::APPLICABLE_TYPE_ constants
|
||||
* @param string|null $value user_id, group_id or null for global mounts
|
||||
* @return array
|
||||
*/
|
||||
public function getAdminMountsFor($type, $value) {
|
||||
$builder = $this->connection->getQueryBuilder();
|
||||
$query = $this->getForQuery($builder, $type, $value);
|
||||
$query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, \PDO::PARAM_INT)));
|
||||
|
||||
return $this->getMountsFromQuery($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get admin defined mounts for multiple applicable
|
||||
*
|
||||
* @param int $type any of the self::APPLICABLE_TYPE_ constants
|
||||
* @param string[] $values user_ids or group_ids
|
||||
* @return array
|
||||
*/
|
||||
public function getAdminMountsForMultiple($type, array $values) {
|
||||
$builder = $this->connection->getQueryBuilder();
|
||||
$params = array_map(function ($value) use ($builder) {
|
||||
return $builder->createNamedParameter($value, \PDO::PARAM_STR);
|
||||
}, $values);
|
||||
|
||||
$query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type'])
|
||||
->from('external_mounts', 'm')
|
||||
->innerJoin('m', 'external_applicable', 'a', 'm.mount_id = a.mount_id')
|
||||
->where($builder->expr()->eq('a.type', $builder->createNamedParameter($type, \PDO::PARAM_INT)))
|
||||
->andWhere($builder->expr()->in('a.value', $params));
|
||||
$query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, \PDO::PARAM_INT)));
|
||||
|
||||
return $this->getMountsFromQuery($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user defined mounts by applicable
|
||||
*
|
||||
* @param int $type any of the self::APPLICABLE_TYPE_ constants
|
||||
* @param string|null $value user_id, group_id or null for global mounts
|
||||
* @return array
|
||||
*/
|
||||
public function getUserMountsFor($type, $value) {
|
||||
$builder = $this->connection->getQueryBuilder();
|
||||
$query = $this->getForQuery($builder, $type, $value);
|
||||
$query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_PERSONAl, \PDO::PARAM_INT)));
|
||||
|
||||
return $this->getMountsFromQuery($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a mount to the database
|
||||
*
|
||||
* @param string $mountPoint
|
||||
* @param string $storageBackend
|
||||
* @param string $authBackend
|
||||
* @param int $priority
|
||||
* @param int $type self::MOUNT_TYPE_ADMIN or self::MOUNT_TYPE_PERSONAL
|
||||
* @return int the id of the new mount
|
||||
*/
|
||||
public function addMount($mountPoint, $storageBackend, $authBackend, $priority, $type) {
|
||||
if (!$priority) {
|
||||
$priority = 100;
|
||||
}
|
||||
$builder = $this->connection->getQueryBuilder();
|
||||
$query = $builder->insert('external_mounts')
|
||||
->values([
|
||||
'mount_point' => $builder->createNamedParameter($mountPoint, \PDO::PARAM_STR),
|
||||
'storage_backend' => $builder->createNamedParameter($storageBackend, \PDO::PARAM_STR),
|
||||
'auth_backend' => $builder->createNamedParameter($authBackend, \PDO::PARAM_STR),
|
||||
'priority' => $builder->createNamedParameter($priority, \PDO::PARAM_INT),
|
||||
'type' => $builder->createNamedParameter($type, \PDO::PARAM_INT)
|
||||
]);
|
||||
$query->execute();
|
||||
return (int)$this->connection->lastInsertId('external_mounts');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a mount from the database
|
||||
*
|
||||
* @param int $mountId
|
||||
*/
|
||||
public function removeMount($mountId) {
|
||||
$builder = $this->connection->getQueryBuilder();
|
||||
$query = $builder->delete('external_mounts')
|
||||
->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, \PDO::PARAM_INT)));
|
||||
$query->execute();
|
||||
|
||||
$query = $builder->delete('external_applicable')
|
||||
->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, \PDO::PARAM_INT)));
|
||||
$query->execute();
|
||||
|
||||
$query = $builder->delete('external_config')
|
||||
->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, \PDO::PARAM_INT)));
|
||||
$query->execute();
|
||||
|
||||
$query = $builder->delete('external_options')
|
||||
->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, \PDO::PARAM_INT)));
|
||||
$query->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $mountId
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
*/
|
||||
public function setConfig($mountId, $key, $value) {
|
||||
$count = $this->connection->insertIfNotExist('*PREFIX*external_config', [
|
||||
'mount_id' => $mountId,
|
||||
'key' => $key,
|
||||
'value' => $value
|
||||
], ['mount_id', 'key']);
|
||||
if ($count === 0) {
|
||||
$builder = $this->connection->getQueryBuilder();
|
||||
$query = $builder->update('external_config')
|
||||
->set('value', $builder->createNamedParameter($value, \PDO::PARAM_STR))
|
||||
->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, \PDO::PARAM_INT)))
|
||||
->andWhere($builder->expr()->eq('key', $builder->createNamedParameter($key, \PDO::PARAM_STR)));
|
||||
$query->execute();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $mountId
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
*/
|
||||
public function setOption($mountId, $key, $value) {
|
||||
$count = $this->connection->insertIfNotExist('*PREFIX*external_options', [
|
||||
'mount_id' => $mountId,
|
||||
'key' => $key,
|
||||
'value' => json_encode($value)
|
||||
], ['mount_id', 'key']);
|
||||
if ($count === 0) {
|
||||
$builder = $this->connection->getQueryBuilder();
|
||||
$query = $builder->update('external_options')
|
||||
->set('value', $builder->createNamedParameter(json_encode($value), \PDO::PARAM_STR))
|
||||
->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, \PDO::PARAM_INT)))
|
||||
->andWhere($builder->expr()->eq('key', $builder->createNamedParameter($key, \PDO::PARAM_STR)));
|
||||
$query->execute();
|
||||
}
|
||||
}
|
||||
|
||||
public function addApplicable($mountId, $type, $value) {
|
||||
$this->connection->insertIfNotExist('*PREFIX*external_applicable', [
|
||||
'mount_id' => $mountId,
|
||||
'type' => $type,
|
||||
'value' => $value
|
||||
], ['mount_id', 'type', 'value']);
|
||||
}
|
||||
|
||||
public function removeApplicable($mountId, $type, $value) {
|
||||
$builder = $this->connection->getQueryBuilder();
|
||||
$query = $builder->delete('external_applicable')
|
||||
->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, \PDO::PARAM_INT)))
|
||||
->andWhere($builder->expr()->eq('type', $builder->createNamedParameter($type, \PDO::PARAM_INT)))
|
||||
->andWhere($builder->expr()->eq('value', $builder->createNamedParameter($value, \PDO::PARAM_STR)));
|
||||
$query->execute();
|
||||
}
|
||||
|
||||
private function getMountsFromQuery(IQueryBuilder $query) {
|
||||
$result = $query->execute();
|
||||
$mounts = $result->fetchAll();
|
||||
|
||||
$mountIds = array_map(function ($mount) {
|
||||
return $mount['mount_id'];
|
||||
}, $mounts);
|
||||
|
||||
$applicable = $this->getApplicableForMounts($mountIds);
|
||||
$config = $this->getConfigForMounts($mountIds);
|
||||
$options = $this->getOptionsForMounts($mountIds);
|
||||
|
||||
return array_map(function ($mount, $applicable, $config, $options) {
|
||||
$mount['type'] = (int)$mount['type'];
|
||||
$mount['priority'] = (int)$mount['priority'];
|
||||
$mount['applicable'] = $applicable;
|
||||
$mount['config'] = $config;
|
||||
$mount['options'] = $options;
|
||||
return $mount;
|
||||
}, $mounts, $applicable, $config, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get mount options from a table grouped by mount id
|
||||
*
|
||||
* @param string $table
|
||||
* @param string[] $fields
|
||||
* @param int[] $mountIds
|
||||
* @return array [$mountId => [['field1' => $value1, ...], ...], ...]
|
||||
*/
|
||||
private function selectForMounts($table, array $fields, array $mountIds) {
|
||||
if (count($mountIds) === 0) {
|
||||
return [];
|
||||
}
|
||||
$builder = $this->connection->getQueryBuilder();
|
||||
$fields[] = 'mount_id';
|
||||
$placeHolders = array_map(function ($id) use ($builder) {
|
||||
return $builder->createPositionalParameter($id, \PDO::PARAM_INT);
|
||||
}, $mountIds);
|
||||
$query = $builder->select($fields)
|
||||
->from($table)
|
||||
->where($builder->expr()->in('mount_id', $placeHolders));
|
||||
$rows = $query->execute()->fetchAll();
|
||||
|
||||
$result = [];
|
||||
foreach ($mountIds as $mountId) {
|
||||
$result[$mountId] = [];
|
||||
}
|
||||
foreach ($rows as $row) {
|
||||
if (isset($row['type'])) {
|
||||
$row['type'] = (int)$row['type'];
|
||||
}
|
||||
$result[$row['mount_id']][] = $row;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int[] $mountIds
|
||||
* @return array [$id => [['type' => $type, 'value' => $value], ...], ...]
|
||||
*/
|
||||
public function getApplicableForMounts($mountIds) {
|
||||
return $this->selectForMounts('external_applicable', ['type', 'value'], $mountIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int[] $mountIds
|
||||
* @return array [$id => ['key1' => $value1, ...], ...]
|
||||
*/
|
||||
public function getConfigForMounts($mountIds) {
|
||||
$mountConfigs = $this->selectForMounts('external_config', ['key', 'value'], $mountIds);
|
||||
return array_map([$this, 'createKeyValueMap'], $mountConfigs);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int[] $mountIds
|
||||
* @return array [$id => ['key1' => $value1, ...], ...]
|
||||
*/
|
||||
public function getOptionsForMounts($mountIds) {
|
||||
$mountOptions = $this->selectForMounts('external_options', ['key', 'value'], $mountIds);
|
||||
$optionsMap = array_map([$this, 'createKeyValueMap'], $mountOptions);
|
||||
return array_map(function (array $options) {
|
||||
return array_map(function ($option) {
|
||||
return json_decode($option);
|
||||
}, $options);
|
||||
}, $optionsMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $keyValuePairs [['key'=>$key, 'value=>$value], ...]
|
||||
* @return array ['key1' => $value1, ...]
|
||||
*/
|
||||
private function createKeyValueMap(array $keyValuePairs) {
|
||||
$keys = array_map(function ($pair) {
|
||||
return $pair['key'];
|
||||
}, $keyValuePairs);
|
||||
$values = array_map(function ($pair) {
|
||||
return $pair['value'];
|
||||
}, $keyValuePairs);
|
||||
|
||||
return array_combine($keys, $values);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Robin Appelman <icewind@owncloud.com>
|
||||
*
|
||||
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
||||
* @license AGPL-3.0
|
||||
*
|
||||
* This code is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3,
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License, version 3,
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Files_external\Service;
|
||||
|
||||
/**
|
||||
* Read admin defined mounts from the legacy mount.json
|
||||
*/
|
||||
class GlobalLegacyStoragesService extends LegacyStoragesService {
|
||||
/**
|
||||
* @param BackendService $backendService
|
||||
*/
|
||||
public function __construct(BackendService $backendService) {
|
||||
$this->backendService = $backendService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read legacy config data
|
||||
*
|
||||
* @return array list of mount configs
|
||||
*/
|
||||
protected function readLegacyConfig() {
|
||||
// read global config
|
||||
return \OC_Mount_Config::readData();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,209 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Robin Appelman <icewind@owncloud.com>
|
||||
*
|
||||
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
||||
* @license AGPL-3.0
|
||||
*
|
||||
* This code is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3,
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License, version 3,
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Files_external\Service;
|
||||
|
||||
use \OCA\Files_external\Lib\StorageConfig;
|
||||
|
||||
/**
|
||||
* Read mount config from legacy mount.json
|
||||
*/
|
||||
abstract class LegacyStoragesService {
|
||||
/** @var BackendService */
|
||||
protected $backendService;
|
||||
|
||||
/**
|
||||
* Read legacy config data
|
||||
*
|
||||
* @return array list of mount configs
|
||||
*/
|
||||
abstract protected function readLegacyConfig();
|
||||
|
||||
/**
|
||||
* Copy legacy storage options into the given storage config object.
|
||||
*
|
||||
* @param StorageConfig $storageConfig storage config to populate
|
||||
* @param string $mountType mount type
|
||||
* @param string $applicable applicable user or group
|
||||
* @param array $storageOptions legacy storage options
|
||||
*
|
||||
* @return StorageConfig populated storage config
|
||||
*/
|
||||
protected function populateStorageConfigWithLegacyOptions(
|
||||
&$storageConfig,
|
||||
$mountType,
|
||||
$applicable,
|
||||
$storageOptions
|
||||
) {
|
||||
$backend = $this->backendService->getBackend($storageOptions['backend']);
|
||||
if (!$backend) {
|
||||
throw new \UnexpectedValueException('Invalid backend ' . $storageOptions['backend']);
|
||||
}
|
||||
$storageConfig->setBackend($backend);
|
||||
if (isset($storageOptions['authMechanism']) && $storageOptions['authMechanism'] !== 'builtin::builtin') {
|
||||
$authMechanism = $this->backendService->getAuthMechanism($storageOptions['authMechanism']);
|
||||
} else {
|
||||
$authMechanism = $backend->getLegacyAuthMechanism($storageOptions);
|
||||
$storageOptions['authMechanism'] = 'null'; // to make error handling easier
|
||||
}
|
||||
if (!$authMechanism) {
|
||||
throw new \UnexpectedValueException('Invalid authentication mechanism ' . $storageOptions['authMechanism']);
|
||||
}
|
||||
$storageConfig->setAuthMechanism($authMechanism);
|
||||
$storageConfig->setBackendOptions($storageOptions['options']);
|
||||
if (isset($storageOptions['mountOptions'])) {
|
||||
$storageConfig->setMountOptions($storageOptions['mountOptions']);
|
||||
}
|
||||
if (!isset($storageOptions['priority'])) {
|
||||
$storageOptions['priority'] = $backend->getPriority();
|
||||
}
|
||||
$storageConfig->setPriority($storageOptions['priority']);
|
||||
if ($mountType === \OC_Mount_Config::MOUNT_TYPE_USER) {
|
||||
$applicableUsers = $storageConfig->getApplicableUsers();
|
||||
if ($applicable !== 'all') {
|
||||
$applicableUsers[] = $applicable;
|
||||
$storageConfig->setApplicableUsers($applicableUsers);
|
||||
}
|
||||
} else if ($mountType === \OC_Mount_Config::MOUNT_TYPE_GROUP) {
|
||||
$applicableGroups = $storageConfig->getApplicableGroups();
|
||||
$applicableGroups[] = $applicable;
|
||||
$storageConfig->setApplicableGroups($applicableGroups);
|
||||
}
|
||||
return $storageConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the external storages config
|
||||
*
|
||||
* @return StorageConfig[] map of storage id to storage config
|
||||
*/
|
||||
public function getAllStorages() {
|
||||
$mountPoints = $this->readLegacyConfig();
|
||||
/**
|
||||
* Here is the how the horribly messy mount point array looks like
|
||||
* from the mount.json file:
|
||||
*
|
||||
* $storageOptions = $mountPoints[$mountType][$applicable][$mountPath]
|
||||
*
|
||||
* - $mountType is either "user" or "group"
|
||||
* - $applicable is the name of a user or group (or the current user for personal mounts)
|
||||
* - $mountPath is the mount point path (where the storage must be mounted)
|
||||
* - $storageOptions is a map of storage options:
|
||||
* - "priority": storage priority
|
||||
* - "backend": backend identifier
|
||||
* - "class": LEGACY backend class name
|
||||
* - "options": backend-specific options
|
||||
* - "authMechanism": authentication mechanism identifier
|
||||
* - "mountOptions": mount-specific options (ex: disable previews, scanner, etc)
|
||||
*/
|
||||
// group by storage id
|
||||
/** @var StorageConfig[] $storages */
|
||||
$storages = [];
|
||||
// for storages without id (legacy), group by config hash for
|
||||
// later processing
|
||||
$storagesWithConfigHash = [];
|
||||
foreach ($mountPoints as $mountType => $applicables) {
|
||||
foreach ($applicables as $applicable => $mountPaths) {
|
||||
foreach ($mountPaths as $rootMountPath => $storageOptions) {
|
||||
$currentStorage = null;
|
||||
/**
|
||||
* Flag whether the config that was read already has an id.
|
||||
* If not, it will use a config hash instead and generate
|
||||
* a proper id later
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
$hasId = false;
|
||||
// the root mount point is in the format "/$user/files/the/mount/point"
|
||||
// we remove the "/$user/files" prefix
|
||||
$parts = explode('/', ltrim($rootMountPath, '/'), 3);
|
||||
if (count($parts) < 3) {
|
||||
// something went wrong, skip
|
||||
\OCP\Util::writeLog(
|
||||
'files_external',
|
||||
'Could not parse mount point "' . $rootMountPath . '"',
|
||||
\OCP\Util::ERROR
|
||||
);
|
||||
continue;
|
||||
}
|
||||
$relativeMountPath = rtrim($parts[2], '/');
|
||||
// note: we cannot do this after the loop because the decrypted config
|
||||
// options might be needed for the config hash
|
||||
$storageOptions['options'] = \OC_Mount_Config::decryptPasswords($storageOptions['options']);
|
||||
if (!isset($storageOptions['backend'])) {
|
||||
$storageOptions['backend'] = $storageOptions['class']; // legacy compat
|
||||
}
|
||||
if (!isset($storageOptions['authMechanism'])) {
|
||||
$storageOptions['authMechanism'] = null; // ensure config hash works
|
||||
}
|
||||
if (isset($storageOptions['id'])) {
|
||||
$configId = (int)$storageOptions['id'];
|
||||
if (isset($storages[$configId])) {
|
||||
$currentStorage = $storages[$configId];
|
||||
}
|
||||
$hasId = true;
|
||||
} else {
|
||||
// missing id in legacy config, need to generate
|
||||
// but at this point we don't know the max-id, so use
|
||||
// first group it by config hash
|
||||
$storageOptions['mountpoint'] = $rootMountPath;
|
||||
$configId = \OC_Mount_Config::makeConfigHash($storageOptions);
|
||||
if (isset($storagesWithConfigHash[$configId])) {
|
||||
$currentStorage = $storagesWithConfigHash[$configId];
|
||||
}
|
||||
}
|
||||
if (is_null($currentStorage)) {
|
||||
// create new
|
||||
$currentStorage = new StorageConfig($configId);
|
||||
$currentStorage->setMountPoint($relativeMountPath);
|
||||
}
|
||||
try {
|
||||
$this->populateStorageConfigWithLegacyOptions(
|
||||
$currentStorage,
|
||||
$mountType,
|
||||
$applicable,
|
||||
$storageOptions
|
||||
);
|
||||
if ($hasId) {
|
||||
$storages[$configId] = $currentStorage;
|
||||
} else {
|
||||
$storagesWithConfigHash[$configId] = $currentStorage;
|
||||
}
|
||||
} catch (\UnexpectedValueException $e) {
|
||||
// dont die if a storage backend doesn't exist
|
||||
\OCP\Util::writeLog(
|
||||
'files_external',
|
||||
'Could not load storage: "' . $e->getMessage() . '"',
|
||||
\OCP\Util::ERROR
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// convert parameter values
|
||||
foreach ($storages as $storage) {
|
||||
$storage->getBackend()->validateStorageDefinition($storage);
|
||||
$storage->getAuthMechanism()->validateStorageDefinition($storage);
|
||||
}
|
||||
return $storages;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Robin Appelman <icewind@owncloud.com>
|
||||
*
|
||||
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
||||
* @license AGPL-3.0
|
||||
*
|
||||
* This code is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3,
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License, version 3,
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Files_external\Service;
|
||||
|
||||
use OCP\IUserSession;
|
||||
|
||||
/**
|
||||
* Read user defined mounts from the legacy mount.json
|
||||
*/
|
||||
class UserLegacyStoragesService extends LegacyStoragesService {
|
||||
/**
|
||||
* @var IUserSession
|
||||
*/
|
||||
private $userSession;
|
||||
|
||||
/**
|
||||
* @param BackendService $backendService
|
||||
* @param IUserSession $userSession
|
||||
*/
|
||||
public function __construct(BackendService $backendService, IUserSession $userSession) {
|
||||
$this->backendService = $backendService;
|
||||
$this->userSession = $userSession;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read legacy config data
|
||||
*
|
||||
* @return array list of storage configs
|
||||
*/
|
||||
protected function readLegacyConfig() {
|
||||
// read user config
|
||||
$user = $this->userSession->getUser()->getUID();
|
||||
return \OC_Mount_Config::readData($user);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,233 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Robin Appelman <icewind@owncloud.com>
|
||||
*
|
||||
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
||||
* @license AGPL-3.0
|
||||
*
|
||||
* This code is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3,
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License, version 3,
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Files_External\Tests\Service;
|
||||
|
||||
|
||||
use OCA\Files_External\Service\DBConfigService;
|
||||
use OCP\IDBConnection;
|
||||
use Test\TestCase;
|
||||
|
||||
/**
|
||||
* @group DB
|
||||
*/
|
||||
class DBConfigServiceTest extends TestCase {
|
||||
/**
|
||||
* @var DBConfigService
|
||||
*/
|
||||
private $dbConfig;
|
||||
|
||||
/**
|
||||
* @var IDBConnection
|
||||
*/
|
||||
private $connection;
|
||||
|
||||
private $mounts = [];
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->connection = \OC::$server->getDatabaseConnection();
|
||||
$this->dbConfig = new DBConfigService($this->connection);
|
||||
}
|
||||
|
||||
public function tearDown() {
|
||||
foreach ($this->mounts as $mount) {
|
||||
$this->dbConfig->removeMount($mount);
|
||||
}
|
||||
$this->mounts = [];
|
||||
}
|
||||
|
||||
private function addMount($mountPoint, $storageBackend, $authBackend, $priority, $type) {
|
||||
$id = $this->dbConfig->addMount($mountPoint, $storageBackend, $authBackend, $priority, $type);
|
||||
$this->mounts[] = $id;
|
||||
return $id;
|
||||
}
|
||||
|
||||
public function testAddSimpleMount() {
|
||||
$id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
|
||||
|
||||
$mount = $this->dbConfig->getMountById($id);
|
||||
$this->assertEquals('/test', $mount['mount_point']);
|
||||
$this->assertEquals('foo', $mount['storage_backend']);
|
||||
$this->assertEquals('bar', $mount['auth_backend']);
|
||||
$this->assertEquals(100, $mount['priority']);
|
||||
$this->assertEquals(DBConfigService::MOUNT_TYPE_ADMIN, $mount['type']);
|
||||
$this->assertEquals([], $mount['applicable']);
|
||||
$this->assertEquals([], $mount['config']);
|
||||
$this->assertEquals([], $mount['options']);
|
||||
}
|
||||
|
||||
public function testAddApplicable() {
|
||||
$id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
|
||||
$this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test');
|
||||
|
||||
$mount = $this->dbConfig->getMountById($id);
|
||||
$this->assertEquals([
|
||||
['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id]
|
||||
], $mount['applicable']);
|
||||
|
||||
$this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, 'bar');
|
||||
$this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
|
||||
|
||||
$mount = $this->dbConfig->getMountById($id);
|
||||
$this->assertEquals([
|
||||
['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id],
|
||||
['type' => DBConfigService::APPLICABLE_TYPE_GROUP, 'value' => 'bar', 'mount_id' => $id],
|
||||
['type' => DBConfigService::APPLICABLE_TYPE_GLOBAL, 'value' => null, 'mount_id' => $id]
|
||||
], $mount['applicable']);
|
||||
}
|
||||
|
||||
public function testAddApplicableDouble() {
|
||||
$id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
|
||||
$this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test');
|
||||
$this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test');
|
||||
|
||||
$mount = $this->dbConfig->getMountById($id);
|
||||
$this->assertEquals([
|
||||
['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id]
|
||||
], $mount['applicable']);
|
||||
}
|
||||
|
||||
public function testDeleteMount() {
|
||||
$id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
|
||||
|
||||
$this->dbConfig->removeMount($id);
|
||||
|
||||
$mount = $this->dbConfig->getMountById($id);
|
||||
$this->assertEquals(null, $mount);
|
||||
}
|
||||
|
||||
public function testRemoveApplicable() {
|
||||
$id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
|
||||
$this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test');
|
||||
$this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test');
|
||||
|
||||
$mount = $this->dbConfig->getMountById($id);
|
||||
$this->assertEquals([], $mount['applicable']);
|
||||
}
|
||||
|
||||
public function testSetConfig() {
|
||||
$id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
|
||||
$this->dbConfig->setConfig($id, 'foo', 'bar');
|
||||
|
||||
$mount = $this->dbConfig->getMountById($id);
|
||||
$this->assertEquals(['foo' => 'bar'], $mount['config']);
|
||||
|
||||
$this->dbConfig->setConfig($id, 'foo2', 'bar2');
|
||||
|
||||
$mount = $this->dbConfig->getMountById($id);
|
||||
$this->assertEquals(['foo' => 'bar', 'foo2' => 'bar2'], $mount['config']);
|
||||
}
|
||||
|
||||
public function testSetConfigOverwrite() {
|
||||
$id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
|
||||
$this->dbConfig->setConfig($id, 'foo', 'bar');
|
||||
$this->dbConfig->setConfig($id, 'asd', '1');
|
||||
$this->dbConfig->setConfig($id, 'foo', 'qwerty');
|
||||
|
||||
$mount = $this->dbConfig->getMountById($id);
|
||||
$this->assertEquals(['foo' => 'qwerty', 'asd' => '1'], $mount['config']);
|
||||
}
|
||||
|
||||
public function testSetOption() {
|
||||
$id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
|
||||
$this->dbConfig->setOption($id, 'foo', 'bar');
|
||||
|
||||
$mount = $this->dbConfig->getMountById($id);
|
||||
$this->assertEquals(['foo' => 'bar'], $mount['options']);
|
||||
|
||||
$this->dbConfig->setOption($id, 'foo2', 'bar2');
|
||||
|
||||
$mount = $this->dbConfig->getMountById($id);
|
||||
$this->assertEquals(['foo' => 'bar', 'foo2' => 'bar2'], $mount['options']);
|
||||
}
|
||||
|
||||
public function testSetOptionOverwrite() {
|
||||
$id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
|
||||
$this->dbConfig->setOption($id, 'foo', 'bar');
|
||||
$this->dbConfig->setOption($id, 'asd', '1');
|
||||
$this->dbConfig->setOption($id, 'foo', 'qwerty');
|
||||
|
||||
$mount = $this->dbConfig->getMountById($id);
|
||||
$this->assertEquals(['foo' => 'qwerty', 'asd' => '1'], $mount['options']);
|
||||
}
|
||||
|
||||
public function testGetMountsFor() {
|
||||
$mounts = $this->dbConfig->getMountsFor(DBConfigService::APPLICABLE_TYPE_USER, 'test');
|
||||
$this->assertEquals([], $mounts);
|
||||
|
||||
$id = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
|
||||
$this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, 'test');
|
||||
|
||||
$mounts = $this->dbConfig->getMountsFor(DBConfigService::APPLICABLE_TYPE_USER, 'test');
|
||||
$this->assertCount(1, $mounts);
|
||||
$this->assertEquals($id, $mounts[0]['mount_id']);
|
||||
$this->assertEquals([['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id]], $mounts[0]['applicable']);
|
||||
}
|
||||
|
||||
public function testGetAdminMounts() {
|
||||
$id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
|
||||
$this->addMount('/test2', 'foo2', 'bar2', 100, DBConfigService::MOUNT_TYPE_PERSONAl);
|
||||
|
||||
$mounts = $this->dbConfig->getAdminMounts();
|
||||
$this->assertCount(1, $mounts);
|
||||
$this->assertEquals($id1, $mounts[0]['mount_id']);
|
||||
}
|
||||
|
||||
public function testGetAdminMountsFor() {
|
||||
$id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
|
||||
$this->addMount('/test2', 'foo2', 'bar2', 100, DBConfigService::MOUNT_TYPE_ADMIN);
|
||||
$id3 = $this->addMount('/test3', 'foo3', 'bar3', 100, DBConfigService::MOUNT_TYPE_PERSONAl);
|
||||
|
||||
$this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_USER, 'test');
|
||||
$this->dbConfig->addApplicable($id3, DBConfigService::APPLICABLE_TYPE_USER, 'test');
|
||||
|
||||
$mounts = $this->dbConfig->getAdminMountsFor(DBConfigService::APPLICABLE_TYPE_USER, 'test');
|
||||
$this->assertCount(1, $mounts);
|
||||
$this->assertEquals($id1, $mounts[0]['mount_id']);
|
||||
$this->assertEquals([['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id1]], $mounts[0]['applicable']);
|
||||
}
|
||||
|
||||
public function testGetUserMountsFor() {
|
||||
$id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
|
||||
$this->addMount('/test2', 'foo2', 'bar2', 100, DBConfigService::MOUNT_TYPE_PERSONAl);
|
||||
$id3 = $this->addMount('/test3', 'foo3', 'bar3', 100, DBConfigService::MOUNT_TYPE_PERSONAl);
|
||||
|
||||
$this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_USER, 'test');
|
||||
$this->dbConfig->addApplicable($id3, DBConfigService::APPLICABLE_TYPE_USER, 'test');
|
||||
|
||||
$mounts = $this->dbConfig->getUserMountsFor(DBConfigService::APPLICABLE_TYPE_USER, 'test');
|
||||
$this->assertCount(1, $mounts);
|
||||
$this->assertEquals($id3, $mounts[0]['mount_id']);
|
||||
$this->assertEquals([['type' => DBConfigService::APPLICABLE_TYPE_USER, 'value' => 'test', 'mount_id' => $id3]], $mounts[0]['applicable']);
|
||||
}
|
||||
|
||||
public function testGetAdminMountsForGlobal() {
|
||||
$id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
|
||||
|
||||
$this->dbConfig->addApplicable($id1, DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
|
||||
|
||||
$mounts = $this->dbConfig->getAdminMountsFor(DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
|
||||
$this->assertCount(1, $mounts);
|
||||
$this->assertEquals($id1, $mounts[0]['mount_id']);
|
||||
$this->assertEquals([['type' => DBConfigService::APPLICABLE_TYPE_GLOBAL, 'value' => null, 'mount_id' => $id1]], $mounts[0]['applicable']);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue