From ed55ddb169bffdca2855813b12889c5ed7b17014 Mon Sep 17 00:00:00 2001 From: Imko Schumacher Date: Thu, 12 May 2022 21:57:03 +0200 Subject: [PATCH] Handle FlashMessage exceptions inside upgrade wizard When this class is called from the install tool/upgrade wizard, the globals `LANG` and `BE_USER` may not be set and cause exceptions. --- .../Service/TimeTable/ExternalTimeTable.php | 9 ++-- Classes/Utility/HelperUtility.php | 49 +++++++++++++++++-- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/Classes/Service/TimeTable/ExternalTimeTable.php b/Classes/Service/TimeTable/ExternalTimeTable.php index b743fe6c..9b6c5f30 100644 --- a/Classes/Service/TimeTable/ExternalTimeTable.php +++ b/Classes/Service/TimeTable/ExternalTimeTable.php @@ -14,7 +14,6 @@ use HDNET\Calendarize\Utility\HelperUtility; use TYPO3\CMS\Core\Messaging\FlashMessage; use TYPO3\CMS\Core\Utility\GeneralUtility; -use TYPO3\CMS\Extbase\Utility\LocalizationUtility; /** * External service. @@ -60,9 +59,9 @@ public function handleConfiguration(array &$times, Configuration $configuration) try { $fileName = $this->iCalUrlService->getOrCreateLocalFileForUrl($externalIcsUrl); } catch (UnableToGetFileForUrlException $e) { - HelperUtility::createFlashMessage( + HelperUtility::createTranslatedTitleFlashMessage( $e->getMessage(), - LocalizationUtility::translate('flashMessage.invalidExternalUrl.title', 'calendarize') ?? '', + 'flashMessage.invalidExternalUrl.title', FlashMessage::ERROR ); @@ -72,9 +71,9 @@ public function handleConfiguration(array &$times, Configuration $configuration) try { $externalTimes = $this->iCalService->getEvents($fileName); } catch (\Exception $e) { - HelperUtility::createFlashMessage( + HelperUtility::createTranslatedTitleFlashMessage( $e->getMessage(), - LocalizationUtility::translate('flashMessage.unableToProcessEvents.title', 'calendarize') ?? '', + 'flashMessage.unableToProcessEvents.title', FlashMessage::ERROR ); diff --git a/Classes/Utility/HelperUtility.php b/Classes/Utility/HelperUtility.php index be616b12..88a578ff 100644 --- a/Classes/Utility/HelperUtility.php +++ b/Classes/Utility/HelperUtility.php @@ -7,6 +7,7 @@ namespace HDNET\Calendarize\Utility; +use TYPO3\CMS\Core\Authentication\AbstractUserAuthentication; use TYPO3\CMS\Core\Core\Environment; use TYPO3\CMS\Core\Database\Connection; use TYPO3\CMS\Core\Database\ConnectionPool; @@ -18,6 +19,8 @@ use TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface; use TYPO3\CMS\Extbase\Persistence\QueryInterface; use TYPO3\CMS\Extbase\Utility\LocalizationUtility; +use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication; +use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; /** * Helper Utility. @@ -57,7 +60,8 @@ public static function getQuery($objectName) public static function createFlashMessage(string $message, string $title = '', int $mode = FlashMessage::OK): void { // Don't store flash messages in CLI context - $storeInSession = !Environment::isCli(); + // Note: the getUserByContext check is only required for TYPO3 v10 and is fixed in v11 (94418). + $storeInSession = !Environment::isCli() && null !== self::getUserByContext(); $flashMessage = GeneralUtility::makeInstance(FlashMessage::class, $message, $title, $mode, $storeInSession); $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class); $messageQueue = $flashMessageService->getMessageQueueByIdentifier(); @@ -75,8 +79,32 @@ public static function createFlashMessage(string $message, string $title = '', i */ public static function createTranslatedFlashMessage(string $messageKey, string $titleKey = '', int $mode = FlashMessage::OK): void { - $message = LocalizationUtility::translate($messageKey, 'calendarize') ?? $messageKey; - $title = LocalizationUtility::translate($titleKey, 'calendarize') ?? $titleKey; + try { + $message = LocalizationUtility::translate($messageKey, 'calendarize') ?? $messageKey; + $title = LocalizationUtility::translate($titleKey, 'calendarize') ?? $titleKey; + } catch (\TypeError $e) { + $message = $messageKey; + $title = $titleKey; + } + self::createFlashMessage($message, $title, $mode); + } + + /** + * Create a flash message with a translated title. + * + * @param string $messageKey + * @param string $titleKey + * @param int $mode + * + * @throws Exception + */ + public static function createTranslatedTitleFlashMessage(string $message, string $titleKey = '', int $mode = FlashMessage::OK): void + { + try { + $title = LocalizationUtility::translate($titleKey, 'calendarize') ?? $titleKey; + } catch (\TypeError $e) { + $title = $titleKey; + } self::createFlashMessage($message, $title, $mode); } @@ -91,4 +119,19 @@ public static function getDatabaseConnection($table) { return GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($table); } + + /** + * Gets user object by context. + * This class is also used in install tool, where $GLOBALS['BE_USER'] is not set and can be null. + * + * @return AbstractUserAuthentication|null + */ + protected static function getUserByContext(): ?AbstractUserAuthentication + { + if (($GLOBALS['TSFE'] ?? null) instanceof TypoScriptFrontendController && $GLOBALS['TSFE']->fe_user instanceof FrontendUserAuthentication) { + return $GLOBALS['TSFE']->fe_user; + } + + return $GLOBALS['BE_USER'] ?? null; + } }