diff options
author | Vincent Petry <pvince81@owncloud.com> | 2016-10-04 12:28:41 +0200 |
---|---|---|
committer | Lukas Reschke <lukas@statuscode.ch> | 2016-11-02 22:21:46 +0100 |
commit | f737426eca338dbc65db6210a09f3034d372701b (patch) | |
tree | 10fbeb022517900955aff8a69e571652ee48d073 | |
parent | c1feae1684934bb52b1edaa67d33d01b377b875a (diff) | |
download | nextcloud-server-f737426eca338dbc65db6210a09f3034d372701b.tar.gz nextcloud-server-f737426eca338dbc65db6210a09f3034d372701b.zip |
Add using casing check/fix for initMountPoints
-rw-r--r-- | apps/files/lib/Command/TransferOwnership.php | 14 | ||||
-rw-r--r-- | lib/private/Files/Filesystem.php | 14 | ||||
-rw-r--r-- | lib/private/Files/Node/Root.php | 9 | ||||
-rw-r--r-- | lib/private/Share/Share.php | 9 | ||||
-rw-r--r-- | tests/lib/AvatarManagerTest.php | 26 | ||||
-rw-r--r-- | tests/lib/Files/FilesystemTest.php | 35 | ||||
-rw-r--r-- | tests/lib/Files/Node/RootTest.php | 57 | ||||
-rw-r--r-- | tests/lib/TestCase.php | 3 |
8 files changed, 154 insertions, 13 deletions
diff --git a/apps/files/lib/Command/TransferOwnership.php b/apps/files/lib/Command/TransferOwnership.php index 742948b7ac3..3b51e4a9e6d 100644 --- a/apps/files/lib/Command/TransferOwnership.php +++ b/apps/files/lib/Command/TransferOwnership.php @@ -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)) { diff --git a/lib/private/Files/Filesystem.php b/lib/private/Files/Filesystem.php index 113bb5a85c6..d2662af527a 100644 --- a/lib/private/Files/Filesystem.php +++ b/lib/private/Files/Filesystem.php @@ -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 */ diff --git a/lib/private/Files/Node/Root.php b/lib/private/Files/Node/Root.php index 0cda2c8b822..28438e8adbb 100644 --- a/lib/private/Files/Node/Root.php +++ b/lib/private/Files/Node/Root.php @@ -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); diff --git a/lib/private/Share/Share.php b/lib/private/Share/Share.php index 33801cd6347..842dded0671 100644 --- a/lib/private/Share/Share.php +++ b/lib/private/Share/Share.php @@ -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(); diff --git a/tests/lib/AvatarManagerTest.php b/tests/lib/AvatarManagerTest.php index 8ccc51d12e0..83a5cfd9b30 100644 --- a/tests/lib/AvatarManagerTest.php +++ b/tests/lib/AvatarManagerTest.php @@ -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')); + } } diff --git a/tests/lib/Files/FilesystemTest.php b/tests/lib/Files/FilesystemTest.php index 210ce4edc69..fb9dee15051 100644 --- a/tests/lib/Files/FilesystemTest.php +++ b/tests/lib/Files/FilesystemTest.php @@ -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 */ diff --git a/tests/lib/Files/Node/RootTest.php b/tests/lib/Files/Node/RootTest.php index f42a42c42b1..35ba8a6c223 100644 --- a/tests/lib/Files/Node/RootTest.php +++ b/tests/lib/Files/Node/RootTest.php @@ -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); + } } diff --git a/tests/lib/TestCase.php b/tests/lib/TestCase.php index 61372d4ae90..1ca896e0914 100644 --- a/tests/lib/TestCase.php +++ b/tests/lib/TestCase.php @@ -273,6 +273,9 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase { self::tearDownAfterClassCleanStrayHooks(); self::tearDownAfterClassCleanStrayLocks(); + \OC_User::clearBackends(); + \OC_User::useBackend('dummy'); + parent::tearDownAfterClass(); } |