[PLT-7231/PLT-7306] Fix GitLab SSO failure with non-English locale and make config locales more forgiving (#7125)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
2ab2baba58
Коммит
9f3713aa98
10
i18n/en.json
10
i18n/en.json
@@ -6507,9 +6507,17 @@
|
||||
"id": "utils.config.save_config.saving.app_error",
|
||||
"translation": "An error occurred while saving the file to {{.Filename}}"
|
||||
},
|
||||
{
|
||||
"id": "utils.config.add_client_locale.app_error",
|
||||
"translation": "Unable to load mattermost configuration file: Adding DefaultClientLocale to AvailableLocales."
|
||||
},
|
||||
{
|
||||
"id": "utils.config.supported_available_locales.app_error",
|
||||
"translation": "Unable to load mattermost configuration file: AvailableLocales must include DefaultClientLocale. Setting AvailableLocales to all locales as default value."
|
||||
},
|
||||
{
|
||||
"id": "utils.config.supported_client_locale.app_error",
|
||||
"translation": "Unable to load mattermost configuration file: DefaultClientLocale must be one of the supported locales"
|
||||
"translation": "Unable to load mattermost configuration file: DefaultClientLocale must be one of the supported locales. Setting DefaultClientLocale to en as default value."
|
||||
},
|
||||
{
|
||||
"id": "utils.config.supported_server_locale.app_error",
|
||||
|
||||
@@ -343,7 +343,12 @@ func LoadConfig(fileName string) {
|
||||
}
|
||||
|
||||
if err := ValidateLocales(&config); err != nil {
|
||||
panic(T(err.Id))
|
||||
cfgMutex.Unlock()
|
||||
if err := SaveConfig(CfgFileName, &config); err != nil {
|
||||
err.Translate(T)
|
||||
l4g.Warn(err.Error())
|
||||
}
|
||||
cfgMutex.Lock()
|
||||
}
|
||||
|
||||
if err := ValidateLdapFilter(&config); err != nil {
|
||||
@@ -562,26 +567,44 @@ func ValidateLdapFilter(cfg *model.Config) *model.AppError {
|
||||
}
|
||||
|
||||
func ValidateLocales(cfg *model.Config) *model.AppError {
|
||||
var err *model.AppError
|
||||
locales := GetSupportedLocales()
|
||||
if _, ok := locales[*cfg.LocalizationSettings.DefaultServerLocale]; !ok {
|
||||
return model.NewLocAppError("ValidateLocales", "utils.config.supported_server_locale.app_error", nil, "")
|
||||
*cfg.LocalizationSettings.DefaultServerLocale = model.DEFAULT_LOCALE
|
||||
err = model.NewLocAppError("ValidateLocales", "utils.config.supported_server_locale.app_error", nil, "")
|
||||
}
|
||||
|
||||
if _, ok := locales[*cfg.LocalizationSettings.DefaultClientLocale]; !ok {
|
||||
return model.NewLocAppError("ValidateLocales", "utils.config.supported_client_locale.app_error", nil, "")
|
||||
*cfg.LocalizationSettings.DefaultClientLocale = model.DEFAULT_LOCALE
|
||||
err = model.NewLocAppError("ValidateLocales", "utils.config.supported_client_locale.app_error", nil, "")
|
||||
}
|
||||
|
||||
if len(*cfg.LocalizationSettings.AvailableLocales) > 0 {
|
||||
isDefaultClientLocaleInAvailableLocales := false
|
||||
for _, word := range strings.Split(*cfg.LocalizationSettings.AvailableLocales, ",") {
|
||||
if _, ok := locales[word]; !ok {
|
||||
*cfg.LocalizationSettings.AvailableLocales = ""
|
||||
isDefaultClientLocaleInAvailableLocales = true
|
||||
err = model.NewLocAppError("ValidateLocales", "utils.config.supported_available_locales.app_error", nil, "")
|
||||
break
|
||||
}
|
||||
|
||||
if word == *cfg.LocalizationSettings.DefaultClientLocale {
|
||||
return nil
|
||||
isDefaultClientLocaleInAvailableLocales = true
|
||||
}
|
||||
}
|
||||
|
||||
return model.NewLocAppError("ValidateLocales", "utils.config.validate_locale.app_error", nil, "")
|
||||
availableLocales := *cfg.LocalizationSettings.AvailableLocales
|
||||
|
||||
if !isDefaultClientLocaleInAvailableLocales {
|
||||
availableLocales += "," + *cfg.LocalizationSettings.DefaultClientLocale
|
||||
err = model.NewLocAppError("ValidateLocales", "utils.config.add_client_locale.app_error", nil, "")
|
||||
}
|
||||
|
||||
*cfg.LocalizationSettings.AvailableLocales = strings.Join(RemoveDuplicatesFromStringArray(strings.Split(availableLocales, ",")), ",")
|
||||
}
|
||||
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
|
||||
func Desanitize(cfg *model.Config) {
|
||||
|
||||
@@ -5,6 +5,7 @@ package utils
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -157,3 +158,122 @@ func TestConfigListener(t *testing.T) {
|
||||
t.Fatal("listener 2 should've been called")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateLocales(t *testing.T) {
|
||||
TranslationsPreInit()
|
||||
LoadConfig("config.json")
|
||||
|
||||
defaultServerLocale := *Cfg.LocalizationSettings.DefaultServerLocale
|
||||
defaultClientLocale := *Cfg.LocalizationSettings.DefaultClientLocale
|
||||
availableLocales := *Cfg.LocalizationSettings.AvailableLocales
|
||||
|
||||
defer func() {
|
||||
*Cfg.LocalizationSettings.DefaultClientLocale = defaultClientLocale
|
||||
*Cfg.LocalizationSettings.DefaultServerLocale = defaultServerLocale
|
||||
*Cfg.LocalizationSettings.AvailableLocales = availableLocales
|
||||
}()
|
||||
|
||||
*Cfg.LocalizationSettings.DefaultServerLocale = "en"
|
||||
*Cfg.LocalizationSettings.DefaultClientLocale = "en"
|
||||
*Cfg.LocalizationSettings.AvailableLocales = ""
|
||||
|
||||
// t.Logf("*Cfg.LocalizationSettings.DefaultClientLocale: %+v", *Cfg.LocalizationSettings.DefaultClientLocale)
|
||||
if err := ValidateLocales(Cfg); err != nil {
|
||||
t.Fatal("Should have not returned an error")
|
||||
}
|
||||
|
||||
// validate DefaultServerLocale
|
||||
*Cfg.LocalizationSettings.DefaultServerLocale = "junk"
|
||||
if err := ValidateLocales(Cfg); err != nil {
|
||||
if *Cfg.LocalizationSettings.DefaultServerLocale != "en" {
|
||||
t.Fatal("DefaultServerLocale should have assigned to en as a default value")
|
||||
}
|
||||
} else {
|
||||
|
||||
t.Fatal("Should have returned an error validating DefaultServerLocale")
|
||||
}
|
||||
|
||||
*Cfg.LocalizationSettings.DefaultServerLocale = ""
|
||||
if err := ValidateLocales(Cfg); err != nil {
|
||||
if *Cfg.LocalizationSettings.DefaultServerLocale != "en" {
|
||||
t.Fatal("DefaultServerLocale should have assigned to en as a default value")
|
||||
}
|
||||
} else {
|
||||
t.Fatal("Should have returned an error validating DefaultServerLocale")
|
||||
}
|
||||
|
||||
*Cfg.LocalizationSettings.AvailableLocales = "en"
|
||||
*Cfg.LocalizationSettings.DefaultServerLocale = "de"
|
||||
if err := ValidateLocales(Cfg); err != nil {
|
||||
if strings.Contains(*Cfg.LocalizationSettings.AvailableLocales, *Cfg.LocalizationSettings.DefaultServerLocale) {
|
||||
t.Fatal("DefaultServerLocale should not be added to AvailableLocales")
|
||||
}
|
||||
t.Fatal("Should have not returned an error validating DefaultServerLocale")
|
||||
}
|
||||
|
||||
// validate DefaultClientLocale
|
||||
*Cfg.LocalizationSettings.AvailableLocales = ""
|
||||
*Cfg.LocalizationSettings.DefaultClientLocale = "junk"
|
||||
if err := ValidateLocales(Cfg); err != nil {
|
||||
if *Cfg.LocalizationSettings.DefaultClientLocale != "en" {
|
||||
t.Fatal("DefaultClientLocale should have assigned to en as a default value")
|
||||
}
|
||||
} else {
|
||||
|
||||
t.Fatal("Should have returned an error validating DefaultClientLocale")
|
||||
}
|
||||
|
||||
*Cfg.LocalizationSettings.DefaultClientLocale = ""
|
||||
if err := ValidateLocales(Cfg); err != nil {
|
||||
if *Cfg.LocalizationSettings.DefaultClientLocale != "en" {
|
||||
t.Fatal("DefaultClientLocale should have assigned to en as a default value")
|
||||
}
|
||||
} else {
|
||||
t.Fatal("Should have returned an error validating DefaultClientLocale")
|
||||
}
|
||||
|
||||
*Cfg.LocalizationSettings.AvailableLocales = "en"
|
||||
*Cfg.LocalizationSettings.DefaultClientLocale = "de"
|
||||
if err := ValidateLocales(Cfg); err != nil {
|
||||
if !strings.Contains(*Cfg.LocalizationSettings.AvailableLocales, *Cfg.LocalizationSettings.DefaultClientLocale) {
|
||||
t.Fatal("DefaultClientLocale should have added to AvailableLocales")
|
||||
}
|
||||
} else {
|
||||
t.Fatal("Should have returned an error validating DefaultClientLocale")
|
||||
}
|
||||
|
||||
// validate AvailableLocales
|
||||
*Cfg.LocalizationSettings.DefaultServerLocale = "en"
|
||||
*Cfg.LocalizationSettings.DefaultClientLocale = "en"
|
||||
*Cfg.LocalizationSettings.AvailableLocales = "junk"
|
||||
if err := ValidateLocales(Cfg); err != nil {
|
||||
if *Cfg.LocalizationSettings.AvailableLocales != "" {
|
||||
t.Fatal("AvailableLocales should have assigned to empty string as a default value")
|
||||
}
|
||||
} else {
|
||||
t.Fatal("Should have returned an error validating AvailableLocales")
|
||||
}
|
||||
|
||||
*Cfg.LocalizationSettings.AvailableLocales = "en,de,junk"
|
||||
if err := ValidateLocales(Cfg); err != nil {
|
||||
if *Cfg.LocalizationSettings.AvailableLocales != "" {
|
||||
t.Fatal("AvailableLocales should have assigned to empty string as a default value")
|
||||
}
|
||||
} else {
|
||||
t.Fatal("Should have returned an error validating AvailableLocales")
|
||||
}
|
||||
|
||||
*Cfg.LocalizationSettings.DefaultServerLocale = "fr"
|
||||
*Cfg.LocalizationSettings.DefaultClientLocale = "de"
|
||||
*Cfg.LocalizationSettings.AvailableLocales = "en"
|
||||
if err := ValidateLocales(Cfg); err != nil {
|
||||
if strings.Contains(*Cfg.LocalizationSettings.AvailableLocales, *Cfg.LocalizationSettings.DefaultServerLocale) {
|
||||
t.Fatal("DefaultServerLocale should not be added to AvailableLocales")
|
||||
}
|
||||
if !strings.Contains(*Cfg.LocalizationSettings.AvailableLocales, *Cfg.LocalizationSettings.DefaultClientLocale) {
|
||||
t.Fatal("DefaultClientLocale should have added to AvailableLocales")
|
||||
}
|
||||
} else {
|
||||
t.Fatal("Should have returned an error validating AvailableLocales")
|
||||
}
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user