From 7dede49aee599f59994a4ee6f18b441189b4291d Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 8 Mar 2025 05:38:51 +0100 Subject: [PATCH] Add command to validate various settings. --- .../ValidatesEnvironmentVariables.php | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 app/Console/Commands/Integrity/ValidatesEnvironmentVariables.php diff --git a/app/Console/Commands/Integrity/ValidatesEnvironmentVariables.php b/app/Console/Commands/Integrity/ValidatesEnvironmentVariables.php new file mode 100644 index 0000000000..3034ab9b97 --- /dev/null +++ b/app/Console/Commands/Integrity/ValidatesEnvironmentVariables.php @@ -0,0 +1,93 @@ +validateLanguage(); + $this->validateGuard(); + $this->validateStaticToken(); + return Command::SUCCESS; + } + + private function validateLanguage(): void + { + $language = env('DEFAULT_LANGUAGE', 'en_US'); + $locale = env('DEFAULT_LOCALE', 'equal'); + $options = array_keys(config('firefly.languages')); + if (!in_array($language, $options)) { + $this->friendlyError(sprintf('DEFAULT_LANGUAGE "%s" is not a valid language for Firefly III.', $language)); + $this->friendlyError('Please check your .env file and make sure you use a valid setting.'); + $this->friendlyError(sprintf('Valid languages are: %s', implode(', ', $options))); + exit(1); + } + $options[] = 'equal'; + if (!in_array($locale, $options)) { + $this->friendlyError(sprintf('DEFAULT_LOCALE "%s" is not a valid local for Firefly III.', $locale)); + $this->friendlyError('Please check your .env file and make sure you use a valid setting.'); + $this->friendlyError(sprintf('Valid locales are: %s', implode(', ', $options))); + exit(1); + } + } + + private function validateGuard(): void { + $guard = env('AUTHENTICATION_GUARD','web'); + if('web' !== $guard && 'remote_user_guard' !== $guard) { + $this->friendlyError(sprintf('AUTHENTICATION_GUARD "%s" is not a valid guard for Firefly III.', $guard)); + $this->friendlyError('Please check your .env file and make sure you use a valid setting.'); + $this->friendlyError('Valid guards are: web, remote_user_guard'); + exit(1); + } + } + + private function validateStaticToken(): void { + $token = (string) env('STATIC_CRON_TOKEN',''); + if(0 !== strlen($token) && 32 !== strlen($token)) { + $this->friendlyError('STATIC_CRON_TOKEN must be empty or a 32-character string.'); + $this->friendlyError('Please check your .env file and make sure you use a valid setting.'); + exit(1); + } + } +}