clear mount cache when removing applicables

This commit is contained in:
Robin Appelman 2016-01-13 15:29:53 +01:00
parent bc7bd0cd05
commit 483c6b68e2
11 changed files with 113 additions and 13 deletions

View File

@ -26,6 +26,7 @@
namespace OCA\Files_External\AppInfo; namespace OCA\Files_External\AppInfo;
use \OCP\AppFramework\App; use \OCP\AppFramework\App;
use OCP\AppFramework\IAppContainer;
use \OCP\IContainer; use \OCP\IContainer;
use \OCA\Files_External\Service\BackendService; use \OCA\Files_External\Service\BackendService;
@ -33,9 +34,13 @@ use \OCA\Files_External\Service\BackendService;
* @package OCA\Files_External\Appinfo * @package OCA\Files_External\Appinfo
*/ */
class Application extends App { class Application extends App {
public function __construct(array $urlParams=array()) { public function __construct(array $urlParams = array()) {
parent::__construct('files_external', $urlParams); parent::__construct('files_external', $urlParams);
$this->getContainer()->registerService('OCP\Files\Config\IUserMountCache', function (IAppContainer $c) {
return $c->getServer()->query('UserMountCache');
});
$this->loadBackends(); $this->loadBackends();
$this->loadAuthMechanisms(); $this->loadAuthMechanisms();

View File

@ -29,6 +29,7 @@ use OCA\Files_external\Service\LegacyStoragesService;
use OCA\Files_external\Service\StoragesService; use OCA\Files_external\Service\StoragesService;
use OCA\Files_external\Service\UserLegacyStoragesService; use OCA\Files_external\Service\UserLegacyStoragesService;
use OCA\Files_external\Service\UserStoragesService; use OCA\Files_external\Service\UserStoragesService;
use OCP\Files\Config\IUserMountCache;
use OCP\IConfig; use OCP\IConfig;
use OCP\IDBConnection; use OCP\IDBConnection;
use OCP\ILogger; use OCP\ILogger;
@ -64,6 +65,9 @@ class StorageMigrator {
*/ */
private $logger; private $logger;
/** @var IUserMountCache */
private $userMountCache;
/** /**
* StorageMigrator constructor. * StorageMigrator constructor.
* *
@ -72,19 +76,22 @@ class StorageMigrator {
* @param IConfig $config * @param IConfig $config
* @param IDBConnection $connection * @param IDBConnection $connection
* @param ILogger $logger * @param ILogger $logger
* @param IUserMountCache $userMountCache
*/ */
public function __construct( public function __construct(
BackendService $backendService, BackendService $backendService,
DBConfigService $dbConfig, DBConfigService $dbConfig,
IConfig $config, IConfig $config,
IDBConnection $connection, IDBConnection $connection,
ILogger $logger ILogger $logger,
IUserMountCache $userMountCache
) { ) {
$this->backendService = $backendService; $this->backendService = $backendService;
$this->dbConfig = $dbConfig; $this->dbConfig = $dbConfig;
$this->config = $config; $this->config = $config;
$this->connection = $connection; $this->connection = $connection;
$this->logger = $logger; $this->logger = $logger;
$this->userMountCache = $userMountCache;
} }
private function migrate(LegacyStoragesService $legacyService, StoragesService $storageService) { private function migrate(LegacyStoragesService $legacyService, StoragesService $storageService) {
@ -107,7 +114,7 @@ class StorageMigrator {
*/ */
public function migrateGlobal() { public function migrateGlobal() {
$legacyService = new GlobalLegacyStoragesService($this->backendService); $legacyService = new GlobalLegacyStoragesService($this->backendService);
$storageService = new GlobalStoragesService($this->backendService, $this->dbConfig); $storageService = new GlobalStoragesService($this->backendService, $this->dbConfig, $this->userMountCache);
$this->migrate($legacyService, $storageService); $this->migrate($legacyService, $storageService);
} }
@ -125,7 +132,7 @@ class StorageMigrator {
if (version_compare($userVersion, '0.5.0', '<')) { if (version_compare($userVersion, '0.5.0', '<')) {
$this->config->setUserValue($userId, 'files_external', 'config_version', '0.5.0'); $this->config->setUserValue($userId, 'files_external', 'config_version', '0.5.0');
$legacyService = new UserLegacyStoragesService($this->backendService, $dummySession); $legacyService = new UserLegacyStoragesService($this->backendService, $dummySession);
$storageService = new UserStoragesService($this->backendService, $this->dbConfig, $dummySession); $storageService = new UserStoragesService($this->backendService, $this->dbConfig, $dummySession, $this->userMountCache);
$this->migrate($legacyService, $storageService); $this->migrate($legacyService, $storageService);
} }

View File

@ -31,6 +31,7 @@ use \OCA\Files_external\Lib\StorageConfig;
use \OCA\Files_external\NotFoundException; use \OCA\Files_external\NotFoundException;
use \OCA\Files_External\Lib\Backend\Backend; use \OCA\Files_External\Lib\Backend\Backend;
use \OCA\Files_External\Lib\Auth\AuthMechanism; use \OCA\Files_External\Lib\Auth\AuthMechanism;
use OCP\Files\Config\IUserMountCache;
use \OCP\Files\StorageNotAvailableException; use \OCP\Files\StorageNotAvailableException;
/** /**
@ -46,13 +47,20 @@ abstract class StoragesService {
*/ */
protected $dbConfig; protected $dbConfig;
/**
* @var IUserMountCache
*/
protected $userMountCache;
/** /**
* @param BackendService $backendService * @param BackendService $backendService
* @param DBConfigService $dbConfigService * @param DBConfigService $dbConfigService
* @param IUserMountCache $userMountCache
*/ */
public function __construct(BackendService $backendService, DBConfigService $dbConfigService) { public function __construct(BackendService $backendService, DBConfigService $dbConfigService, IUserMountCache $userMountCache) {
$this->backendService = $backendService; $this->backendService = $backendService;
$this->dbConfig = $dbConfigService; $this->dbConfig = $dbConfigService;
$this->userMountCache = $userMountCache;
} }
protected function readDBConfig() { protected function readDBConfig() {
@ -416,6 +424,15 @@ abstract class StoragesService {
$this->triggerChangeHooks($oldStorage, $updatedStorage); $this->triggerChangeHooks($oldStorage, $updatedStorage);
if (($wasGlobal && !$isGlobal) || count($removedGroups) > 0) { // to expensive to properly handle these on the fly
$this->userMountCache->remoteStorageMounts($this->getStorageId($updatedStorage));
} else {
$storageId = $this->getStorageId($updatedStorage);
foreach ($removedUsers as $userId) {
$this->userMountCache->removeUserStorageMount($storageId, $userId);
}
}
return $this->getStorage($id); return $this->getStorage($id);
} }
@ -480,4 +497,25 @@ abstract class StoragesService {
return $storageImpl->getId(); return $storageImpl->getId();
} }
/**
* Construct the storage implementation
*
* @param StorageConfig $storageConfig
* @return int
*/
private function getStorageId(StorageConfig $storageConfig) {
try {
$class = $storageConfig->getBackend()->getStorageClass();
/** @var \OC\Files\Storage\Storage $storage */
$storage = new $class($storageConfig->getBackendOptions());
// auth mechanism should fire first
$storage = $storageConfig->getBackend()->wrapStorage($storage);
$storage = $storageConfig->getAuthMechanism()->wrapStorage($storage);
return $storage->getStorageCache()->getNumericId();
} catch (\Exception $e) {
return -1;
}
}
} }

View File

@ -25,6 +25,7 @@ namespace OCA\Files_External\Service;
use \OCA\Files_external\Service\GlobalStoragesService; use \OCA\Files_external\Service\GlobalStoragesService;
use \OCA\Files_External\Service\BackendService; use \OCA\Files_External\Service\BackendService;
use OCP\Files\Config\IUserMountCache;
use \OCP\IUserSession; use \OCP\IUserSession;
use \OCP\IGroupManager; use \OCP\IGroupManager;
use \OCA\Files_External\Service\UserTrait; use \OCA\Files_External\Service\UserTrait;
@ -46,14 +47,16 @@ class UserGlobalStoragesService extends GlobalStoragesService {
* @param DBConfigService $dbConfig * @param DBConfigService $dbConfig
* @param IUserSession $userSession * @param IUserSession $userSession
* @param IGroupManager $groupManager * @param IGroupManager $groupManager
* @param IUserMountCache $userMountCache
*/ */
public function __construct( public function __construct(
BackendService $backendService, BackendService $backendService,
DBConfigService $dbConfig, DBConfigService $dbConfig,
IUserSession $userSession, IUserSession $userSession,
IGroupManager $groupManager IGroupManager $groupManager,
IUserMountCache $userMountCache
) { ) {
parent::__construct($backendService, $dbConfig); parent::__construct($backendService, $dbConfig, $userMountCache);
$this->userSession = $userSession; $this->userSession = $userSession;
$this->groupManager = $groupManager; $this->groupManager = $groupManager;
} }

View File

@ -23,6 +23,7 @@
namespace OCA\Files_external\Service; namespace OCA\Files_external\Service;
use OCP\Files\Config\IUserMountCache;
use \OCP\IUserSession; use \OCP\IUserSession;
use \OC\Files\Filesystem; use \OC\Files\Filesystem;
@ -44,14 +45,16 @@ class UserStoragesService extends StoragesService {
* @param BackendService $backendService * @param BackendService $backendService
* @param DBConfigService $dbConfig * @param DBConfigService $dbConfig
* @param IUserSession $userSession user session * @param IUserSession $userSession user session
* @param IUserMountCache $userMountCache
*/ */
public function __construct( public function __construct(
BackendService $backendService, BackendService $backendService,
DBConfigService $dbConfig, DBConfigService $dbConfig,
IUserSession $userSession IUserSession $userSession,
IUserMountCache $userMountCache
) { ) {
$this->userSession = $userSession; $this->userSession = $userSession;
parent::__construct($backendService, $dbConfig); parent::__construct($backendService, $dbConfig, $userMountCache);
} }
protected function readDBConfig() { protected function readDBConfig() {

View File

@ -34,7 +34,7 @@ use \OCA\Files_external\Lib\StorageConfig;
class GlobalStoragesServiceTest extends StoragesServiceTest { class GlobalStoragesServiceTest extends StoragesServiceTest {
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
$this->service = new GlobalStoragesService($this->backendService, $this->dbConfig); $this->service = new GlobalStoragesService($this->backendService, $this->dbConfig, $this->mountCache);
} }
public function tearDown() { public function tearDown() {

View File

@ -76,6 +76,11 @@ abstract class StoragesServiceTest extends \Test\TestCase {
*/ */
protected static $hookCalls; protected static $hookCalls;
/**
* @var \PHPUnit_Framework_MockObject_MockObject|\OCP\Files\Config\IUserMountCache
*/
protected $mountCache;
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
$this->dbConfig = new CleaningDBConfig(\OC::$server->getDatabaseConnection()); $this->dbConfig = new CleaningDBConfig(\OC::$server->getDatabaseConnection());
@ -87,6 +92,8 @@ abstract class StoragesServiceTest extends \Test\TestCase {
); );
\OC_Mount_Config::$skipTest = true; \OC_Mount_Config::$skipTest = true;
$this->mountCache = $this->getMock('OCP\Files\Config\IUserMountCache');
// prepare BackendService mock // prepare BackendService mock
$this->backendService = $this->backendService =
$this->getMockBuilder('\OCA\Files_External\Service\BackendService') $this->getMockBuilder('\OCA\Files_External\Service\BackendService')

View File

@ -94,7 +94,8 @@ class UserGlobalStoragesServiceTest extends GlobalStoragesServiceTest {
$this->backendService, $this->backendService,
$this->dbConfig, $this->dbConfig,
$userSession, $userSession,
$this->groupManager $this->groupManager,
$this->mountCache
); );
} }

View File

@ -49,7 +49,7 @@ class UserStoragesServiceTest extends StoragesServiceTest {
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
$this->globalStoragesService = new GlobalStoragesService($this->backendService, $this->dbConfig); $this->globalStoragesService = new GlobalStoragesService($this->backendService, $this->dbConfig, $this->mountCache);
$this->userId = $this->getUniqueID('user_'); $this->userId = $this->getUniqueID('user_');
$this->createUser($this->userId, $this->userId); $this->createUser($this->userId, $this->userId);
@ -62,7 +62,7 @@ class UserStoragesServiceTest extends StoragesServiceTest {
->method('getUser') ->method('getUser')
->will($this->returnValue($this->user)); ->will($this->returnValue($this->user));
$this->service = new UserStoragesService($this->backendService, $this->dbConfig, $userSession); $this->service = new UserStoragesService($this->backendService, $this->dbConfig, $userSession, $this->mountCache);
} }
private function makeTestStorageData() { private function makeTestStorageData() {

View File

@ -230,4 +230,21 @@ class UserMountCache implements IUserMountCache {
->where($builder->expr()->eq('user_id', $builder->createNamedParameter($user->getUID()))); ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($user->getUID())));
$query->execute(); $query->execute();
} }
public function removeUserStorageMount($storageId, $userId) {
$builder = $this->connection->getQueryBuilder();
$query = $builder->delete('mounts')
->where($builder->expr()->eq('user_id', $builder->createNamedParameter($userId)))
->andWhere($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, \PDO::PARAM_INT)));
$query->execute();
}
public function remoteStorageMounts($storageId) {
$builder = $this->connection->getQueryBuilder();
$query = $builder->delete('mounts')
->where($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, \PDO::PARAM_INT)));
$query->execute();
}
} }

View File

@ -67,4 +67,23 @@ interface IUserMountCache {
* @since 9.0.0 * @since 9.0.0
*/ */
public function removeUserMounts(IUser $user); public function removeUserMounts(IUser $user);
/**
* Remove all mounts for a user and storage
*
* @param $storageId
* @param string $userId
* @return mixed
* @since 9.0.0
*/
public function removeUserStorageMount($storageId, $userId);
/**
* Remove all cached mounts for a storage
*
* @param $storageId
* @return mixed
* @since 9.0.0
*/
public function remoteStorageMounts($storageId);
} }