Skip to content

Commit

Permalink
Refactor OC\Server::getSession
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Summers <18727110+summersab@users.noreply.github.com>
  • Loading branch information
summersab committed Aug 30, 2023
1 parent 613cd16 commit 01d8c0f
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 17 deletions.
4 changes: 3 additions & 1 deletion cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
*/
require_once __DIR__ . '/lib/versioncheck.php';

use OC\User\Session;

try {
require_once __DIR__ . '/lib/base.php';

Expand All @@ -54,7 +56,7 @@
// load all apps to get all api routes properly setup
OC_App::loadApps();

\OC::$server->getSession()->close();
\OC::$server->get(Session::class)->getSession()->close();

// initialize a dummy memory session
$session = new \OC\Session\Memory('');
Expand Down
5 changes: 3 additions & 2 deletions lib/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@

use OC\Encryption\HookManager;
use OC\Share20\Hooks;
use OC\User\Session;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Group\Events\UserRemovedEvent;
use OCP\ILogger;
Expand Down Expand Up @@ -753,7 +754,7 @@ public static function init(): void {

// User and Groups
if (!$systemConfig->getValue("installed", false)) {
self::$server->getSession()->set('user_id', '');
self::$server->get(Session::class)->getSession()->set('user_id', '');
}

OC_User::useBackend(new \OC\User\Database());
Expand Down Expand Up @@ -989,7 +990,7 @@ public static function handleRequest(): void {

// Check if Nextcloud is installed or in maintenance (update) mode
if (!$systemConfig->getValue('installed', false)) {
\OC::$server->getSession()->clear();
\OC::$server->get(Session::class)->getSession()->clear();
$setupHelper = new OC\Setup(
$systemConfig,
Server::get(\bantu\IniGetWrapper\IniGetWrapper::class),
Expand Down
3 changes: 2 additions & 1 deletion lib/private/AppFramework/DependencyInjection/DIContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
use OC\Log\PsrLoggerAdapter;
use OC\ServerContainer;
use OC\Settings\AuthorizedGroupMapper;
use OC\User\Session;
use OCA\WorkflowEngine\Manager;
use OCP\AppFramework\Http\IOutput;
use OCP\AppFramework\IAppContainer;
Expand Down Expand Up @@ -399,7 +400,7 @@ public function isAdminUser() {
}

private function getUserId() {
return $this->getServer()->getSession()->get('user_id');
return $this->getServer()->get(Session::class)->getSession()->get('user_id');
}

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/private/TemplateLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
use OC\Template\CSSResourceLocator;
use OC\Template\JSConfigHelper;
use OC\Template\JSResourceLocator;
use OC\User\Session;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\Defaults;
use OCP\IConfig;
Expand Down Expand Up @@ -228,7 +229,7 @@ public function __construct($renderAs, $appId = '') {
\OC::$server->getL10N('lib'),
\OCP\Server::get(Defaults::class),
\OC::$server->getAppManager(),
\OC::$server->getSession(),
\OC::$server->get(Session::class)->getSession(),
\OC::$server->getUserSession()->getUser(),
$this->config,
\OC::$server->getGroupManager(),
Expand Down
3 changes: 2 additions & 1 deletion lib/private/legacy/OC_Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
*
*/
use OC\TemplateLayout;
use OC\User\Session;
use OCP\AppFramework\Http\TemplateResponse;

require_once __DIR__.'/template/functions.php';
Expand Down Expand Up @@ -72,7 +73,7 @@ class OC_Template extends \OC\Template\Base {
public function __construct($app, $name, $renderAs = TemplateResponse::RENDER_AS_BLANK, $registerCall = true) {
$theme = OC_Util::getTheme();

$requestToken = (OC::$server->getSession() && $registerCall) ? \OCP\Util::callRegister() : '';
$requestToken = (OC::$server->get(Session::class)->getSession() && $registerCall) ? \OCP\Util::callRegister() : '';

$parts = explode('/', $app); // fix translation when app is something like core/lostpassword
$l10n = \OC::$server->getL10N($parts[0]);
Expand Down
5 changes: 3 additions & 2 deletions lib/private/legacy/OC_User.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
*/

use OC\User\LoginException;
use OC\User\Session;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\ILogger;
use OCP\IUserManager;
Expand Down Expand Up @@ -258,7 +259,7 @@ public static function setUserId($uid) {
if ($user = $userManager->get($uid)) {
$userSession->setUser($user);
} else {
\OC::$server->getSession()->set('user_id', $uid);
\OC::$server->get(Session::class)->getSession()->set('user_id', $uid);
}
}

Expand Down Expand Up @@ -338,7 +339,7 @@ public static function isAdminUser($uid) {
* @return string|false uid or false
*/
public static function getUser() {
$uid = \OC::$server->getSession() ? \OC::$server->getSession()->get('user_id') : null;
$uid = \OC::$server->get(Session::class)->getSession() ? \OC::$server->get(Session::class)->getSession()->get('user_id') : null;
if (!is_null($uid) && self::$incognitoMode === false) {
return $uid;
} else {
Expand Down
5 changes: 3 additions & 2 deletions lib/private/legacy/OC_Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@

use bantu\IniGetWrapper\IniGetWrapper;
use OC\Files\SetupManager;
use OC\User\Session;
use OCP\Files\Template\ITemplateManager;
use OCP\IConfig;
use OCP\IGroupManager;
Expand Down Expand Up @@ -507,7 +508,7 @@ public static function checkServer(\OC\SystemConfig $config) {
}

// Assume that if checkServer() succeeded before in this session, then all is fine.
if (\OC::$server->getSession()->exists('checkServer_succeeded') && \OC::$server->getSession()->get('checkServer_succeeded')) {
if (\OC::$server->get(Session::class)->getSession()->exists('checkServer_succeeded') && \OC::$server->get(Session::class)->getSession()->get('checkServer_succeeded')) {
return $errors;
}

Expand Down Expand Up @@ -715,7 +716,7 @@ public static function checkServer(\OC\SystemConfig $config) {
}

// Cache the result of this function
\OC::$server->getSession()->set('checkServer_succeeded', count($errors) == 0);
\OC::$server->get(Session::class)->getSession()->set('checkServer_succeeded', count($errors) == 0);

return $errors;
}
Expand Down
6 changes: 4 additions & 2 deletions tests/lib/UtilCheckServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace Test;

use OC\User\Session;

/**
* Tests for server check functions
*
Expand Down Expand Up @@ -41,7 +43,7 @@ protected function setUp(): void {
$this->datadir = \OC::$server->getTempManager()->getTemporaryFolder();

file_put_contents($this->datadir . '/.ocdata', '');
\OC::$server->getSession()->set('checkServer_succeeded', false);
\OC::$server->get(Session::class)->getSession()->set('checkServer_succeeded', false);
}

protected function tearDown(): void {
Expand Down Expand Up @@ -86,7 +88,7 @@ public function testCheckServerSkipDataDirValidityOnUpgrade() {
// simulate old version that didn't have it
unlink($this->datadir . '/.ocdata');

$session = \OC::$server->getSession();
$session = \OC::$server->get(Session::class)->getSession();
$oldCurrentVersion = $session->get('OC_Version');

// upgrade condition to simulate needUpgrade() === true
Expand Down
7 changes: 4 additions & 3 deletions tests/lib/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Test;

use OC\User\Session;
use OC_Util;

/**
Expand Down Expand Up @@ -186,18 +187,18 @@ public function filenameValidationProvider() {
public function testNeedUpgradeCore() {
$config = \OC::$server->getConfig();
$oldConfigVersion = $config->getSystemValue('version', '0.0.0');
$oldSessionVersion = \OC::$server->getSession()->get('OC_Version');
$oldSessionVersion = \OC::$server->get(Session::class)->getSession()->get('OC_Version');

$this->assertFalse(\OCP\Util::needUpgrade());

$config->setSystemValue('version', '7.0.0.0');
\OC::$server->getSession()->set('OC_Version', [7, 0, 0, 1]);
\OC::$server->get(Session::class)->getSession()->set('OC_Version', [7, 0, 0, 1]);
self::invokePrivate(new \OCP\Util, 'needUpgradeCache', [null]);

$this->assertTrue(\OCP\Util::needUpgrade());

$config->setSystemValue('version', $oldConfigVersion);
\OC::$server->getSession()->set('OC_Version', $oldSessionVersion);
\OC::$server->get(Session::class)->getSession()->set('OC_Version', $oldSessionVersion);
self::invokePrivate(new \OCP\Util, 'needUpgradeCache', [null]);

$this->assertFalse(\OCP\Util::needUpgrade());
Expand Down
5 changes: 3 additions & 2 deletions tests/startsessionlistener.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

use OC\Session\Memory;
use OC\User\Session;
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestListener;
use PHPUnit\Framework\TestListenerDefaultImplementation;
Expand All @@ -19,9 +20,9 @@ class StartSessionListener implements TestListener {

public function endTest(Test $test, float $time): void {
// reopen the session - only allowed for memory session
if (\OC::$server->getSession() instanceof Memory) {
if (\OC::$server->get(Session::class)->getSession() instanceof Memory) {
/** @var $session Memory */
$session = \OC::$server->getSession();
$session = \OC::$server->get(Session::class)->getSession();
$session->reopen();
}
}
Expand Down

0 comments on commit 01d8c0f

Please sign in to comment.