From 7a5752acad3c70c16f49f177d10ac3d21ec9d039 Mon Sep 17 00:00:00 2001
From: Andreas Fernandez <a.fernandez@scripting-base.de>
Date: Thu, 23 Apr 2020 14:41:46 +0200
Subject: [PATCH] [BUGFIX] Re-determine first start module if configuration is
 invalid

This patch fixes two flaws with the determination and loading of the
first available backend module:

* ModuleLoader::getModuleSetupInformation() only sets default options if
  the configuration array is not empty
* The availability of the module is checked. If the configured module
  doesn't exist, the first available module is determined again.

Resolves: #91210
Releases: master
Change-Id: Idae8de6e0bf253754c4d44e50b15e81038e1315f
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/64309
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Daniel Siepmann <coding@daniel-siepmann.de>
Tested-by: Benni Mack <benni@typo3.org>
Reviewed-by: Daniel Siepmann <coding@daniel-siepmann.de>
Reviewed-by: Josef Glatz <josefglatz@gmail.com>
Reviewed-by: Benni Mack <benni@typo3.org>
---
 .../Classes/Controller/BackendController.php    | 17 +++++++++++------
 .../backend/Classes/Module/ModuleLoader.php     |  2 +-
 2 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/typo3/sysext/backend/Classes/Controller/BackendController.php b/typo3/sysext/backend/Classes/Controller/BackendController.php
index 7b882bc981ad..357e64e39e33 100644
--- a/typo3/sysext/backend/Classes/Controller/BackendController.php
+++ b/typo3/sysext/backend/Classes/Controller/BackendController.php
@@ -93,6 +93,11 @@ class BackendController
      */
     protected $moduleStorage;
 
+    /**
+     * @var ModuleLoader
+     */
+    protected $moduleLoader;
+
     /**
      * Constructor
      */
@@ -104,6 +109,10 @@ class BackendController
         $this->uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
         $this->typo3Version = GeneralUtility::makeInstance(Typo3Version::class);
         $this->pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
+        $this->moduleLoader = GeneralUtility::makeInstance(ModuleLoader::class);
+        $this->moduleLoader->observeWorkspaces = true;
+        $this->moduleLoader->load($GLOBALS['TBE_MODULES']);
+
         // Add default BE javascript
         $this->pageRenderer->addJsFile('EXT:backend/Resources/Public/JavaScript/md5.js');
         $this->pageRenderer->addJsFile('EXT:backend/Resources/Public/JavaScript/backend.js');
@@ -451,7 +460,7 @@ class BackendController
                 $startModule = $beUser->uc['startModuleOnFirstLogin'];
                 unset($beUser->uc['startModuleOnFirstLogin']);
                 $beUser->writeUC();
-            } elseif ($beUser->uc['startModule']) {
+            } elseif ($this->moduleLoader->checkMod($beUser->uc['startModule']) !== 'notFound') {
                 $startModule = $beUser->uc['startModule'];
             } else {
                 $startModule = $this->determineFirstAvailableBackendModule();
@@ -481,11 +490,7 @@ class BackendController
 
     protected function determineFirstAvailableBackendModule(): string
     {
-        $loadModules = GeneralUtility::makeInstance(ModuleLoader::class);
-        $loadModules->observeWorkspaces = true;
-        $loadModules->load($GLOBALS['TBE_MODULES']);
-
-        foreach ($loadModules->modules as $mainMod => $modData) {
+        foreach ($this->moduleLoader->modules as $mainMod => $modData) {
             $hasSubmodules = !empty($modData['sub']) && is_array($modData['sub']);
             $isStandalone = $modData['standalone'] ?? false;
             if ($isStandalone) {
diff --git a/typo3/sysext/backend/Classes/Module/ModuleLoader.php b/typo3/sysext/backend/Classes/Module/ModuleLoader.php
index 6bea2e21fda0..a5eb2336f70c 100644
--- a/typo3/sysext/backend/Classes/Module/ModuleLoader.php
+++ b/typo3/sysext/backend/Classes/Module/ModuleLoader.php
@@ -226,7 +226,7 @@ class ModuleLoader
         }
 
         // Add some default configuration
-        if (!isset($moduleSetupInformation['configuration']['inheritNavigationComponentFromMainModule'])) {
+        if ($moduleSetupInformation['configuration'] !== [] && !isset($moduleSetupInformation['configuration']['inheritNavigationComponentFromMainModule'])) {
             $moduleSetupInformation['configuration']['inheritNavigationComponentFromMainModule'] = true;
         }
 
-- 
GitLab