Merge pull request #9334 from owncloud/defaultappfix

Default app fix
This commit is contained in:
Vincent Petry 2014-07-01 20:39:13 +02:00
commit 3d921ed3c3
4 changed files with 101 additions and 7 deletions

View File

@ -74,7 +74,11 @@ $CONFIG = array(
/* URL to the parent directory of the 3rdparty directory, as seen by the browser */
"3rdpartyurl" => "",
/* Default app to load on login */
/* Default app to open on login.
* This can be a comma-separated list of app ids.
* If the first app is not enabled for the current user,
* it will try with the second one and so on. If no enabled app could be found,
* the "files" app will be displayed instead. */
"defaultapp" => "files",
/* Enable the help menu item in the settings */

View File

@ -163,7 +163,7 @@ class OC_App {
/**
* get all enabled apps
*/
private static $enabledAppsCache = array();
protected static $enabledAppsCache = array();
public static function getEnabledApps($forceRefresh = false) {
if (!OC_Config::getValue('installed', false)) {

View File

@ -815,10 +815,13 @@ class OC_Util {
}
/**
* Redirect to the user default page
* @return void
* Returns the URL of the default page
* based on the system configuration and
* the apps visible for the current user
*
* @return string URL
*/
public static function redirectToDefaultPage() {
public static function getDefaultPageUrl() {
$urlGenerator = \OC::$server->getURLGenerator();
if(isset($_REQUEST['redirect_url'])) {
$location = urldecode($_REQUEST['redirect_url']);
@ -827,11 +830,30 @@ class OC_Util {
if ($defaultPage) {
$location = $urlGenerator->getAbsoluteURL($defaultPage);
} else {
$location = $urlGenerator->getAbsoluteURL('/index.php/apps/files');
$appId = 'files';
$defaultApps = explode(',', \OCP\Config::getSystemValue('defaultapp', 'files'));
// find the first app that is enabled for the current user
foreach ($defaultApps as $defaultApp) {
$defaultApp = OC_App::cleanAppId(strip_tags($defaultApp));
if (OC_App::isEnabled($defaultApp)) {
$appId = $defaultApp;
break;
}
}
$location = $urlGenerator->getAbsoluteURL('/index.php/apps/' . $appId . '/');
}
}
return $location;
}
/**
* Redirect to the user default page
* @return void
*/
public static function redirectToDefaultPage() {
$location = self::getDefaultPageUrl();
OC_Log::write('core', 'redirectToDefaultPage: '.$location, OC_Log::DEBUG);
header( 'Location: '.$location );
header('Location: '.$location);
exit();
}

View File

@ -290,4 +290,72 @@ class Test_Util extends PHPUnit_Framework_TestCase {
array(array('g1', 'g2', 'g3'), array('g1', 'g2'), array('g1', 'g2', 'g3'), true),
);
}
/**
* Test default apps
*
* @dataProvider defaultAppsProvider
*/
function testDefaultApps($defaultAppConfig, $expectedPath, $enabledApps) {
$oldDefaultApps = \OCP\Config::getSystemValue('core', 'defaultapp', '');
// CLI is doing messy stuff with the webroot, so need to work it around
$oldWebRoot = \OC::$WEBROOT;
\OC::$WEBROOT = '';
Dummy_OC_App::setEnabledApps($enabledApps);
\OCP\Config::setSystemValue('defaultapp', $defaultAppConfig);
$this->assertEquals('http://localhost/' . $expectedPath, \OC_Util::getDefaultPageUrl());
// restore old state
\OC::$WEBROOT = $oldWebRoot;
Dummy_OC_App::restore();
\OCP\Config::setSystemValue('defaultapp', $oldDefaultApps);
}
function defaultAppsProvider() {
return array(
// none specified, default to files
array(
'',
'index.php/apps/files/',
array('files'),
),
// unexisting or inaccessible app specified, default to files
array(
'unexist',
'index.php/apps/files/',
array('files'),
),
// non-standard app
array(
'calendar',
'index.php/apps/calendar/',
array('files', 'calendar'),
),
// non-standard app with fallback
array(
'contacts,calendar',
'index.php/apps/calendar/',
array('files', 'calendar'),
),
);
}
}
/**
* Dummy OC Apps class to make it possible to override
* enabled apps
*/
class Dummy_OC_App extends OC_App {
private static $enabledAppsCacheBackup;
public static function setEnabledApps($enabledApps) {
self::$enabledAppsCacheBackup = self::$enabledAppsCache;
self::$enabledAppsCache = $enabledApps;
}
public static function restore() {
self::$enabledAppsCache = self::$enabledAppsCacheBackup;
}
}