Skip to content

Commit

Permalink
During installation no UserSession is available and no database conne…
Browse files Browse the repository at this point in the history
…ction - behave accordingly
  • Loading branch information
DeepDiver1975 committed Mar 23, 2017
1 parent 9489104 commit a253b56
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 32 deletions.
17 changes: 10 additions & 7 deletions lib/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public static function checkSingleUserMode($lockIfNoUserLoggedIn = false) {
/**
* Checks if the version requires an update and shows
* @param bool $showTemplate Whether an update screen should get shown
* @return bool|void
* @return bool
*/
public static function checkUpgrade($showTemplate = true) {
if (\OCP\Util::needUpgrade()) {
Expand Down Expand Up @@ -391,6 +391,7 @@ private static function printUpgradePage() {
\OCP\Util::addScript('update');
\OCP\Util::addStyle('update');

/** @var \OC\App\AppManager $appManager */
$appManager = \OC::$server->getAppManager();

$tmpl = new OC_Template('', 'update.admin', 'guest');
Expand Down Expand Up @@ -627,13 +628,13 @@ public static function init() {
$systemConfig = \OC::$server->getSystemConfig();

// User and Groups
if (!$systemConfig->getValue("installed", false)) {
if ($systemConfig->getValue("installed", false)) {
OC_User::useBackend(new \OC\User\Database());
\OC::$server->getGroupManager()->addBackend(new \OC\Group\Database());
} else {
self::$server->getSession()->set('user_id', '');
}

OC_User::useBackend(new \OC\User\Database());
\OC::$server->getGroupManager()->addBackend(new \OC\Group\Database());

// Subscribe to the hook
\OCP\Util::connectHook(
'\OCA\Files_Sharing\API\Server2Server',
Expand All @@ -657,8 +658,10 @@ public static function init() {
}
self::registerShareHooks();
self::registerLogRotate();
self::registerEncryptionWrapper();
self::registerEncryptionHooks();
if ($systemConfig->getValue("installed", false)) {
self::registerEncryptionWrapper();
self::registerEncryptionHooks();
}

//make sure temporary files are cleaned up
$tmpManager = \OC::$server->getTempManager();
Expand Down
4 changes: 2 additions & 2 deletions lib/private/App/AppManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class AppManager implements IAppManager {
* @param ICacheFactory $memCacheFactory
* @param EventDispatcherInterface $dispatcher
*/
public function __construct(IUserSession $userSession,
public function __construct(IUserSession $userSession = null,
IAppConfig $appConfig,
IGroupManager $groupManager,
ICacheFactory $memCacheFactory,
Expand Down Expand Up @@ -152,7 +152,7 @@ public function isEnabledForUser($appId, $user = null) {
if ($this->isAlwaysEnabled($appId)) {
return true;
}
if (is_null($user)) {
if (is_null($user) && !is_null($this->userSession)) {
$user = $this->userSession->getUser();
}
$installedApps = $this->getInstalledAppsValues();
Expand Down
27 changes: 14 additions & 13 deletions lib/private/Files/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
use Icewind\Streams\CallbackWrapper;
use OC\Files\Mount\MoveableMount;
use OC\Files\Storage\Storage;
use OC\User\User;
use OC\User\RemoteUser;
use OCP\Constants;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\FileNameTooLongException;
Expand All @@ -61,6 +61,7 @@
use OCP\Lock\ILockingProvider;
use OCP\Lock\LockedException;
use OCA\Files_Sharing\SharedMount;
use OCP\Util;

/**
* Class to provide access to ownCloud filesystem via a "view", and methods for
Expand Down Expand Up @@ -959,7 +960,7 @@ public function fopen($path, $mode) {
$hooks[] = 'write';
break;
default:
\OCP\Util::writeLog('core', 'invalid mode (' . $mode . ') for ' . $path, \OCP\Util::ERROR);
Util::writeLog('core', 'invalid mode (' . $mode . ') for ' . $path, Util::ERROR);
}

return $this->basicOperation('fopen', $path, $hooks, $mode);
Expand Down Expand Up @@ -1262,15 +1263,15 @@ public function hasUpdated($path, $time) {

/**
* @param string $ownerId
* @return \OC\User\User
* @return IUser
*/
private function getUserObjectForOwner($ownerId) {
$owner = $this->userManager->get($ownerId);
if ($owner instanceof IUser) {
return $owner;
} else {
return new User($ownerId, null);
if (!$owner instanceof IUser) {
return new RemoteUser($ownerId);
}

return $owner;
}

/**
Expand Down Expand Up @@ -1408,7 +1409,7 @@ public function getDirectoryContent($directory, $mimetype_filter = '') {
$folderId = $data['fileid'];
$contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter

$sharingDisabled = \OCP\Util::isSharingDisabledForUser();
$sharingDisabled = Util::isSharingDisabledForUser();
/**
* @var \OC\Files\FileInfo[] $files
*/
Expand Down Expand Up @@ -1443,11 +1444,11 @@ public function getDirectoryContent($directory, $mimetype_filter = '') {
continue;
} catch (\Exception $e) {
// sometimes when the storage is not available it can be any exception
\OCP\Util::writeLog(
Util::writeLog(
'core',
'Exception while scanning storage "' . $subStorage->getId() . '": ' .
get_class($e) . ': ' . $e->getMessage(),
\OCP\Util::ERROR
Util::ERROR
);
continue;
}
Expand Down Expand Up @@ -1486,7 +1487,7 @@ public function getDirectoryContent($directory, $mimetype_filter = '') {
$rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/

// if sharing was disabled for the user we remove the share permissions
if (\OCP\Util::isSharingDisabledForUser()) {
if (Util::isSharingDisabledForUser()) {
$rootEntry['permissions'] = $rootEntry['permissions'] & ~\OCP\Constants::PERMISSION_SHARE;
}

Expand Down Expand Up @@ -1740,9 +1741,9 @@ private function canMove(MoveableMount $mount1, $target) {

list($targetStorage, $targetInternalPath) = \OC\Files\Filesystem::resolvePath($target);
if (!$targetStorage->instanceOfStorage('\OCP\Files\IHomeStorage')) {
\OCP\Util::writeLog('files',
Util::writeLog('files',
'It is not allowed to move one mount point into another one',
\OCP\Util::DEBUG);
Util::DEBUG);
return false;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/private/Group/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
namespace OC\Group;

use OCP\IGroup;
use OCP\IUser;

class Group implements IGroup {
/**
Expand Down Expand Up @@ -117,7 +118,7 @@ public function getUsers() {
/**
* check if a user is in the group
*
* @param \OC\User\User $user
* @param IUser $user
* @return bool
*/
public function inGroup($user) {
Expand Down
4 changes: 2 additions & 2 deletions lib/private/L10N/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Factory implements IFactory {
*/
public function __construct(IConfig $config,
IRequest $request,
IUserSession $userSession,
IUserSession $userSession = null,
$serverRoot) {
$this->config = $config;
$this->request = $request;
Expand Down Expand Up @@ -128,7 +128,7 @@ public function findLanguage($app = null) {
*
* @link https://github.com/owncloud/core/issues/21955
*/
if($this->config->getSystemValue('installed', false)) {
if(!is_null($this->userSession) && $this->config->getSystemValue('installed', false)) {
$userId = !is_null($this->userSession->getUser()) ? $this->userSession->getUser()->getUID() : null;
if(!is_null($userId)) {
$userLang = $this->config->getUserValue($userId, 'core', 'lang', null);
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Security/CSRF/TokenStorage/SessionStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class SessionStorage {
/**
* @param ISession $session
*/
public function __construct(ISession $session) {
public function __construct(ISession $session = null) {
$this->session = $session;
}

Expand Down
30 changes: 24 additions & 6 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,15 @@
use OC\Security\SecureRandom;
use OC\Security\TrustedDomainHelper;
use OC\Session\CryptoWrapper;
use OC\Session\Memory;
use OC\Settings\Panels\Helper;
use OC\Settings\SettingsManager;
use OC\Tagging\TagMapper;
use OC\Theme\ThemeService;
use OC\User\AccountMapper;
use OCP\IL10N;
use OCP\IServerContainer;
use OCP\ISession;
use OCP\Security\IContentSecurityPolicyManager;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
Expand Down Expand Up @@ -115,7 +117,7 @@ public function __construct($webRoot, \OC\Config $config) {
parent::__construct();
$this->webRoot = $webRoot;

$this->registerService('SettingsManager', function($c) {
$this->registerService('SettingsManager', function(Server $c) {
return new SettingsManager(
$c->getL10N('core'),
$c->getAppManager(),
Expand Down Expand Up @@ -444,6 +446,11 @@ public function __construct($webRoot, \OC\Config $config) {
});
$this->registerService('DatabaseConnection', function (Server $c) {
$systemConfig = $c->getSystemConfig();
$keys = $systemConfig->getKeys();
if (!isset($keys['dbname']) && !isset($keys['dbhost']) && isset($keys['dbtableprefix'])) {
throw new \OC\DatabaseException('No database configured');
}

$factory = new \OC\DB\ConnectionFactory($systemConfig);
$type = $systemConfig->getValue('dbtype', 'sqlite');
if (!$factory->isValidType($type)) {
Expand Down Expand Up @@ -954,21 +961,32 @@ public function getGroupManager() {
* @return \OC\User\Session
*/
public function getUserSession() {
if($this->getConfig()->getSystemValue('installed', false) === false) {
return null;
}
return $this->query('UserSession');
}

/**
* @return \OCP\ISession
* @return ISession
*/
public function getSession() {
return $this->query('UserSession')->getSession();
$userSession = $this->getUserSession();
if (is_null($userSession)) {
return new Memory('');
}
return $userSession->getSession();
}

/**
* @param \OCP\ISession $session
* @param ISession $session
*/
public function setSession(\OCP\ISession $session) {
return $this->query('UserSession')->setSession($session);
public function setSession(ISession $session) {
$userSession = $this->getUserSession();
if (is_null($userSession)) {
return;
}
$userSession->setSession($session);
}

/**
Expand Down
Loading

0 comments on commit a253b56

Please sign in to comment.