call mount providers that are registered after the filesystem is setup

This commit is contained in:
Robin Appelman 2015-05-07 14:07:02 +02:00
parent c91b52d38c
commit 24131586d7
3 changed files with 75 additions and 8 deletions

View File

@ -22,12 +22,16 @@
namespace OC\Files\Config;
use OC\Hooks\Emitter;
use OC\Hooks\EmitterTrait;
use OCP\Files\Config\IMountProviderCollection;
use OCP\Files\Config\IMountProvider;
use OCP\Files\Storage\IStorageFactory;
use OCP\IUser;
class MountProviderCollection implements IMountProviderCollection {
class MountProviderCollection implements IMountProviderCollection, Emitter {
use EmitterTrait;
/**
* @var \OCP\Files\Config\IMountProvider[]
*/
@ -65,5 +69,6 @@ class MountProviderCollection implements IMountProviderCollection {
*/
public function registerProvider(IMountProvider $provider) {
$this->providers[] = $provider;
$this->emit('\OC\Files\Config', 'registerMountProvider', [$provider]);
}
}

View File

@ -59,7 +59,11 @@
namespace OC\Files;
use OC\Cache\File;
use OC\Files\Config\MountProviderCollection;
use OC\Files\Storage\StorageFactory;
use OCP\Files\Config\IMountProvider;
use OCP\IUserManager;
class Filesystem {
@ -78,6 +82,8 @@ class Filesystem {
static private $normalizedPathCache = array();
static private $listeningForProviders = false;
/**
* classname which used for hooks handling
* used as signalclass in OC_Hooks::emit()
@ -371,14 +377,15 @@ class Filesystem {
$root = \OC_User::getHome($user);
$userObject = \OC_User::getManager()->get($user);
$userManager = \OC::$server->getUserManager();
$userObject = $userManager->get($user);
if (is_null($userObject)) {
\OCP\Util::writeLog('files', ' Backends provided no user object for '.$user, \OCP\Util::ERROR);
\OCP\Util::writeLog('files', ' Backends provided no user object for ' . $user, \OCP\Util::ERROR);
throw new \OC\User\NoUserException();
}
$homeStorage = \OC_Config::getValue( 'objectstore' );
$homeStorage = \OC_Config::getValue('objectstore');
if (!empty($homeStorage)) {
// sanity checks
if (empty($homeStorage['class'])) {
@ -412,16 +419,33 @@ class Filesystem {
self::mountCacheDir($user);
// Chance to mount for other storages
if($userObject) {
$mountConfigManager = \OC::$server->getMountProviderCollection();
/** @var \OC\Files\Config\MountProviderCollection $mountConfigManager */
$mountConfigManager = \OC::$server->getMountProviderCollection();
if ($userObject) {
$mounts = $mountConfigManager->getMountsForUser($userObject);
array_walk($mounts, array(self::$mounts, 'addMount'));
}
self::listenForNewMountProviders($mountConfigManager, $userManager);
\OC_Hook::emit('OC_Filesystem', 'post_initMountPoints', array('user' => $user, 'user_dir' => $root));
}
private static function listenForNewMountProviders(MountProviderCollection $mountConfigManager, IUserManager $userManager) {
if (!self::$listeningForProviders) {
self::$listeningForProviders = true;
$mountConfigManager->listen('\OC\Files\Config', 'registerMountProvider', function (IMountProvider $provider) use ($userManager) {
foreach (Filesystem::$usersSetup as $user => $setup) {
$userObject = $userManager->get($user);
$mounts = $provider->getMountsForUser($userObject, Filesystem::getLoader());
array_walk($mounts, array(self::$mounts, 'addMount'));
}
});
}
}
/**
* Mounts the cache directory
*
* @param string $user user name
*/
private static function mountCacheDir($user) {
@ -455,6 +479,7 @@ class Filesystem {
/**
* get the relative path of the root data directory for the current user
*
* @return string
*
* Returns path like /admin/files
@ -537,7 +562,7 @@ class Filesystem {
if (!$path || $path[0] !== '/') {
$path = '/' . $path;
}
if (strpos($path, '/../') !== FALSE || strrchr($path, '/') === '/..') {
if (strpos($path, '/../') !== false || strrchr($path, '/') === '/..') {
return false;
}
return true;
@ -577,6 +602,7 @@ class Filesystem {
/**
* check if the directory should be ignored when scanning
* NOTE: the special directories . and .. would cause never ending recursion
*
* @param String $dir
* @return boolean
*/
@ -745,6 +771,7 @@ class Filesystem {
/**
* Fix common problems with a file path
*
* @param string $path
* @param bool $stripTrailingSlash
* @param bool $isAbsolutePath
@ -761,7 +788,7 @@ class Filesystem {
$cacheKey = json_encode([$path, $stripTrailingSlash, $isAbsolutePath]);
if(isset(self::$normalizedPathCache[$cacheKey])) {
if (isset(self::$normalizedPathCache[$cacheKey])) {
return self::$normalizedPathCache[$cacheKey];
}

View File

@ -22,11 +22,32 @@
namespace Test\Files;
use OC\Files\Mount\MountPoint;
use OC\Files\Storage\Temporary;
use OC\User\NoUserException;
use OCP\Files\Config\IMountProvider;
use OCP\Files\Storage\IStorageFactory;
use OCP\IUser;
class DummyMountProvider implements IMountProvider {
private $mounts = [];
/**
* @param array $mounts
*/
public function __construct(array $mounts) {
$this->mounts = $mounts;
}
public function getMountsForUser(IUser $user, IStorageFactory $loader) {
return isset($this->mounts[$user->getUID()]) ? $this->mounts[$user->getUID()] : [];
}
}
class Filesystem extends \Test\TestCase {
const TEST_FILESYSTEM_USER1 = "test-filesystem-user1";
const TEST_FILESYSTEM_USER2 = "test-filesystem-user1";
/**
* @var array tmpDirs
@ -44,6 +65,10 @@ class Filesystem extends \Test\TestCase {
protected function setUp() {
parent::setUp();
$userBackend = new \OC_User_Dummy();
$userBackend->createUser(self::TEST_FILESYSTEM_USER1, self::TEST_FILESYSTEM_USER1);
$userBackend->createUser(self::TEST_FILESYSTEM_USER2, self::TEST_FILESYSTEM_USER2);
\OC::$server->getUserManager()->registerBackend($userBackend);
$this->loginAsUser();
}
@ -271,6 +296,7 @@ class Filesystem extends \Test\TestCase {
/**
* Tests that an exception is thrown when passed user does not exist.
*
* @expectedException \OC\User\NoUserException
*/
public function testLocalMountWhenUserDoesNotExist() {
@ -380,4 +406,13 @@ class Filesystem extends \Test\TestCase {
\OC_Config::setValue('cache_path', $oldCachePath);
}
public function testRegisterMountProviderAfterSetup() {
\OC\Files\Filesystem::initMountPoints(self::TEST_FILESYSTEM_USER2);
$this->assertEquals('/', \OC\Files\Filesystem::getMountPoint('/foo/bar'));
$mount = new MountPoint(new Temporary([]), '/foo/bar');
$mountProvider = new DummyMountProvider([self::TEST_FILESYSTEM_USER2 => [$mount]]);
\OC::$server->getMountProviderCollection()->registerProvider($mountProvider);
$this->assertEquals('/foo/bar/', \OC\Files\Filesystem::getMountPoint('/foo/bar'));
}
}