diff --git a/config/config.sample.php b/config/config.sample.php index 59e1f3890c..e613609bce 100755 --- a/config/config.sample.php +++ b/config/config.sample.php @@ -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 */ diff --git a/lib/private/app.php b/lib/private/app.php index 9fb0ec2e34..0ca2ca36bd 100644 --- a/lib/private/app.php +++ b/lib/private/app.php @@ -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)) { diff --git a/lib/private/util.php b/lib/private/util.php index 7836489832..897795f753 100755 --- a/lib/private/util.php +++ b/lib/private/util.php @@ -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(); } diff --git a/tests/lib/util.php b/tests/lib/util.php index aaa47f033d..c2bb99c3b2 100644 --- a/tests/lib/util.php +++ b/tests/lib/util.php @@ -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; + } }