From 6292f665d7d10f23cedc71befb2acb2ef467870a Mon Sep 17 00:00:00 2001
From: Joas Schilling <coding@schilljs.com>
Date: Wed, 4 Oct 2017 16:35:10 +0200
Subject: [PATCH] Allow multiple settings and sections per app

Signed-off-by: Joas Schilling <coding@schilljs.com>
---
 lib/private/App/InfoParser.php     | 27 +++++++++++++++++
 lib/private/Settings/Manager.php   | 48 ++++++++++++++++++++----------
 tests/data/app/expected-info.json  |  6 ++++
 tests/lib/Settings/ManagerTest.php |  4 +--
 4 files changed, 67 insertions(+), 18 deletions(-)

diff --git a/lib/private/App/InfoParser.php b/lib/private/App/InfoParser.php
index 0531682d67a..40eaaf21bca 100644
--- a/lib/private/App/InfoParser.php
+++ b/lib/private/App/InfoParser.php
@@ -122,6 +122,21 @@ class InfoParser {
 		if (!array_key_exists('providers', $array['activity'])) {
 			$array['activity']['providers'] = [];
 		}
+		if (!array_key_exists('settings', $array)) {
+			$array['settings'] = [];
+		}
+		if (!array_key_exists('admin', $array['settings'])) {
+			$array['settings']['admin'] = [];
+		}
+		if (!array_key_exists('admin-section', $array['settings'])) {
+			$array['settings']['admin-section'] = [];
+		}
+		if (!array_key_exists('personal', $array['settings'])) {
+			$array['settings']['personal'] = [];
+		}
+		if (!array_key_exists('personal-section', $array['settings'])) {
+			$array['settings']['personal-section'] = [];
+		}
 
 		if (array_key_exists('types', $array)) {
 			if (is_array($array['types'])) {
@@ -171,6 +186,18 @@ class InfoParser {
 		) {
 			$array['collaboration']['collaborators']['searchPlugins'] = $array['collaboration']['collaborators']['searchPlugins']['searchPlugin'];
 		}
+		if (isset($array['settings']['admin']) && !is_array($array['settings']['admin'])) {
+			$array['settings']['admin'] = [$array['settings']['admin']];
+		}
+		if (isset($array['settings']['admin-section']) && !is_array($array['settings']['admin-section'])) {
+			$array['settings']['admin-section'] = [$array['settings']['admin-section']];
+		}
+		if (isset($array['settings']['personal']) && !is_array($array['settings']['personal'])) {
+			$array['settings']['personal'] = [$array['settings']['personal']];
+		}
+		if (isset($array['settings']['personal-section']) && !is_array($array['settings']['personal-section'])) {
+			$array['settings']['personal-section'] = [$array['settings']['personal-section']];
+		}
 
 		if(!is_null($this->cache)) {
 			$this->cache->set($fileCacheKey, json_encode($array));
diff --git a/lib/private/Settings/Manager.php b/lib/private/Settings/Manager.php
index 3c5ff5670da..8de72ce314e 100644
--- a/lib/private/Settings/Manager.php
+++ b/lib/private/Settings/Manager.php
@@ -127,18 +127,26 @@ class Manager implements IManager {
 	 * @inheritdoc
 	 */
 	public function setupSettings(array $settings) {
-		if (isset($settings[IManager::KEY_ADMIN_SECTION])) {
-			$this->setupSectionEntry($settings[IManager::KEY_ADMIN_SECTION], 'admin');
+		if (!empty($settings[IManager::KEY_ADMIN_SECTION])) {
+			foreach ($settings[IManager::KEY_ADMIN_SECTION] as $className) {
+				$this->setupSectionEntry($className, 'admin');
+			}
 		}
-		if (isset($settings[IManager::KEY_ADMIN_SETTINGS])) {
-			$this->setupSettingsEntry($settings[IManager::KEY_ADMIN_SETTINGS], 'admin');
+		if (!empty($settings[IManager::KEY_ADMIN_SETTINGS])) {
+			foreach ($settings[IManager::KEY_ADMIN_SETTINGS] as $className) {
+				$this->setupSettingsEntry($className, 'admin');
+			}
 		}
 
-		if (isset($settings[IManager::KEY_PERSONAL_SECTION])) {
-			$this->setupSectionEntry($settings[IManager::KEY_PERSONAL_SECTION], 'personal');
+		if (!empty($settings[IManager::KEY_PERSONAL_SECTION])) {
+			foreach ($settings[IManager::KEY_PERSONAL_SECTION] as $className) {
+				$this->setupSectionEntry($className, 'personal');
+			}
 		}
-		if (isset($settings[IManager::KEY_PERSONAL_SETTINGS])) {
-			$this->setupSettingsEntry($settings[IManager::KEY_PERSONAL_SETTINGS], 'personal');
+		if (!empty($settings[IManager::KEY_PERSONAL_SETTINGS])) {
+			foreach ($settings[IManager::KEY_PERSONAL_SETTINGS] as $className) {
+				$this->setupSettingsEntry($className, 'personal');
+			}
 		}
 	}
 
@@ -153,18 +161,26 @@ class Manager implements IManager {
 	public function onAppDisabled($appId) {
 		$appInfo = \OC_App::getAppInfo($appId); // hello static legacy
 
-		if (isset($appInfo['settings'][IManager::KEY_ADMIN_SECTION])) {
-			$this->mapper->remove(Mapper::TABLE_ADMIN_SECTIONS, trim($appInfo['settings'][IManager::KEY_ADMIN_SECTION], '\\'));
+		if (!empty($appInfo['settings'][IManager::KEY_ADMIN_SECTION])) {
+			foreach ($appInfo['settings'][IManager::KEY_ADMIN_SECTION] as $className) {
+				$this->mapper->remove(Mapper::TABLE_ADMIN_SECTIONS, trim($className, '\\'));
+			}
 		}
-		if (isset($appInfo['settings'][IManager::KEY_ADMIN_SETTINGS])) {
-			$this->mapper->remove(Mapper::TABLE_ADMIN_SETTINGS, trim($appInfo['settings'][IManager::KEY_ADMIN_SETTINGS], '\\'));
+		if (!empty($appInfo['settings'][IManager::KEY_ADMIN_SETTINGS])) {
+			foreach ($appInfo['settings'][IManager::KEY_ADMIN_SETTINGS] as $className) {
+				$this->mapper->remove(Mapper::TABLE_ADMIN_SETTINGS, trim($className, '\\'));
+			}
 		}
 
-		if (isset($appInfo['settings'][IManager::KEY_PERSONAL_SECTION])) {
-			$this->mapper->remove(Mapper::TABLE_PERSONAL_SECTIONS, trim($appInfo['settings'][IManager::KEY_PERSONAL_SECTION], '\\'));
+		if (!empty($appInfo['settings'][IManager::KEY_PERSONAL_SECTION])) {
+			foreach ($appInfo['settings'][IManager::KEY_PERSONAL_SECTION] as $className) {
+				$this->mapper->remove(Mapper::TABLE_PERSONAL_SECTIONS, trim($className, '\\'));
+			}
 		}
-		if (isset($appInfo['settings'][IManager::KEY_PERSONAL_SETTINGS])) {
-			$this->mapper->remove(Mapper::TABLE_PERSONAL_SETTINGS, trim($appInfo['settings'][IManager::KEY_PERSONAL_SETTINGS], '\\'));
+		if (!empty($appInfo['settings'][IManager::KEY_PERSONAL_SETTINGS])) {
+			foreach ($appInfo['settings'][IManager::KEY_PERSONAL_SETTINGS] as $className) {
+				$this->mapper->remove(Mapper::TABLE_PERSONAL_SETTINGS, trim($className, '\\'));
+			}
 		}
 	}
 
diff --git a/tests/data/app/expected-info.json b/tests/data/app/expected-info.json
index 0666b902f2c..8527f18a2c0 100644
--- a/tests/data/app/expected-info.json
+++ b/tests/data/app/expected-info.json
@@ -81,5 +81,11 @@
 		"filters": [],
 		"settings": [],
 		"providers": []
+	},
+	"settings": {
+		"admin": [],
+		"admin-section": [],
+		"personal": [],
+		"personal-section": []
 	}
 }
diff --git a/tests/lib/Settings/ManagerTest.php b/tests/lib/Settings/ManagerTest.php
index 6a13b737c8e..be5aad2e414 100644
--- a/tests/lib/Settings/ManagerTest.php
+++ b/tests/lib/Settings/ManagerTest.php
@@ -147,7 +147,7 @@ class ManagerTest extends TestCase {
 			->method('add');
 
 		$this->manager->setupSettings([
-			$type => $className,
+			$type => [$className],
 		]);
 	}
 
@@ -174,7 +174,7 @@ class ManagerTest extends TestCase {
 			->method('update');
 
 		$this->manager->setupSettings([
-			$type => 'OCA\Files\Settings\Admin',
+			$type => ['OCA\Files\Settings\Admin'],
 		]);
 	}
 
-- 
GitLab