Skip to content

Commit

Permalink
Refactor OC\Server::getUserSession
Browse files Browse the repository at this point in the history
  • Loading branch information
summersab committed Aug 29, 2023
1 parent 613cd16 commit 9be4e73
Show file tree
Hide file tree
Showing 20 changed files with 52 additions and 31 deletions.
3 changes: 2 additions & 1 deletion core/Controller/LostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Mail\IMailer;
use OCP\Security\VerificationToken\IVerificationToken;
use OCP\Security\VerificationToken\InvalidTokenException;
Expand Down Expand Up @@ -239,7 +240,7 @@ public function setPassword(string $token, string $userId, string $password, boo
$this->twoFactorManager->clearTwoFactorPending($userId);

$this->config->deleteUserValue($userId, 'core', 'lostpassword');
@\OC::$server->getUserSession()->unsetMagicInCookie();
@\OC::$server->get(IUserSession::class)->unsetMagicInCookie();
} catch (HintException $e) {
$response = new JSONResponse($this->error($e->getHint()));
$response->throttle();
Expand Down
6 changes: 3 additions & 3 deletions lib/private/AppFramework/DependencyInjection/DIContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,9 @@ public function __construct(string $appName, array $urlParams = [], ServerContai
$c->get(IURLGenerator::class),
$server->get(LoggerInterface::class),
$c->get('AppName'),
$server->getUserSession()->isLoggedIn(),
$server->get(IUserSession::class)->isLoggedIn(),
$this->getUserId() !== null && $server->getGroupManager()->isAdmin($this->getUserId()),
$server->getUserSession()->getUser() !== null && $server->query(ISubAdmin::class)->isSubAdmin($server->getUserSession()->getUser()),
$server->get(IUserSession::class)->getUser() !== null && $server->query(ISubAdmin::class)->isSubAdmin($server->get(IUserSession::class)->getUser()),
$server->getAppManager(),
$server->getL10N('lib'),
$c->get(AuthorizedGroupMapper::class),
Expand Down Expand Up @@ -386,7 +386,7 @@ public function getAppName() {
* @return boolean
*/
public function isLoggedIn() {
return \OC::$server->getUserSession()->isLoggedIn();
return \OC::$server->get(IUserSession::class)->isLoggedIn();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/private/Authentication/TwoFactorAuth/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
use OCP\IConfig;
use OCP\ISession;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Session\Exceptions\SessionNotAvailableException;
use Psr\Log\LoggerInterface;
use function array_diff;
Expand Down Expand Up @@ -257,7 +258,7 @@ public function verifyChallenge(string $providerId, IUser $user, string $challen
if ($passed) {
if ($this->session->get(self::REMEMBER_LOGIN) === true) {
// TODO: resolve cyclic dependency and use DI
\OC::$server->getUserSession()->createRememberMeToken($user);
\OC::$server->get(IUserSession::class)->createRememberMeToken($user);
}
$this->session->remove(self::SESSION_UID_KEY);
$this->session->remove(self::REMEMBER_LOGIN);
Expand Down
5 changes: 3 additions & 2 deletions lib/private/Cache/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use OC\Files\Filesystem;
use OC\Files\View;
use OCP\ICache;
use OCP\IUserSession;
use OCP\Security\ISecureRandom;
use Psr\Log\LoggerInterface;

Expand All @@ -50,9 +51,9 @@ protected function getStorage() {
if ($this->storage !== null) {
return $this->storage;
}
if (\OC::$server->getUserSession()->isLoggedIn()) {
if (\OC::$server->get(IUserSession::class)->isLoggedIn()) {
$rootView = new View();
$user = \OC::$server->getUserSession()->getUser();
$user = \OC::$server->get(IUserSession::class)->getUser();
Filesystem::initMountPoints($user->getUID());
if (!$rootView->file_exists('/' . $user->getUID() . '/cache')) {
$rootView->mkdir('/' . $user->getUID() . '/cache');
Expand Down
3 changes: 2 additions & 1 deletion lib/private/Encryption/EncryptionWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OC\Memcache\ArrayCache;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\Storage;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -76,7 +77,7 @@ public function wrapStorage($mountPoint, Storage $storage, IMountPoint $mount) {
];

if (!$storage->instanceOfStorage(Storage\IDisableEncryptionStorage::class) && $mountPoint !== '/') {
$user = \OC::$server->getUserSession()->getUser();
$user = \OC::$server->get(IUserSession::class)->getUser();
$mountManager = Filesystem::getMountManager();
$uid = $user ? $user->getUID() : null;
$fileHelper = \OC::$server->getEncryptionFilesHelper();
Expand Down
3 changes: 2 additions & 1 deletion lib/private/Encryption/HookManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use OC\Files\Filesystem;
use OC\Files\View;
use OC\Files\SetupManager;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;

class HookManager {
Expand All @@ -52,7 +53,7 @@ public static function postRestore($params): void {

private static function getUpdate(?string $owner = null): Update {
if (is_null(self::$updater)) {
$user = \OC::$server->getUserSession()->getUser();
$user = \OC::$server->get(IUserSession::class)->getUser();
if (!$user && $owner) {
$user = \OC::$server->getUserManager()->get($owner);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/private/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
use OCP\Defaults;
use OCP\IGroup;
use OCP\IL10N;
use OCP\IUserSession;
use OCP\Security\ISecureRandom;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -431,7 +432,7 @@ public function install($options) {
// Create a session token for the newly created user
// The token provider requires a working db, so it's not injected on setup
/* @var $userSession User\Session */
$userSession = \OC::$server->getUserSession();
$userSession = \OC::$server->get(IUserSession::class);
$provider = \OCP\Server::get(PublicKeyTokenProvider::class);
$userSession->setTokenProvider($provider);
$userSession->login($username, $password);
Expand Down
3 changes: 2 additions & 1 deletion lib/private/Share/Share.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use OCP\DB\Exception;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\IUserSession;
use OCP\Share\IShare;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -918,7 +919,7 @@ private static function formatResult($items, $column, $backend, $format = self::
$statuses = [];
foreach ($items as $item) {
if ($item['share_type'] === IShare::TYPE_LINK) {
if ($item['uid_initiator'] !== \OC::$server->getUserSession()->getUser()->getUID()) {
if ($item['uid_initiator'] !== \OC::$server->get(IUserSession::class)->getUser()->getUID()) {
continue;
}
$statuses[$item[$column]]['link'] = true;
Expand Down
2 changes: 1 addition & 1 deletion lib/private/TemplateLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public function __construct($renderAs, $appId = '') {
\OCP\Server::get(Defaults::class),
\OC::$server->getAppManager(),
\OC::$server->getSession(),
\OC::$server->getUserSession()->getUser(),
\OC::$server->get(IUserSession::class)->getUser(),
$this->config,
\OC::$server->getGroupManager(),
\OC::$server->get(IniGetWrapper::class),
Expand Down
5 changes: 3 additions & 2 deletions lib/private/legacy/OC_App.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
use OCP\Authentication\IAlternativeLogin;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\ILogger;
use OCP\IUserSession;
use OC\AppFramework\Bootstrap\Coordinator;
use OC\App\DependencyAnalyzer;
use OC\App\Platform;
Expand Down Expand Up @@ -225,7 +226,7 @@ public static function getEnabledApps(bool $forceRefresh = false, bool $all = fa
if ($all) {
$user = null;
} else {
$user = \OC::$server->getUserSession()->getUser();
$user = \OC::$server->get(IUserSession::class)->getUser();
}

if (is_null($user)) {
Expand Down Expand Up @@ -863,7 +864,7 @@ private static function setupLiveMigrations(string $appId, array $steps) {
*/
public static function getStorage(string $appId) {
if (\OC::$server->getAppManager()->isEnabledForUser($appId)) { //sanity check
if (\OC::$server->getUserSession()->isLoggedIn()) {
if (\OC::$server->get(IUserSession::class)->isLoggedIn()) {
$view = new \OC\Files\View('/' . OC_User::getUser());
if (!$view->file_exists($appId)) {
$view->mkdir($appId);
Expand Down
3 changes: 2 additions & 1 deletion lib/private/legacy/OC_Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
use OCP\ICacheFactory;
use OCP\IBinaryFinder;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Util;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -526,7 +527,7 @@ public static function getStorageInfo($path, $rootInfo = null, $includeMountPoin
/** @var \OC\Files\Storage\Home $storage */
$user = $storage->getUser();
} else {
$user = \OC::$server->getUserSession()->getUser();
$user = \OC::$server->get(IUserSession::class)->getUser();
}
$quota = OC_Util::getUserQuota($user);
if ($quota !== \OCP\Files\FileInfo::SPACE_UNLIMITED) {
Expand Down
7 changes: 5 additions & 2 deletions lib/private/legacy/OC_JSON.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

use OCP\IUserSession;

class OC_JSON {
/**
* Check if the app is enabled, send json error msg if not
Expand All @@ -49,8 +52,8 @@ public static function checkAppEnabled($app) {
*/
public static function checkLoggedIn() {
$twoFactorAuthManger = \OC::$server->getTwoFactorAuthManager();
if (!\OC::$server->getUserSession()->isLoggedIn()
|| $twoFactorAuthManger->needsSecondFactor(\OC::$server->getUserSession()->getUser())) {
if (!\OC::$server->get(IUserSession::class)->isLoggedIn()
|| $twoFactorAuthManger->needsSecondFactor(\OC::$server->get(IUserSession::class)->getUser())) {
$l = \OC::$server->getL10N('lib');
http_response_code(\OCP\AppFramework\Http::STATUS_UNAUTHORIZED);
self::error([ 'data' => [ 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' ]]);
Expand Down
13 changes: 7 additions & 6 deletions lib/private/legacy/OC_User.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use OCP\EventDispatcher\IEventDispatcher;
use OCP\ILogger;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\User\Events\BeforeUserLoggedInEvent;
use OCP\User\Events\UserLoggedInEvent;

Expand Down Expand Up @@ -172,7 +173,7 @@ public static function loginWithApache(\OCP\Authentication\IApacheBackend $backe
if ($uid) {
if (self::getUser() !== $uid) {
self::setUserId($uid);
$userSession = \OC::$server->getUserSession();
$userSession = \OC::$server->get(IUserSession::class);

/** @var IEventDispatcher $dispatcher */
$dispatcher = \OC::$server->get(IEventDispatcher::class);
Expand Down Expand Up @@ -238,7 +239,7 @@ public static function handleApacheAuth() {

//setup extra user backends
self::setupBackends();
\OC::$server->getUserSession()->unsetMagicInCookie();
\OC::$server->get(IUserSession::class)->unsetMagicInCookie();

return self::loginWithApache($backend);
}
Expand All @@ -253,7 +254,7 @@ public static function handleApacheAuth() {
* @param string $uid
*/
public static function setUserId($uid) {
$userSession = \OC::$server->getUserSession();
$userSession = \OC::$server->get(IUserSession::class);
$userManager = \OC::$server->getUserManager();
if ($user = $userManager->get($uid)) {
$userSession->setUser($user);
Expand All @@ -265,11 +266,11 @@ public static function setUserId($uid) {
/**
* Check if the user is logged in, considers also the HTTP basic credentials
*
* @deprecated use \OC::$server->getUserSession()->isLoggedIn()
* @deprecated use \OC::$server->get(IUserSession::class)->isLoggedIn()
* @return bool
*/
public static function isLoggedIn() {
return \OC::$server->getUserSession()->isLoggedIn();
return \OC::$server->get(IUserSession::class)->isLoggedIn();
}

/**
Expand Down Expand Up @@ -302,7 +303,7 @@ public static function getLogoutUrl(\OCP\IURLGenerator $urlGenerator) {
return $backend->getLogoutUrl();
}

$user = \OC::$server->getUserSession()->getUser();
$user = \OC::$server->get(IUserSession::class)->getUser();
if ($user instanceof \OCP\IUser) {
$backend = $user->getBackend();
if ($backend instanceof \OCP\User\Backend\ICustomLogout) {
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 @@ -71,6 +71,7 @@
use OCP\IGroupManager;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Share\IManager;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -781,7 +782,7 @@ public static function checkDataDirectoryValidity($dataDirectory) {
*/
public static function checkLoggedIn() {
// Check if we are a user
if (!\OC::$server->getUserSession()->isLoggedIn()) {
if (!\OC::$server->get(IUserSession::class)->isLoggedIn()) {
header('Location: ' . \OC::$server->getURLGenerator()->linkToRoute(
'core.login.showLoginForm',
[
Expand All @@ -792,7 +793,7 @@ public static function checkLoggedIn() {
exit();
}
// Redirect to 2FA challenge selection if 2FA challenge was not solved yet
if (\OC::$server->getTwoFactorAuthManager()->needsSecondFactor(\OC::$server->getUserSession()->getUser())) {
if (\OC::$server->getTwoFactorAuthManager()->needsSecondFactor(\OC::$server->get(IUserSession::class)->getUser())) {
header('Location: ' . \OC::$server->getURLGenerator()->linkToRoute('core.TwoFactorChallenge.selectChallenge'));
exit();
}
Expand Down
3 changes: 2 additions & 1 deletion lib/public/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

use OC\AppScriptDependency;
use OC\AppScriptSort;
use OCP\IUserSession;
use bantu\IniGetWrapper\IniGetWrapper;
use Psr\Container\ContainerExceptionInterface;

Expand Down Expand Up @@ -134,7 +135,7 @@ public static function isSharingDisabledForUser() {
self::$shareManager = \OC::$server->getShareManager();
}

$user = \OC::$server->getUserSession()->getUser();
$user = \OC::$server->get(IUserSession::class)->getUser();
if ($user !== null) {
$user = $user->getUID();
}
Expand Down
4 changes: 3 additions & 1 deletion ocs/v1.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
require_once __DIR__ . '/../lib/versioncheck.php';
require_once __DIR__ . '/../lib/base.php';

use OCP\IUserSession;

if (\OCP\Util::needUpgrade()
|| \OC::$server->getConfig()->getSystemValueBool('maintenance')) {
// since the behavior of apps or remotes are unpredictable during
Expand Down Expand Up @@ -57,7 +59,7 @@
// side effects in existing apps
OC_App::loadApps();

if (!\OC::$server->getUserSession()->isLoggedIn()) {
if (!\OC::$server->get(IUserSession::class)->isLoggedIn()) {
OC::handleLogin(\OC::$server->getRequest());
}

Expand Down
3 changes: 2 additions & 1 deletion tests/lib/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OC\AppConfig;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IAppConfig;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -555,7 +556,7 @@ private function setupAppConfigMock() {
private function registerAppConfig(AppConfig $appConfig) {
$this->overwriteService(AppConfig::class, $appConfig);
$this->overwriteService(AppManager::class, new AppManager(
\OC::$server->getUserSession(),
\OC::$server->get(IUserSession::class),
\OC::$server->getConfig(),
$appConfig,
\OC::$server->getGroupManager(),
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/Files/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use OCP\Files\Config\IMountProvider;
use OCP\Files\Storage\IStorageFactory;
use OCP\IUser;
use OCP\IUserSession;

class DummyMountProvider implements IMountProvider {
private $mounts = [];
Expand Down Expand Up @@ -313,7 +314,7 @@ public function testHooks() {
\OC_User::useBackend($backend);
$backend->createUser($user, $user);
$userObj = \OC::$server->getUserManager()->get($user);
\OC::$server->getUserSession()->setUser($userObj);
\OC::$server->get(IUserSession::class)->setUser($userObj);
\OC\Files\Filesystem::init($user, '/' . $user . '/files');
}
\OC_Hook::clear('OC_Filesystem');
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IL10N;
use OCP\IUserSession;
use OCP\Security\ISecureRandom;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -425,7 +426,7 @@ protected static function logout() {
\OC_Util::tearDownFS();
\OC_User::setUserId('');
// needed for fully logout
\OC::$server->getUserSession()->setUser(null);
\OC::$server->get(IUserSession::class)->setUser(null);
}

/**
Expand Down
Loading

0 comments on commit 9be4e73

Please sign in to comment.