2015-11-02 15:13:06 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-03-01 19:25:15 +03:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2015-11-02 15:13:06 +03:00
|
|
|
*
|
|
|
|
* @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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-11-02 15:13:06 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Files_External\Service;
|
|
|
|
|
2018-11-12 17:44:53 +03:00
|
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
|
2015-11-02 15:13:06 +03:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
|
|
|
use OCP\IDBConnection;
|
2016-02-11 15:55:22 +03:00
|
|
|
use OCP\Security\ICrypto;
|
2015-11-02 15:13:06 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores the mount config in the database
|
|
|
|
*/
|
|
|
|
class DBConfigService {
|
2020-04-10 17:54:27 +03:00
|
|
|
public const MOUNT_TYPE_ADMIN = 1;
|
|
|
|
public const MOUNT_TYPE_PERSONAl = 2;
|
2015-11-02 15:13:06 +03:00
|
|
|
|
2020-04-10 17:54:27 +03:00
|
|
|
public const APPLICABLE_TYPE_GLOBAL = 1;
|
|
|
|
public const APPLICABLE_TYPE_GROUP = 2;
|
|
|
|
public const APPLICABLE_TYPE_USER = 3;
|
2015-11-02 15:13:06 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var IDBConnection
|
|
|
|
*/
|
|
|
|
private $connection;
|
|
|
|
|
2016-02-11 15:55:22 +03:00
|
|
|
/**
|
|
|
|
* @var ICrypto
|
|
|
|
*/
|
|
|
|
private $crypto;
|
|
|
|
|
2015-11-02 15:13:06 +03:00
|
|
|
/**
|
|
|
|
* DBConfigService constructor.
|
|
|
|
*
|
|
|
|
* @param IDBConnection $connection
|
2016-02-11 15:55:22 +03:00
|
|
|
* @param ICrypto $crypto
|
2015-11-02 15:13:06 +03:00
|
|
|
*/
|
2016-02-11 15:55:22 +03:00
|
|
|
public function __construct(IDBConnection $connection, ICrypto $crypto) {
|
2015-11-02 15:13:06 +03:00
|
|
|
$this->connection = $connection;
|
2016-02-11 15:55:22 +03:00
|
|
|
$this->crypto = $crypto;
|
2015-11-02 15:13:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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')
|
2016-02-29 11:44:40 +03:00
|
|
|
->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)));
|
2015-11-02 15:13:06 +03:00
|
|
|
$mounts = $this->getMountsFromQuery($query);
|
|
|
|
if (count($mounts) > 0) {
|
|
|
|
return $mounts[0];
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-17 14:29:29 +03:00
|
|
|
/**
|
|
|
|
* Get all configured mounts
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getAllMounts() {
|
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
$query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type'])
|
|
|
|
->from('external_mounts');
|
|
|
|
return $this->getMountsFromQuery($query);
|
|
|
|
}
|
|
|
|
|
2016-09-02 15:55:45 +03:00
|
|
|
public function getMountsForUser($userId, $groupIds) {
|
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
$query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type'])
|
|
|
|
->from('external_mounts', 'm')
|
|
|
|
->innerJoin('m', 'external_applicable', 'a', $builder->expr()->eq('m.mount_id', 'a.mount_id'))
|
|
|
|
->where($builder->expr()->orX(
|
|
|
|
$builder->expr()->andX( // global mounts
|
|
|
|
$builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_GLOBAL, IQueryBuilder::PARAM_INT)),
|
|
|
|
$builder->expr()->isNull('a.value')
|
|
|
|
),
|
|
|
|
$builder->expr()->andX( // mounts for user
|
|
|
|
$builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_USER, IQueryBuilder::PARAM_INT)),
|
|
|
|
$builder->expr()->eq('a.value', $builder->createNamedParameter($userId))
|
|
|
|
),
|
|
|
|
$builder->expr()->andX( // mounts for group
|
|
|
|
$builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_GROUP, IQueryBuilder::PARAM_INT)),
|
2017-05-22 18:39:56 +03:00
|
|
|
$builder->expr()->in('a.value', $builder->createNamedParameter($groupIds, IQueryBuilder::PARAM_STR_ARRAY))
|
2016-09-02 15:55:45 +03:00
|
|
|
)
|
|
|
|
));
|
|
|
|
|
|
|
|
return $this->getMountsFromQuery($query);
|
|
|
|
}
|
|
|
|
|
2019-11-13 14:28:23 +03:00
|
|
|
public function modifyMountsOnUserDelete(string $uid): void {
|
|
|
|
$this->modifyMountsOnDelete($uid, self::APPLICABLE_TYPE_USER);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function modifyMountsOnGroupDelete(string $gid): void {
|
|
|
|
$this->modifyMountsOnDelete($gid, self::APPLICABLE_TYPE_GROUP);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function modifyMountsOnDelete(string $applicableId, int $applicableType): void {
|
2019-11-13 01:53:33 +03:00
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
$query = $builder->select(['a.mount_id', $builder->func()->count('a.mount_id', 'count')])
|
|
|
|
->from('external_applicable', 'a')
|
2019-12-05 16:43:58 +03:00
|
|
|
->leftJoin('a', 'external_applicable', 'b', $builder->expr()->eq('a.mount_id', 'b.mount_id'))
|
2019-11-13 14:28:23 +03:00
|
|
|
->where($builder->expr()->andX(
|
2019-12-05 16:43:58 +03:00
|
|
|
$builder->expr()->eq('b.type', $builder->createNamedParameter($applicableType, IQueryBuilder::PARAM_INT)),
|
|
|
|
$builder->expr()->eq('b.value', $builder->createNamedParameter($applicableId))
|
2019-11-13 14:28:23 +03:00
|
|
|
)
|
2019-11-13 01:53:33 +03:00
|
|
|
)
|
|
|
|
->groupBy(['a.mount_id']);
|
|
|
|
$stmt = $query->execute();
|
|
|
|
$result = $stmt->fetchAll();
|
|
|
|
$stmt->closeCursor();
|
|
|
|
|
|
|
|
foreach ($result as $row) {
|
2020-04-10 15:19:56 +03:00
|
|
|
if ((int)$row['count'] > 1) {
|
2019-11-13 14:28:23 +03:00
|
|
|
$this->removeApplicable($row['mount_id'], $applicableType, $applicableId);
|
2019-11-13 01:53:33 +03:00
|
|
|
} else {
|
|
|
|
$this->removeMount($row['mount_id']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-02 15:13:06 +03:00
|
|
|
/**
|
|
|
|
* 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')
|
2016-02-29 11:44:40 +03:00
|
|
|
->where($builder->expr()->eq('type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT)));
|
2015-11-02 15:13:06 +03:00
|
|
|
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')
|
2016-03-10 12:24:08 +03:00
|
|
|
->innerJoin('m', 'external_applicable', 'a', $builder->expr()->eq('m.mount_id', 'a.mount_id'))
|
2016-02-29 11:44:40 +03:00
|
|
|
->where($builder->expr()->eq('a.type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT)));
|
2015-11-02 15:13:06 +03:00
|
|
|
|
|
|
|
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);
|
2016-02-29 11:44:40 +03:00
|
|
|
$query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT)));
|
2015-11-02 15:13:06 +03:00
|
|
|
|
|
|
|
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) {
|
2016-02-29 11:44:40 +03:00
|
|
|
return $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR);
|
2015-11-02 15:13:06 +03:00
|
|
|
}, $values);
|
|
|
|
|
|
|
|
$query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type'])
|
|
|
|
->from('external_mounts', 'm')
|
2016-03-10 12:24:08 +03:00
|
|
|
->innerJoin('m', 'external_applicable', 'a', $builder->expr()->eq('m.mount_id', 'a.mount_id'))
|
2016-02-29 11:44:40 +03:00
|
|
|
->where($builder->expr()->eq('a.type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT)))
|
2015-11-02 15:13:06 +03:00
|
|
|
->andWhere($builder->expr()->in('a.value', $params));
|
2016-02-29 11:44:40 +03:00
|
|
|
$query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT)));
|
2015-11-02 15:13:06 +03:00
|
|
|
|
|
|
|
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);
|
2016-02-29 11:44:40 +03:00
|
|
|
$query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_PERSONAl, IQueryBuilder::PARAM_INT)));
|
2015-11-02 15:13:06 +03:00
|
|
|
|
|
|
|
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([
|
2016-02-29 11:44:40 +03:00
|
|
|
'mount_point' => $builder->createNamedParameter($mountPoint, IQueryBuilder::PARAM_STR),
|
|
|
|
'storage_backend' => $builder->createNamedParameter($storageBackend, IQueryBuilder::PARAM_STR),
|
|
|
|
'auth_backend' => $builder->createNamedParameter($authBackend, IQueryBuilder::PARAM_STR),
|
|
|
|
'priority' => $builder->createNamedParameter($priority, IQueryBuilder::PARAM_INT),
|
|
|
|
'type' => $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT)
|
2015-11-02 15:13:06 +03:00
|
|
|
]);
|
|
|
|
$query->execute();
|
2021-02-16 00:32:56 +03:00
|
|
|
return $query->getLastInsertId();
|
2015-11-02 15:13:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a mount from the database
|
|
|
|
*
|
|
|
|
* @param int $mountId
|
|
|
|
*/
|
|
|
|
public function removeMount($mountId) {
|
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
$query = $builder->delete('external_mounts')
|
2016-02-29 11:44:40 +03:00
|
|
|
->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)));
|
2015-11-02 15:13:06 +03:00
|
|
|
$query->execute();
|
|
|
|
|
|
|
|
$query = $builder->delete('external_applicable')
|
2016-02-29 11:44:40 +03:00
|
|
|
->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)));
|
2015-11-02 15:13:06 +03:00
|
|
|
$query->execute();
|
|
|
|
|
|
|
|
$query = $builder->delete('external_config')
|
2016-02-29 11:44:40 +03:00
|
|
|
->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)));
|
2015-11-02 15:13:06 +03:00
|
|
|
$query->execute();
|
|
|
|
|
|
|
|
$query = $builder->delete('external_options')
|
2016-02-29 11:44:40 +03:00
|
|
|
->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)));
|
2015-11-02 15:13:06 +03:00
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
|
2015-12-15 15:24:30 +03:00
|
|
|
/**
|
|
|
|
* @param int $mountId
|
|
|
|
* @param string $newMountPoint
|
|
|
|
*/
|
|
|
|
public function setMountPoint($mountId, $newMountPoint) {
|
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
|
|
|
|
$query = $builder->update('external_mounts')
|
|
|
|
->set('mount_point', $builder->createNamedParameter($newMountPoint))
|
2016-02-29 11:44:40 +03:00
|
|
|
->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)));
|
2015-12-15 15:24:30 +03:00
|
|
|
|
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $mountId
|
|
|
|
* @param string $newAuthBackend
|
|
|
|
*/
|
|
|
|
public function setAuthBackend($mountId, $newAuthBackend) {
|
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
|
|
|
|
$query = $builder->update('external_mounts')
|
|
|
|
->set('auth_backend', $builder->createNamedParameter($newAuthBackend))
|
2016-02-29 11:44:40 +03:00
|
|
|
->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)));
|
2015-12-15 15:24:30 +03:00
|
|
|
|
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
|
2015-11-02 15:13:06 +03:00
|
|
|
/**
|
|
|
|
* @param int $mountId
|
|
|
|
* @param string $key
|
|
|
|
* @param string $value
|
|
|
|
*/
|
|
|
|
public function setConfig($mountId, $key, $value) {
|
2016-02-11 15:55:22 +03:00
|
|
|
if ($key === 'password') {
|
|
|
|
$value = $this->encryptValue($value);
|
|
|
|
}
|
2018-11-12 17:44:53 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
$builder->insert('external_config')
|
|
|
|
->setValue('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))
|
|
|
|
->setValue('key', $builder->createNamedParameter($key, IQueryBuilder::PARAM_STR))
|
|
|
|
->setValue('value', $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR))
|
|
|
|
->execute();
|
2020-04-10 15:19:56 +03:00
|
|
|
} catch (UniqueConstraintViolationException $e) {
|
2015-11-02 15:13:06 +03:00
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
$query = $builder->update('external_config')
|
2016-02-29 11:44:40 +03:00
|
|
|
->set('value', $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR))
|
|
|
|
->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)))
|
|
|
|
->andWhere($builder->expr()->eq('key', $builder->createNamedParameter($key, IQueryBuilder::PARAM_STR)));
|
2015-11-02 15:13:06 +03:00
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $mountId
|
|
|
|
* @param string $key
|
|
|
|
* @param string $value
|
|
|
|
*/
|
|
|
|
public function setOption($mountId, $key, $value) {
|
2018-11-12 17:44:53 +03:00
|
|
|
try {
|
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
$builder->insert('external_options')
|
|
|
|
->setValue('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))
|
|
|
|
->setValue('key', $builder->createNamedParameter($key, IQueryBuilder::PARAM_STR))
|
|
|
|
->setValue('value', $builder->createNamedParameter(json_encode($value), IQueryBuilder::PARAM_STR))
|
|
|
|
->execute();
|
2020-04-10 15:19:56 +03:00
|
|
|
} catch (UniqueConstraintViolationException $e) {
|
2015-11-02 15:13:06 +03:00
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
$query = $builder->update('external_options')
|
2016-02-29 11:44:40 +03:00
|
|
|
->set('value', $builder->createNamedParameter(json_encode($value), IQueryBuilder::PARAM_STR))
|
|
|
|
->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)))
|
|
|
|
->andWhere($builder->expr()->eq('key', $builder->createNamedParameter($key, IQueryBuilder::PARAM_STR)));
|
2015-11-02 15:13:06 +03:00
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addApplicable($mountId, $type, $value) {
|
2018-11-12 17:44:53 +03:00
|
|
|
try {
|
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
$builder->insert('external_applicable')
|
|
|
|
->setValue('mount_id', $builder->createNamedParameter($mountId))
|
|
|
|
->setValue('type', $builder->createNamedParameter($type))
|
|
|
|
->setValue('value', $builder->createNamedParameter($value))
|
|
|
|
->execute();
|
2020-04-10 15:19:56 +03:00
|
|
|
} catch (UniqueConstraintViolationException $e) {
|
2018-11-12 17:44:53 +03:00
|
|
|
// applicable exists already
|
|
|
|
}
|
2015-11-02 15:13:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function removeApplicable($mountId, $type, $value) {
|
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
$query = $builder->delete('external_applicable')
|
2016-02-29 11:44:40 +03:00
|
|
|
->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)))
|
|
|
|
->andWhere($builder->expr()->eq('type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT)));
|
2016-01-16 02:17:22 +03:00
|
|
|
|
|
|
|
if (is_null($value)) {
|
|
|
|
$query = $query->andWhere($builder->expr()->isNull('value'));
|
|
|
|
} else {
|
2016-02-29 11:44:40 +03:00
|
|
|
$query = $query->andWhere($builder->expr()->eq('value', $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR)));
|
2016-01-16 02:17:22 +03:00
|
|
|
}
|
|
|
|
|
2015-11-02 15:13:06 +03:00
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getMountsFromQuery(IQueryBuilder $query) {
|
|
|
|
$result = $query->execute();
|
|
|
|
$mounts = $result->fetchAll();
|
2016-04-25 14:50:19 +03:00
|
|
|
$uniqueMounts = [];
|
|
|
|
foreach ($mounts as $mount) {
|
|
|
|
$id = $mount['mount_id'];
|
|
|
|
if (!isset($uniqueMounts[$id])) {
|
|
|
|
$uniqueMounts[$id] = $mount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$uniqueMounts = array_values($uniqueMounts);
|
2015-11-02 15:13:06 +03:00
|
|
|
|
|
|
|
$mountIds = array_map(function ($mount) {
|
|
|
|
return $mount['mount_id'];
|
2016-04-25 14:50:19 +03:00
|
|
|
}, $uniqueMounts);
|
|
|
|
$mountIds = array_values(array_unique($mountIds));
|
2015-11-02 15:13:06 +03:00
|
|
|
|
|
|
|
$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;
|
2016-04-25 14:50:19 +03:00
|
|
|
}, $uniqueMounts, $applicable, $config, $options);
|
2015-11-02 15:13:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2016-02-29 11:44:40 +03:00
|
|
|
return $builder->createPositionalParameter($id, IQueryBuilder::PARAM_INT);
|
2015-11-02 15:13:06 +03:00
|
|
|
}, $mountIds);
|
|
|
|
$query = $builder->select($fields)
|
|
|
|
->from($table)
|
|
|
|
->where($builder->expr()->in('mount_id', $placeHolders));
|
2020-11-05 12:50:53 +03:00
|
|
|
|
|
|
|
$result = $query->execute();
|
|
|
|
$rows = $result->fetchAll();
|
|
|
|
$result->closeCursor();
|
2015-11-02 15:13:06 +03:00
|
|
|
|
|
|
|
$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) {
|
2016-02-11 15:55:22 +03:00
|
|
|
$decryptedPairts = array_map(function ($pair) {
|
|
|
|
if ($pair['key'] === 'password') {
|
|
|
|
$pair['value'] = $this->decryptValue($pair['value']);
|
|
|
|
}
|
|
|
|
return $pair;
|
|
|
|
}, $keyValuePairs);
|
2015-11-02 15:13:06 +03:00
|
|
|
$keys = array_map(function ($pair) {
|
|
|
|
return $pair['key'];
|
2016-02-11 15:55:22 +03:00
|
|
|
}, $decryptedPairts);
|
2015-11-02 15:13:06 +03:00
|
|
|
$values = array_map(function ($pair) {
|
|
|
|
return $pair['value'];
|
2016-02-11 15:55:22 +03:00
|
|
|
}, $decryptedPairts);
|
2015-11-02 15:13:06 +03:00
|
|
|
|
|
|
|
return array_combine($keys, $values);
|
|
|
|
}
|
2016-02-11 15:55:22 +03:00
|
|
|
|
|
|
|
private function encryptValue($value) {
|
|
|
|
return $this->crypto->encrypt($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function decryptValue($value) {
|
|
|
|
try {
|
|
|
|
return $this->crypto->decrypt($value);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
}
|
2015-11-02 15:13:06 +03:00
|
|
|
}
|