Add using casing check/fix for initMountPoints

This commit is contained in:
Vincent Petry 2016-10-04 12:28:41 +02:00 committed by Lukas Reschke
parent c1feae1684
commit f737426eca
No known key found for this signature in database
GPG Key ID: B9F6980CF6E759B1
8 changed files with 154 additions and 13 deletions

View File

@ -91,23 +91,21 @@ class TransferOwnership extends Command {
}
protected function execute(InputInterface $input, OutputInterface $output) {
$this->sourceUser = $input->getArgument('source-user');
$this->destinationUser = $input->getArgument('destination-user');
$source = $this->userManager->get($this->sourceUser);
$destination = $this->userManager->get($this->destinationUser);
$sourceUserObject = $this->userManager->get($input->getArgument('source-user'));
$destinationUserObject = $this->userManager->get($input->getArgument('destination-user'));
if (!$source instanceof IUser) {
if (!$sourceUserObject instanceof IUser) {
$output->writeln("<error>Unknown source user $this->sourceUser</error>");
return;
}
if (!$destination instanceof IUser) {
if (!$destinationUserObject instanceof IUser) {
$output->writeln("<error>Unknown destination user $this->destinationUser</error>");
return;
}
$this->sourceUser = $source->getUID();
$this->destinationUser = $destination->getUID();
$this->sourceUser = $sourceUserObject->getUID();
$this->destinationUser = $destinationUserObject->getUID();
// target user has to be ready
if (!\OC::$server->getEncryptionManager()->isReadyForUser($this->destinationUser)) {

View File

@ -394,9 +394,6 @@ class Filesystem {
if ($user === null || $user === false || $user === '') {
throw new \OC\User\NoUserException('Attempted to initialize mount points for null user and no user in session');
}
if (isset(self::$usersSetup[$user])) {
return;
}
$userManager = \OC::$server->getUserManager();
$userObject = $userManager->get($user);
@ -406,6 +403,17 @@ class Filesystem {
throw new \OC\User\NoUserException('Backends provided no user object for ' . $user);
}
// workaround in case of different casings
if ($user !== $userObject->getUID()) {
$stack = json_encode(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 50));
\OCP\Util::writeLog('files', 'initMountPoints() called with wrong user casing. This could be a bug. Expected: "' . $userObject->getUID() . '" got "' . $user . '". Stack: ' . $stack, \OCP\Util::WARN);
}
$user = $userObject->getUID();
if (isset(self::$usersSetup[$user])) {
return;
}
self::$usersSetup[$user] = true;
/** @var \OC\Files\Config\MountProviderCollection $mountConfigManager */

View File

@ -347,6 +347,15 @@ class Root extends Folder implements IRootFolder {
* @return \OCP\Files\Folder
*/
public function getUserFolder($userId) {
$userObject = \OC::$server->getUserManager()->get($userId);
if (is_null($userObject)) {
\OCP\Util::writeLog('files', 'Backends provided no user object for ' . $userId, \OCP\Util::ERROR);
throw new \OC\User\NoUserException('Backends provided no user object for ' . $userId);
}
$userId = $userObject->getUID();
if (!$this->userFolderCache->hasKey($userId)) {
\OC\Files\Filesystem::initMountPoints($userId);

View File

@ -136,6 +136,15 @@ class Share extends Constants {
* not '/admin/data/file.txt'
*/
public static function getUsersSharingFile($path, $ownerUser, $includeOwner = false, $returnUserPaths = false, $recursive = true) {
$userManager = \OC::$server->getUserManager();
$userObject = $userManager->get($ownerUser);
if (is_null($ownerUser)) {
\OCP\Util::writeLog('files', ' Backends provided no user object for ' . $ownerUser, \OCP\Util::ERROR);
throw new \OC\User\NoUserException('Backends provided no user object for ' . $ownerUser);
}
$ownerUser = $userObject->getUID();
Filesystem::initMountPoints($ownerUser);
$shares = $sharePaths = $fileTargets = array();

View File

@ -26,6 +26,7 @@ namespace Test;
use OC\Avatar;
use OC\AvatarManager;
use OC\Files\AppData\AppData;
use OCP\Files\IAppData;
use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\IConfig;
@ -48,7 +49,7 @@ class AvatarManagerTest extends \Test\TestCase {
private $logger;
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
private $config;
/** @var AvatarManager */
/** @var AvatarManager | \PHPUnit_Framework_MockObject_MockObject */
private $avatarManager;
public function setUp() {
@ -101,8 +102,29 @@ class AvatarManagerTest extends \Test\TestCase {
->with('valid-user')
->willReturn($folder);
$expected = new Avatar($folder, $this->l10n, $user, $this->logger, $this->config);;
$expected = new Avatar($folder, $this->l10n, $user, $this->logger, $this->config);
$this->assertEquals($expected, $this->avatarManager->getAvatar('valid-user'));
}
public function testGetAvatarValidUserDifferentCasing() {
$user = $this->createMock(IUser::class);
$this->userManager->expects($this->once())
->method('get')
->with('vaLid-USER')
->willReturn($user);
$user->expects($this->once())
->method('getUID')
->willReturn('valid-user');
$folder = $this->createMock(ISimpleFolder::class);
$this->appData
->expects($this->once())
->method('getFolder')
->with('valid-user')
->willReturn($folder);
$expected = new Avatar($folder, $this->l10n, $user, $this->logger, $this->config);
$this->assertEquals($expected, $this->avatarManager->getAvatar('vaLid-USER'));
}
}

View File

@ -79,6 +79,7 @@ class FilesystemTest extends \Test\TestCase {
protected function setUp() {
parent::setUp();
\OC_User::clearBackends();
$userBackend = new \Test\Util\User\Dummy();
$userBackend->createUser(self::TEST_FILESYSTEM_USER1, self::TEST_FILESYSTEM_USER1);
$userBackend->createUser(self::TEST_FILESYSTEM_USER2, self::TEST_FILESYSTEM_USER2);
@ -93,6 +94,7 @@ class FilesystemTest extends \Test\TestCase {
$this->logout();
$this->invokePrivate('\OC\Files\Filesystem', 'normalizedPathCache', [null]);
\OC_User::clearBackends();
parent::tearDown();
}
@ -368,6 +370,39 @@ class FilesystemTest extends \Test\TestCase {
$this->assertEquals(2, $thrown);
}
public function testUserNameCasing() {
$this->logout();
$userId = $this->getUniqueID('user_');
\OC_User::clearBackends();
// needed for loginName2UserName mapping
$userBackend = $this->getMock('\OC\User\Database');
\OC::$server->getUserManager()->registerBackend($userBackend);
$userBackend->expects($this->once())
->method('userExists')
->with(strtoupper($userId))
->will($this->returnValue(true));
$userBackend->expects($this->once())
->method('loginName2UserName')
->with(strtoupper($userId))
->will($this->returnValue($userId));
$view = new \OC\Files\View();
$this->assertFalse($view->file_exists('/' . $userId));
\OC\Files\Filesystem::initMountPoints(strtoupper($userId));
list($storage1, $path1) = $view->resolvePath('/' . $userId);
list($storage2, $path2) = $view->resolvePath('/' . strtoupper($userId));
$this->assertTrue($storage1->instanceOfStorage('\OCP\Files\IHomeStorage'));
$this->assertEquals('', $path1);
// not mounted, still on the local root storage
$this->assertEquals(strtoupper($userId), $path2);
}
/**
* Tests that the home storage is used for the user's mount point
*/

View File

@ -9,6 +9,8 @@
namespace Test\Files\Node;
use OC\Files\FileInfo;
use OC\Files\Mount\Manager;
use OC\User\NoUserException;
class RootTest extends \Test\TestCase {
/** @var \OC\User\User */
@ -125,4 +127,59 @@ class RootTest extends \Test\TestCase {
$root->get('/bar/foo');
}
public function testGetUserFolder() {
$this->logout();
$manager = new Manager();
/**
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
*/
$view = new \OC\Files\View();
$user1 = $this->getUniqueID('user1_');
$user2 = $this->getUniqueID('user2_');
\OC_User::clearBackends();
// needed for loginName2UserName mapping
$userBackend = $this->getMock('\OC\User\Database');
\OC::$server->getUserManager()->registerBackend($userBackend);
$userBackend->expects($this->any())
->method('userExists')
->will($this->returnValueMap([
[$user1, true],
[$user2, true],
[strtoupper($user1), true],
[strtoupper($user2), true],
]));
$userBackend->expects($this->any())
->method('loginName2UserName')
->will($this->returnValueMap([
[strtoupper($user1), $user1],
[$user1, $user1],
[strtoupper($user2), $user2],
[$user2, $user2],
]));
$this->loginAsUser($user1);
$root = new \OC\Files\Node\Root($manager, $view, null, $this->userMountCache);
$folder = $root->getUserFolder($user1);
$this->assertEquals('/' . $user1 . '/files', $folder->getPath());
$folder = $root->getUserFolder($user2);
$this->assertEquals('/' . $user2 . '/files', $folder->getPath());
// case difference must not matter here
$folder = $root->getUserFolder(strtoupper($user2));
$this->assertEquals('/' . $user2 . '/files', $folder->getPath());
$thrown = false;
try {
$folder = $root->getUserFolder($this->getUniqueID('unexist'));
} catch (NoUserException $e) {
$thrown = true;
}
$this->assertTrue($thrown);
}
}

View File

@ -273,6 +273,9 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase {
self::tearDownAfterClassCleanStrayHooks();
self::tearDownAfterClassCleanStrayLocks();
\OC_User::clearBackends();
\OC_User::useBackend('dummy');
parent::tearDownAfterClass();
}