Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable10] Check the return value of getUserSession before using it #30432

Merged
merged 1 commit into from
Feb 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions lib/private/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use InterfaSys\LogNormalizer\Normalizer;

use \OCP\ILogger;
use OCP\IUserSession;
use OCP\Util;

/**
Expand Down Expand Up @@ -292,12 +293,15 @@ public function log($level, $message, array $context = []) {

// check for user
if (!empty($logCondition['users'])) {
$user = \OC::$server->getUserSession()->getUser();

// if the user matches set the log condition to satisfied
if ($user !== null && in_array($user->getUID(), $logCondition['users'], true)) {
$this->logConditionSatisfied = true;
break;
$userSession = \OC::$server->getUserSession();
if ($userSession instanceof IUserSession) {
$user = $userSession->getUser();

// if the user matches set the log condition to satisfied
if ($user !== null && in_array($user->getUID(), $logCondition['users'], true)) {
$this->logConditionSatisfied = true;
break;
}
}
}
}
Expand Down
15 changes: 11 additions & 4 deletions lib/private/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use OCP\IUser;
use OCP\IConfig;
use OCP\IUserBackend;
use OCP\IUserSession;
use OCP\User\IChangePasswordBackend;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\GenericEvent;
Expand Down Expand Up @@ -331,11 +332,17 @@ public function canChangePassword() {
* @return bool
*/
public function canChangeDisplayName() {
if (($this->config->getSystemValue('allow_user_to_change_display_name') === false) &&
(!$this->groupManager->isAdmin($this->userSession->getUser()->getUID())) &&
(!$this->groupManager->getSubAdmin()->isSubAdmin($this->userSession->getUser()))) {
return false;
if ($this->userSession instanceof IUserSession) {
$user = $this->userSession->getUser();
if (
($this->config->getSystemValue('allow_user_to_change_display_name') === false) &&
(($user !== null) && (!$this->groupManager->isAdmin($user->getUID()))) &&
(($user !== null) && (!$this->groupManager->getSubAdmin()->isSubAdmin($user)))
) {
return false;
}
}

$backend = $this->account->getBackendInstance();
if (is_null($backend)) {
return false;
Expand Down
22 changes: 22 additions & 0 deletions tests/lib/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use OC\Log;
use OCP\IConfig;
use OCP\IUserSession;
use OCP\Util;

class LoggerTest extends TestCase {
Expand Down Expand Up @@ -77,6 +78,27 @@ public function testAppLogCondition() {
$this->assertEquals($expected, $this->getLogs());
}

public function testNullUserSession() {
$userSession = $this->createMock(IUserSession::class);
$userSession->expects($this->any())
->method('getUser')
->willReturn(null);
$this->config->expects($this->any())
->method('getValue')
->will(($this->returnValueMap([
['loglevel', Util::WARN, Util::WARN],
['log.conditions', [], [['users' => ['foo'], 'apps' => ['files'], 'logfile' => '/tmp/test.log']]]
])));
$logger = $this->logger;

$logger->warning('Don\'t display info messages');

$expected = [
'2 Don\'t display info messages',
];
$this->assertEquals($expected, $this->getLogs());
}

private function getLogs() {
return self::$logs;
}
Expand Down
39 changes: 39 additions & 0 deletions tests/lib/User/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,45 @@ public function testCanChangeDisplayName($expected, $implements) {
$this->assertEquals($expected, $user->setDisplayName('Foo'));
}

public function provideNullorFalseData() {
return [
[null, null, false],
[null, true, false],
[null, true, true]
];
}

/**
* @dataProvider provideNullorFalseData
* @param $user
* @param $backendinstance
* @param $setDisplayName
*/
public function testCanChangeDisplayNameWhenNullSession($getUser, $backendinstance, $setDisplayName) {
$this->sessionUser->method('getUser')
->willReturn($getUser);
$backend = $this->getMockBuilder(Database::class)
->setMethods(['implementsActions'])
->getMock();

/** @var Account | \PHPUnit_Framework_MockObject_MockObject $account */
$account = $this->getMockBuilder(Account::class)
->setMethods(['getBackendInstance', 'getDisplayName', 'setDisplayName'])
->getMock();
if ($backendinstance !== null) {
$backendinstance = $backend;
}
$account->expects($this->any())->method('getBackendInstance')->willReturn($backendinstance);
$account->expects($this->any())->method('getDisplayName')->willReturn('admin');
$account->expects($this->any())->method('setDisplayName')->willReturn($setDisplayName);
$user = new User($account, $this->accountMapper, null, $this->config, null, null, $this->groupManager, null);
if ($setDisplayName !== true) {
$this->assertEquals($setDisplayName, $user->canChangeDisplayName());
} else {
$this->assertEquals(null, $user->canChangeDisplayName());
}
}

/**
* don't allow display names containing whitespaces only
*/
Expand Down