From 894d69184abc78750160d4818412e45fdc22a81b Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Tue, 1 Jul 2014 14:28:45 +0200 Subject: [PATCH 1/4] Add bak defaultapp setting --- lib/private/util.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/private/util.php b/lib/private/util.php index 7836489832..424c27e74a 100755 --- a/lib/private/util.php +++ b/lib/private/util.php @@ -827,7 +827,9 @@ class OC_Util { if ($defaultPage) { $location = $urlGenerator->getAbsoluteURL($defaultPage); } else { - $location = $urlGenerator->getAbsoluteURL('/index.php/apps/files'); + $defaultApp = \OCP\Config::getSystemValue('defaultapp', 'files'); + $defaultApp = OC_App::cleanAppId(strip_tags($defaultApp)); + $location = $urlGenerator->getAbsoluteURL('/index.php/apps/' . $defaultApp); } } OC_Log::write('core', 'redirectToDefaultPage: '.$location, OC_Log::DEBUG); From c005515ebd3fcf1d79070b3e7e2a783cf2ea53e1 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Tue, 1 Jul 2014 15:42:26 +0200 Subject: [PATCH 2/4] Support for multiple default apps If a default app isn't visible for the user, try the next one. Else fallback to the "files" app. --- config/config.sample.php | 6 +++++- lib/private/util.php | 14 +++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) 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/util.php b/lib/private/util.php index 424c27e74a..e9e081a48f 100755 --- a/lib/private/util.php +++ b/lib/private/util.php @@ -827,9 +827,17 @@ class OC_Util { if ($defaultPage) { $location = $urlGenerator->getAbsoluteURL($defaultPage); } else { - $defaultApp = \OCP\Config::getSystemValue('defaultapp', 'files'); - $defaultApp = OC_App::cleanAppId(strip_tags($defaultApp)); - $location = $urlGenerator->getAbsoluteURL('/index.php/apps/' . $defaultApp); + $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->linkTo($appId, 'index.php'); } } OC_Log::write('core', 'redirectToDefaultPage: '.$location, OC_Log::DEBUG); From 3e78f41d00a6b6d2f2dbc90beb9fb0a3e85f6cf4 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Tue, 1 Jul 2014 16:02:38 +0200 Subject: [PATCH 3/4] Use getAbsoluteUrl for redirection URL Also separate the function into getDefaultPageUrl() and redirectToDefaultPage() to make it testable. --- lib/private/util.php | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/private/util.php b/lib/private/util.php index e9e081a48f..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']); @@ -837,11 +840,20 @@ class OC_Util { break; } } - $location = $urlGenerator->linkTo($appId, 'index.php'); + $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(); } From 7c174520289dc5337dda1d32352e7542cb329670 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Tue, 1 Jul 2014 16:55:29 +0200 Subject: [PATCH 4/4] Added unit test for default app URL --- lib/private/app.php | 2 +- tests/lib/util.php | 68 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) 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/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; + } }