Unverified Commit 5415cade authored by Vincent Petry's avatar Vincent Petry
Browse files

Add using casing check/fix for initMountPoints

parent 8c1b2528
......@@ -89,17 +89,20 @@ class TransferOwnership extends Command {
}
protected function execute(InputInterface $input, OutputInterface $output) {
$this->sourceUser = $input->getArgument('source-user');
$this->destinationUser = $input->getArgument('destination-user');
if (!$this->userManager->userExists($this->sourceUser)) {
$sourceUserObject = $this->userManager->get($input->getArgument('source-user'));
$destinationUserObject = $this->userManager->get($input->getArgument('destination-user'));
if (is_null($sourceUserObject)) {
$output->writeln("<error>Unknown source user $this->sourceUser</error>");
return;
}
if (!$this->userManager->userExists($this->destinationUser)) {
if (is_null($destinationUserObject)) {
$output->writeln("<error>Unknown destination user $this->destinationUser</error>");
return;
}
$this->sourceUser = $sourceUserObject->getUID();
$this->destinationUser = $destinationUserObject->getUID();
// target user has to be ready
if (!\OC::$server->getEncryptionManager()->isReadyForUser($this->destinationUser)) {
$output->writeln("<error>The target user is not ready to accept files. The user has at least to be logged in once.</error>");
......
......@@ -84,6 +84,8 @@ class AvatarManager implements IAvatarManager {
throw new \Exception('user does not exist');
}
$userId = $user->getUID();
$avatarsFolder = $this->getAvatarFolder($userId);
return new Avatar($avatarsFolder, $this->l, $user, $this->logger);
}
......
......@@ -393,9 +393,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);
......@@ -405,6 +402,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 */
......
......@@ -334,6 +334,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();
\OC\Files\Filesystem::initMountPoints($userId);
$dir = '/' . $userId;
$folder = null;
......
......@@ -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 = [];
......
......@@ -28,15 +28,20 @@ use OCP\IL10N;
use OCP\ILogger;
use OCP\IUser;
use OCP\IUserManager;
use Test\Traits\MountProviderTrait;
/**
* Class AvatarManagerTest
*/
class AvatarManagerTest extends TestCase {
use MountProviderTrait;
/** @var AvatarManager | \PHPUnit_Framework_MockObject_MockObject */
private $avatarManager;
/** @var \OC\Files\Storage\Temporary */
private $storage;
/** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject */
private $userManager;
......@@ -51,6 +56,8 @@ class AvatarManagerTest extends TestCase {
$l = $this->createMock(IL10N::class);
$logger = $this->createMock(ILogger::class);
$this->storage = new \OC\Files\Storage\Temporary();
$this->registerMount('valid-user', $this->storage, '/valid-user/');
$this->avatarManager = $this->getMockBuilder(AvatarManager::class)
->setMethods(['getAvatarFolder'])
......@@ -79,6 +86,7 @@ class AvatarManagerTest extends TestCase {
$avatar = $this->avatarManager->getAvatar('valid-user');
$this->assertInstanceOf('\OCP\IAvatar', $avatar);
$this->assertFalse($this->storage->file_exists('files'));
}
/**
......@@ -97,4 +105,26 @@ class AvatarManagerTest extends TestCase {
['d4/1d/8cd98f00b204e9800998ecf8427e', ''],
];
}
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(Folder::class);
$this->avatarManager->expects($this->once())
->method('getAvatarFolder')
->with('valid-user')
->willReturn($folder);
$avatar = $this->avatarManager->getAvatar('vaLid-USER');
$this->assertInstanceOf('\OCP\IAvatar', $avatar);
$this->assertFalse($this->storage->file_exists('files'));
}
}
......@@ -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();
}
......@@ -391,6 +393,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
*/
......
......@@ -11,6 +11,7 @@ namespace Test\Files\Node;
use OC\Files\FileInfo;
use OCP\Files\NotPermittedException;
use OC\Files\Mount\Manager;
use OC\User\NoUserException;
/**
* @group DB
......@@ -101,4 +102,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);
$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);
}
}
......@@ -229,6 +229,9 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase {
self::tearDownAfterClassCleanStrayHooks();
self::tearDownAfterClassCleanStrayLocks();
\OC_User::clearBackends();
\OC_User::useBackend('dummy');
parent::tearDownAfterClass();
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment