summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Appelman <icewind@owncloud.com>2015-02-02 14:47:29 +0100
committerRobin Appelman <icewind@owncloud.com>2015-02-16 15:15:35 +0100
commit2b58e8489fcb5eff0e2312209beca038bfe9b40a (patch)
treea86ba1a304e63e71f8a075a6401fa6449e0551be
parent9c47ab91f2c27ced6b80ce44003809358da76ee2 (diff)
downloadnextcloud-server-2b58e8489fcb5eff0e2312209beca038bfe9b40a.tar.gz
nextcloud-server-2b58e8489fcb5eff0e2312209beca038bfe9b40a.zip
Add getInstalledApps and getAppsForUser to the app manager
-rw-r--r--lib/private/app/appmanager.php66
-rw-r--r--lib/public/app/iappmanager.php17
-rw-r--r--tests/lib/app/manager.php32
3 files changed, 98 insertions, 17 deletions
diff --git a/lib/private/app/appmanager.php b/lib/private/app/appmanager.php
index 20a765e3434..f35fc3e5e6b 100644
--- a/lib/private/app/appmanager.php
+++ b/lib/private/app/appmanager.php
@@ -12,6 +12,7 @@ namespace OC\App;
use OCP\App\IAppManager;
use OCP\IAppConfig;
use OCP\IGroupManager;
+use OCP\IUser;
use OCP\IUserSession;
class AppManager implements IAppManager {
@@ -49,7 +50,7 @@ class AppManager implements IAppManager {
/**
* @return string[] $appId => $enabled
*/
- private function getInstalledApps() {
+ private function getInstalledAppsValues() {
if (!$this->installedAppsCache) {
$values = $this->appConfig->getValues(false, 'enabled');
$this->installedAppsCache = array_filter($values, function ($value) {
@@ -61,6 +62,29 @@ class AppManager implements IAppManager {
}
/**
+ * List all installed apps
+ *
+ * @return string[]
+ */
+ public function getInstalledApps() {
+ return array_keys($this->getInstalledAppsValues());
+ }
+
+ /**
+ * List all apps enabled for a user
+ *
+ * @param \OCP\IUser $user
+ * @return string[]
+ */
+ public function getAppsEnabledForUser(IUser $user) {
+ $apps = $this->getInstalledAppsValues();
+ $appsForUser = array_filter($apps, function ($enabled) use ($user) {
+ return $this->checkAppForUser($enabled, $user);
+ });
+ return array_keys($appsForUser);
+ }
+
+ /**
* Check if an app is enabled for user
*
* @param string $appId
@@ -71,24 +95,32 @@ class AppManager implements IAppManager {
if (is_null($user)) {
$user = $this->userSession->getUser();
}
- $installedApps = $this->getInstalledApps();
+ $installedApps = $this->getInstalledAppsValues();
if (isset($installedApps[$appId])) {
- $enabled = $installedApps[$appId];
- if ($enabled === 'yes') {
- return true;
- } elseif (is_null($user)) {
- return false;
- } else {
- $groupIds = json_decode($enabled);
- $userGroups = $this->groupManager->getUserGroupIds($user);
- foreach ($userGroups as $groupId) {
- if (array_search($groupId, $groupIds) !== false) {
- return true;
- }
+ return $this->checkAppForUser($installedApps[$appId], $user);
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * @param string $enabled
+ * @param IUser $user
+ * @return bool
+ */
+ private function checkAppForUser($enabled, $user) {
+ if ($enabled === 'yes') {
+ return true;
+ } elseif (is_null($user)) {
+ return false;
+ } else {
+ $groupIds = json_decode($enabled);
+ $userGroups = $this->groupManager->getUserGroupIds($user);
+ foreach ($userGroups as $groupId) {
+ if (array_search($groupId, $groupIds) !== false) {
+ return true;
}
- return false;
}
- } else {
return false;
}
}
@@ -100,7 +132,7 @@ class AppManager implements IAppManager {
* @return bool
*/
public function isInstalled($appId) {
- $installedApps = $this->getInstalledApps();
+ $installedApps = $this->getInstalledAppsValues();
return isset($installedApps[$appId]);
}
diff --git a/lib/public/app/iappmanager.php b/lib/public/app/iappmanager.php
index ebd84a1ce9d..c79dcf9a573 100644
--- a/lib/public/app/iappmanager.php
+++ b/lib/public/app/iappmanager.php
@@ -9,6 +9,8 @@
namespace OCP\App;
+use OCP\IUser;
+
interface IAppManager {
/**
* Check if an app is enabled for user
@@ -48,4 +50,19 @@ interface IAppManager {
* @param string $appId
*/
public function disableApp($appId);
+
+ /**
+ * List all apps enabled for a user
+ *
+ * @param \OCP\IUser $user
+ * @return string[]
+ */
+ public function getAppsEnabledForUser(IUser $user);
+
+ /**
+ * List all installed apps
+ *
+ * @return string[]
+ */
+ public function getInstalledApps();
}
diff --git a/tests/lib/app/manager.php b/tests/lib/app/manager.php
index 4c0555b501f..4eaf4f08be9 100644
--- a/tests/lib/app/manager.php
+++ b/tests/lib/app/manager.php
@@ -192,4 +192,36 @@ class Manager extends \PHPUnit_Framework_TestCase {
$appConfig->setValue('test', 'enabled', '["foo"]');
$this->assertTrue($manager->isEnabledForUser('test'));
}
+
+ public function testGetInstalledApps() {
+ $userSession = $this->getMock('\OCP\IUserSession');
+ $groupManager = $this->getMock('\OCP\IGroupManager');
+
+ $appConfig = $this->getAppConfig();
+ $manager = new \OC\App\AppManager($userSession, $appConfig, $groupManager);
+ $appConfig->setValue('test1', 'enabled', 'yes');
+ $appConfig->setValue('test2', 'enabled', 'no');
+ $appConfig->setValue('test3', 'enabled', '["foo"]');
+ $this->assertEquals(['test1', 'test3'], $manager->getInstalledApps());
+ }
+
+ public function testGetAppsForUser() {
+ $userSession = $this->getMock('\OCP\IUserSession');
+ $groupManager = $this->getMock('\OCP\IGroupManager');
+
+ $user = new User('user1', null);
+
+ $groupManager->expects($this->any())
+ ->method('getUserGroupIds')
+ ->with($user)
+ ->will($this->returnValue(array('foo', 'bar')));
+
+ $appConfig = $this->getAppConfig();
+ $manager = new \OC\App\AppManager($userSession, $appConfig, $groupManager);
+ $appConfig->setValue('test1', 'enabled', 'yes');
+ $appConfig->setValue('test2', 'enabled', 'no');
+ $appConfig->setValue('test3', 'enabled', '["foo"]');
+ $appConfig->setValue('test4', 'enabled', '["asd"]');
+ $this->assertEquals(['test1', 'test3'], $manager->getAppsEnabledForUser($user));
+ }
}