diff options
-rw-r--r-- | lib/private/Config/PresetManager.php | 88 | ||||
-rw-r--r-- | tests/lib/Config/LexiconTest.php | 3 |
2 files changed, 90 insertions, 1 deletions
diff --git a/lib/private/Config/PresetManager.php b/lib/private/Config/PresetManager.php index b45d41b06f8..b8418673157 100644 --- a/lib/private/Config/PresetManager.php +++ b/lib/private/Config/PresetManager.php @@ -8,22 +8,32 @@ declare(strict_types=1); namespace OC\Config; +use OC\App\AppManager; +use OC\Installer; +use OCP\App\AppPathNotFoundException; +use OCP\App\IAppManager; use OCP\Config\Lexicon\Preset; use OCP\IConfig; +use OCP\Server; +use Psr\Log\LoggerInterface; /** - * tools to maintains configurations + * tools to manage the Preset feature * * @since 32.0.0 */ class PresetManager { private const PRESET_CONFIGKEY = 'config_preset'; + private ?AppManager $appManager = null; + private ?Installer $installer = null; + private ?Preset $configLexiconPreset = null; public function __construct( private readonly IConfig $config, private readonly ConfigManager $configManager, + private readonly LoggerInterface $logger, ) { } @@ -35,6 +45,7 @@ class PresetManager { $this->config->setSystemValue(self::PRESET_CONFIGKEY, $preset->value); $this->configLexiconPreset = $preset; $this->configManager->clearConfigCaches(); + $this->refreshPresetApps(); } /** @@ -47,4 +58,79 @@ class PresetManager { return $this->configLexiconPreset; } + + /** + * Enable and/or Disable a list of apps based on the currently selected Preset + */ + public function refreshPresetApps(): void { + $this->loadAppManager(); + + $apps = $this->getPresetApps($this->getLexiconPreset()); + foreach ($apps['disabled'] ?? [] as $app) { + try { + $this->appManager->disableApp($app); + } catch (\Exception $e) { + $this->logger->warning('could not disable app', ['exception' => $e]); + } + } + + foreach ($apps['enabled'] ?? [] as $app) { + $this->installApp($app); + } + } + + /** + * some parts cannot be initiated at __construct() time + */ + private function loadAppManager(): void { + if ($this->appManager === null) { + $this->appManager = Server::get(IAppManager::class); + } + if ($this->installer === null) { + $this->installer = Server::get(Installer::class); + } + } + + /** + * download, install and enable app. + * generate warning entry in logs in case of failure. + */ + private function installApp(string $appId): void { + $this->loadAppManager(); + if (!$this->installer->isDownloaded($appId)) { + try { + $this->installer->downloadApp($appId); + } catch (\Exception $e) { + $this->logger->warning('could not download app', ['appId' => $appId, 'exception' => $e]); + return; + } + } + + try { + $this->installer->installApp($appId, true); + } catch (\Exception $e) { + $this->logger->warning('could not install app', ['appId' => $appId, 'exception' => $e]); + return; + } + + try { + $this->appManager->enableApp($appId); + } catch (AppPathNotFoundException $e) { + $this->logger->warning('could not enable app', ['appId' => $appId, 'exception' => $e]); + return; + } + } + + /** + * get listing of enabled/disabled app from Preset + * + * @return array{enabled: list<string>, disabled: list<string>} + */ + private function getPresetApps(Preset $preset): array { + return match ($preset) { + Preset::CLUB, Preset::FAMILY, Preset::SCHOOL, Preset::UNIVERSITY, Preset::SMALL, Preset::MEDIUM, Preset::LARGE => ['enabled' => ['intros', 'guests'], 'disabled' => []], + Preset::SHARED => ['enabled' => ['intros', 'external'], 'disabled' => []], + default => ['enabled' => [], 'disabled' => []], + }; + } } diff --git a/tests/lib/Config/LexiconTest.php b/tests/lib/Config/LexiconTest.php index d7e9b12a1cf..3f14721dd6e 100644 --- a/tests/lib/Config/LexiconTest.php +++ b/tests/lib/Config/LexiconTest.php @@ -11,6 +11,7 @@ use OC\AppConfig; use OC\AppFramework\Bootstrap\Coordinator; use OC\Config\ConfigManager; use OC\Config\PresetManager; +use OCP\App\IAppManager; use OCP\Config\Exceptions\TypeConflictException; use OCP\Config\Exceptions\UnknownKeyException; use OCP\Config\IUserConfig; @@ -34,6 +35,7 @@ class LexiconTest extends TestCase { private IUserConfig $userConfig; private ConfigManager $configManager; private PresetManager $presetManager; + private IAppManager $appManager; protected function setUp(): void { parent::setUp(); @@ -48,6 +50,7 @@ class LexiconTest extends TestCase { $this->userConfig = Server::get(IUserConfig::class); $this->configManager = Server::get(ConfigManager::class); $this->presetManager = Server::get(PresetManager::class); + $this->appManager = Server::get(IAppManager::class); } protected function tearDown(): void { |