From b4784be2de6b841529e3dc3ec4c1ae1aade0121a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Wed, 21 Mar 2018 16:11:45 +0100 Subject: Receive the email date as parameter (#8484) --- utils/mail.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'utils') diff --git a/utils/mail.go b/utils/mail.go index c59406a18..2a2da9bf1 100644 --- a/utils/mail.go +++ b/utils/mail.go @@ -221,10 +221,10 @@ func SendMailUsingConfigAdvanced(mimeTo, smtpTo string, from mail.Address, subje return err } - return SendMail(c, mimeTo, smtpTo, from, subject, htmlBody, attachments, mimeHeaders, fileBackend) + return SendMail(c, mimeTo, smtpTo, from, subject, htmlBody, attachments, mimeHeaders, fileBackend, time.Now()) } -func SendMail(c *smtp.Client, mimeTo, smtpTo string, from mail.Address, subject, htmlBody string, attachments []*model.FileInfo, mimeHeaders map[string]string, fileBackend FileBackend) *model.AppError { +func SendMail(c *smtp.Client, mimeTo, smtpTo string, from mail.Address, subject, htmlBody string, attachments []*model.FileInfo, mimeHeaders map[string]string, fileBackend FileBackend, date time.Time) *model.AppError { l4g.Debug(T("utils.mail.send_mail.sending.debug"), mimeTo, subject) htmlMessage := "\r\n" + htmlBody + "" @@ -249,7 +249,7 @@ func SendMail(c *smtp.Client, mimeTo, smtpTo string, from mail.Address, subject, m := gomail.NewMessage(gomail.SetCharset("UTF-8")) m.SetHeaders(headers) - m.SetDateHeader("Date", time.Now()) + m.SetDateHeader("Date", date) m.SetBody("text/plain", txtBody) m.AddAlternative("text/html", htmlMessage) -- cgit v1.2.3-1-g7c22 From 9d701c704416a1d8648dd2818a8a15c4da99b424 Mon Sep 17 00:00:00 2001 From: Jesse Hallam Date: Wed, 21 Mar 2018 14:27:14 -0400 Subject: Fix various segfaults when running `go test` manually (#8448) * failing to find i18n shouldn't segfault The server was trying to handle the fact that it couldn't find the i18n directory, by emitting a translated log message... * fix utils.FindDir The attempts to find the directory in the parent or grandparent directory don't work if the current working directory was inside `enterprise`, with `enterprise` itself being a symlink as per the usual developer setup. Recurse to the root of the filesystem, cleaning the path along the way to work around this limitation (and allow tests to be run from an arbitrarily deep nesting level.) Fix corresponding usages to employ filepath.Join. * failing to find html templates shouldn't segfault * fail fast if the test user cannot be created * rework utils.FindDir to retain backwards compatibility --- utils/config.go | 30 ++++++++++++------------------ utils/html.go | 10 ++++++++-- utils/i18n.go | 12 +++++++----- utils/license.go | 3 ++- 4 files changed, 29 insertions(+), 26 deletions(-) (limited to 'utils') diff --git a/utils/config.go b/utils/config.go index 93a870743..5e9849930 100644 --- a/utils/config.go +++ b/utils/config.go @@ -9,7 +9,6 @@ import ( "io" "io/ioutil" "os" - "path" "path/filepath" "strconv" "strings" @@ -51,21 +50,17 @@ func FindConfigFile(fileName string) (path string) { return "" } +// FindDir looks for the given directory in nearby ancestors, falling back to `./` if not found. func FindDir(dir string) (string, bool) { - fileName := "." - found := false - if _, err := os.Stat("./" + dir + "/"); err == nil { - fileName, _ = filepath.Abs("./" + dir + "/") - found = true - } else if _, err := os.Stat("../" + dir + "/"); err == nil { - fileName, _ = filepath.Abs("../" + dir + "/") - found = true - } else if _, err := os.Stat("../../" + dir + "/"); err == nil { - fileName, _ = filepath.Abs("../../" + dir + "/") - found = true + for _, parent := range []string{".", "..", "../.."} { + foundDir, err := filepath.Abs(filepath.Join(parent, dir)) + if err != nil { + continue + } else if _, err := os.Stat(foundDir); err == nil { + return foundDir, true + } } - - return fileName + "/", found + return "./", false } func DisableDebugLogForTest() { @@ -136,11 +131,10 @@ func ConfigureLog(s *model.LogSettings) { func GetLogFileLocation(fileLocation string) string { if fileLocation == "" { - logDir, _ := FindDir("logs") - return logDir + LOG_FILENAME - } else { - return path.Join(fileLocation, LOG_FILENAME) + fileLocation, _ = FindDir("logs") } + + return filepath.Join(fileLocation, LOG_FILENAME) } func SaveConfig(fileName string, config *model.Config) *model.AppError { diff --git a/utils/html.go b/utils/html.go index 6bbe55c6d..f9a7abe5b 100644 --- a/utils/html.go +++ b/utils/html.go @@ -5,8 +5,10 @@ package utils import ( "bytes" + "errors" "html/template" "io" + "path/filepath" "reflect" "sync/atomic" @@ -39,7 +41,7 @@ func NewHTMLTemplateWatcher(directory string) (*HTMLTemplateWatcher, error) { return nil, err } - if htmlTemplates, err := template.ParseGlob(templatesDir + "*.html"); err != nil { + if htmlTemplates, err := template.ParseGlob(filepath.Join(templatesDir, "*.html")); err != nil { return nil, err } else { ret.templates.Store(htmlTemplates) @@ -56,7 +58,7 @@ func NewHTMLTemplateWatcher(directory string) (*HTMLTemplateWatcher, error) { case event := <-watcher.Events: if event.Op&fsnotify.Write == fsnotify.Write { l4g.Info("Re-parsing templates because of modified file %v", event.Name) - if htmlTemplates, err := template.ParseGlob(templatesDir + "*.html"); err != nil { + if htmlTemplates, err := template.ParseGlob(filepath.Join(templatesDir, "*.html")); err != nil { l4g.Error("Failed to parse templates %v", err) } else { ret.templates.Store(htmlTemplates) @@ -103,6 +105,10 @@ func (t *HTMLTemplate) Render() string { } func (t *HTMLTemplate) RenderToWriter(w io.Writer) error { + if t.Templates == nil { + return errors.New("no html templates") + } + if err := t.Templates.ExecuteTemplate(w, t.TemplateName, t); err != nil { l4g.Error(T("api.api.render.error"), t.TemplateName, err) return err diff --git a/utils/i18n.go b/utils/i18n.go index 8ed82d19f..7b8d1fef0 100644 --- a/utils/i18n.go +++ b/utils/i18n.go @@ -23,13 +23,15 @@ var settings model.LocalizationSettings // this functions loads translations from filesystem // and assign english while loading server config func TranslationsPreInit() error { + // Set T even if we fail to load the translations. Lots of shutdown handling code will + // segfault trying to handle the error, and the untranslated IDs are strictly better. + T = TfuncWithFallback("en") + TDefault = TfuncWithFallback("en") + if err := InitTranslationsWithDir("i18n"); err != nil { return err } - T = TfuncWithFallback("en") - TDefault = TfuncWithFallback("en") - return nil } @@ -51,9 +53,9 @@ func InitTranslationsWithDir(dir string) error { for _, f := range files { if filepath.Ext(f.Name()) == ".json" { filename := f.Name() - locales[strings.Split(filename, ".")[0]] = i18nDirectory + filename + locales[strings.Split(filename, ".")[0]] = filepath.Join(i18nDirectory, filename) - if err := i18n.LoadTranslationFile(i18nDirectory + filename); err != nil { + if err := i18n.LoadTranslationFile(filepath.Join(i18nDirectory, filename)); err != nil { return err } } diff --git a/utils/license.go b/utils/license.go index 2853a58d0..cf874b62b 100644 --- a/utils/license.go +++ b/utils/license.go @@ -12,6 +12,7 @@ import ( "encoding/pem" "io/ioutil" "os" + "path/filepath" "strconv" "strings" @@ -114,7 +115,7 @@ func GetLicenseFileFromDisk(fileName string) []byte { func GetLicenseFileLocation(fileLocation string) string { if fileLocation == "" { configDir, _ := FindDir("config") - return configDir + "mattermost.mattermost-license" + return filepath.Join(configDir, "mattermost.mattermost-license") } else { return fileLocation } -- cgit v1.2.3-1-g7c22 From 74e703f58daff2aded07a969410f988cff9ada9b Mon Sep 17 00:00:00 2001 From: Chris Duarte Date: Thu, 22 Mar 2018 06:53:43 -0700 Subject: Timezone feature (#8185) * Add supported timezones into config Remove Timezone list creation in favor of timezone from configs Add Timezone field to Users table Clean up format of SupportedTimezones in config * Remove unwanted change * Add test for updating user timezone * Add empty map[string]string if Timezone is null * Add EnableTimezoneSelection config * Revert back to map[string]string for ClientConfig * Refactor SupportedTimezones into timezones.json * Include timezones.json in TestConfigFlag * Add timezone api endpoint * Bump varchar size to 256 and setMaxSize in user_store * Refactor LoadConfig to LoadConfig and LoadTimezoneConfig * Remove unnecessary argument in LoadConfig, mail_test * Add test for timezone endpoint * Add license header * Refactor timezones endpoint to system.go * Add system base route to timezone endpoint * db timezone upgrade in db v4.9 * Avoid saving SupportedTimezones to config.json * Add timezonePath support in config * Remove EnableTimezoneSelection from config * Use return statement without return parameter * Refactor test for SupportedTimezones * Check for supportedTimezone != nil instead of using len * Decouple SupportedTimezones out of Config * Fix failing test * Add LastTeamIconUpdate back in upgrade.go * Write timezone config in config_flag_test * Add code fallback for default timezone support --- utils/config_test.go | 9 +++++++++ utils/timezone.go | 25 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 utils/timezone.go (limited to 'utils') diff --git a/utils/config_test.go b/utils/config_test.go index 5809422f1..6d6181a3d 100644 --- a/utils/config_test.go +++ b/utils/config_test.go @@ -21,6 +21,15 @@ func TestConfig(t *testing.T) { InitTranslations(cfg.LocalizationSettings) } +func TestTimezoneConfig(t *testing.T) { + TranslationsPreInit() + supportedTimezones := LoadTimezones("timezones.json") + assert.Equal(t, len(supportedTimezones) > 0, true) + + supportedTimezones2 := LoadTimezones("timezones_file_does_not_exists.json") + assert.Equal(t, len(supportedTimezones2) > 0, true) +} + func TestFindConfigFile(t *testing.T) { dir, err := ioutil.TempDir("", "") require.NoError(t, err) diff --git a/utils/timezone.go b/utils/timezone.go new file mode 100644 index 000000000..ea5f15140 --- /dev/null +++ b/utils/timezone.go @@ -0,0 +1,25 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package utils + +import ( + "encoding/json" + "io/ioutil" + + "github.com/mattermost/mattermost-server/model" +) + +func LoadTimezones(fileName string) model.SupportedTimezones { + var supportedTimezones model.SupportedTimezones + + if timezoneFile := FindConfigFile(fileName); timezoneFile == "" { + return model.DefaultSupportedTimezones + } else if raw, err := ioutil.ReadFile(timezoneFile); err != nil { + return model.DefaultSupportedTimezones + } else if err := json.Unmarshal(raw, &supportedTimezones); err != nil { + return model.DefaultSupportedTimezones + } else { + return supportedTimezones + } +} -- cgit v1.2.3-1-g7c22 From 6036ddad8598cc69511a259ffdafcce570653717 Mon Sep 17 00:00:00 2001 From: Jesse Hallam Date: Thu, 22 Mar 2018 10:57:29 -0400 Subject: MM-9804: emit defaults for all enterprise config (#8490) * MM-9804: emit defaults for all enterprise config This prevents the client from having to write inverted checks if a certain value defaults as `'true'` instead of `'false'`. * move EnableMobileFile(Upload|Download) to a defaulted enterprise configuration --- utils/config.go | 51 ++++++++++++++++++++++++- utils/config_test.go | 105 ++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 148 insertions(+), 8 deletions(-) (limited to 'utils') diff --git a/utils/config.go b/utils/config.go index 5e9849930..163838a13 100644 --- a/utils/config.go +++ b/utils/config.go @@ -408,8 +408,6 @@ func GenerateClientConfig(c *model.Config, diagnosticId string, license *model.L props["SupportEmail"] = *c.SupportSettings.SupportEmail props["EnableFileAttachments"] = strconv.FormatBool(*c.FileSettings.EnableFileAttachments) - props["EnableMobileFileUpload"] = strconv.FormatBool(*c.FileSettings.EnableMobileUpload) - props["EnableMobileFileDownload"] = strconv.FormatBool(*c.FileSettings.EnableMobileDownload) props["EnablePublicLink"] = strconv.FormatBool(c.FileSettings.EnablePublicLink) props["WebsocketPort"] = fmt.Sprintf("%v", *c.ServiceSettings.WebsocketPort) @@ -443,8 +441,55 @@ func GenerateClientConfig(c *model.Config, diagnosticId string, license *model.L hasImageProxy := c.ServiceSettings.ImageProxyType != nil && *c.ServiceSettings.ImageProxyType != "" && c.ServiceSettings.ImageProxyURL != nil && *c.ServiceSettings.ImageProxyURL != "" props["HasImageProxy"] = strconv.FormatBool(hasImageProxy) + // Set default values for all options that require a license. + props["ExperimentalTownSquareIsReadOnly"] = "false" + props["ExperimentalEnableAuthenticationTransfer"] = "true" + props["EnableCustomBrand"] = "false" + props["CustomBrandText"] = "" + props["CustomDescriptionText"] = "" + props["EnableLdap"] = "false" + props["LdapLoginFieldName"] = "" + props["LdapNicknameAttributeSet"] = "false" + props["LdapFirstNameAttributeSet"] = "false" + props["LdapLastNameAttributeSet"] = "false" + props["LdapLoginButtonColor"] = "" + props["LdapLoginButtonBorderColor"] = "" + props["LdapLoginButtonTextColor"] = "" + props["EnableMultifactorAuthentication"] = "false" + props["EnforceMultifactorAuthentication"] = "false" + props["EnableCompliance"] = "false" + props["EnableMobileFileDownload"] = "true" + props["EnableMobileFileUpload"] = "true" + props["EnableSaml"] = "false" + props["SamlLoginButtonText"] = "" + props["SamlFirstNameAttributeSet"] = "false" + props["SamlLastNameAttributeSet"] = "false" + props["SamlNicknameAttributeSet"] = "false" + props["SamlLoginButtonColor"] = "" + props["SamlLoginButtonBorderColor"] = "" + props["SamlLoginButtonTextColor"] = "" + props["EnableCluster"] = "false" + props["EnableMetrics"] = "false" + props["EnableSignUpWithGoogle"] = "false" + props["EnableSignUpWithOffice365"] = "false" + props["PasswordMinimumLength"] = "0" + props["PasswordRequireLowercase"] = "false" + props["PasswordRequireUppercase"] = "false" + props["PasswordRequireNumber"] = "false" + props["PasswordRequireSymbol"] = "false" + props["EnableBanner"] = "false" + props["BannerText"] = "" + props["BannerColor"] = "" + props["BannerTextColor"] = "" + props["AllowBannerDismissal"] = "false" props["EnableThemeSelection"] = "true" + props["DefaultTheme"] = "" props["AllowCustomThemes"] = "true" + props["AllowedThemes"] = "" + props["DataRetentionEnableMessageDeletion"] = "false" + props["DataRetentionMessageRetentionDays"] = "0" + props["DataRetentionEnableFileDeletion"] = "false" + props["DataRetentionFileRetentionDays"] = "0" if license != nil { props["ExperimentalTownSquareIsReadOnly"] = strconv.FormatBool(*c.TeamSettings.ExperimentalTownSquareIsReadOnly) @@ -474,6 +519,8 @@ func GenerateClientConfig(c *model.Config, diagnosticId string, license *model.L if *license.Features.Compliance { props["EnableCompliance"] = strconv.FormatBool(*c.ComplianceSettings.Enable) + props["EnableMobileFileDownload"] = strconv.FormatBool(*c.FileSettings.EnableMobileDownload) + props["EnableMobileFileUpload"] = strconv.FormatBool(*c.FileSettings.EnableMobileUpload) } if *license.Features.SAML { diff --git a/utils/config_test.go b/utils/config_test.go index 6d6181a3d..f816e2ee8 100644 --- a/utils/config_test.go +++ b/utils/config_test.go @@ -12,6 +12,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "github.com/mattermost/mattermost-server/model" ) func TestConfig(t *testing.T) { @@ -202,14 +204,97 @@ func TestValidateLocales(t *testing.T) { } func TestGetClientConfig(t *testing.T) { - TranslationsPreInit() - cfg, _, err := LoadConfig("config.json") - require.Nil(t, err) + t.Parallel() + testCases := []struct { + description string + config *model.Config + diagnosticId string + license *model.License + expectedFields map[string]string + }{ + { + "unlicensed", + &model.Config{ + EmailSettings: model.EmailSettings{ + EmailNotificationContentsType: sToP(model.EMAIL_NOTIFICATION_CONTENTS_FULL), + }, + ThemeSettings: model.ThemeSettings{ + // Ignored, since not licensed. + AllowCustomThemes: bToP(false), + }, + }, + "", + nil, + map[string]string{ + "DiagnosticId": "", + "EmailNotificationContentsType": "full", + "AllowCustomThemes": "true", + }, + }, + { + "licensed, but not for theme management", + &model.Config{ + EmailSettings: model.EmailSettings{ + EmailNotificationContentsType: sToP(model.EMAIL_NOTIFICATION_CONTENTS_FULL), + }, + ThemeSettings: model.ThemeSettings{ + // Ignored, since not licensed. + AllowCustomThemes: bToP(false), + }, + }, + "tag1", + &model.License{ + Features: &model.Features{ + ThemeManagement: bToP(false), + }, + }, + map[string]string{ + "DiagnosticId": "tag1", + "EmailNotificationContentsType": "full", + "AllowCustomThemes": "true", + }, + }, + { + "licensed for theme management", + &model.Config{ + EmailSettings: model.EmailSettings{ + EmailNotificationContentsType: sToP(model.EMAIL_NOTIFICATION_CONTENTS_FULL), + }, + ThemeSettings: model.ThemeSettings{ + AllowCustomThemes: bToP(false), + }, + }, + "tag2", + &model.License{ + Features: &model.Features{ + ThemeManagement: bToP(true), + }, + }, + map[string]string{ + "DiagnosticId": "tag2", + "EmailNotificationContentsType": "full", + "AllowCustomThemes": "false", + }, + }, + } - configMap := GenerateClientConfig(cfg, "", nil) - if configMap["EmailNotificationContentsType"] != *cfg.EmailSettings.EmailNotificationContentsType { - t.Fatal("EmailSettings.EmailNotificationContentsType not exposed to client config") + for _, testCase := range testCases { + testCase := testCase + t.Run(testCase.description, func(t *testing.T) { + t.Parallel() + + testCase.config.SetDefaults() + if testCase.license != nil { + testCase.license.Features.SetDefaults() + } + + configMap := GenerateClientConfig(testCase.config, testCase.diagnosticId, testCase.license) + for expectedField, expectedValue := range testCase.expectedFields { + assert.Equal(t, expectedValue, configMap[expectedField]) + } + }) } + } func TestReadConfig(t *testing.T) { @@ -222,3 +307,11 @@ func TestReadConfig(t *testing.T) { assert.Equal(t, "http://foo.bar", *config.ServiceSettings.SiteURL) } + +func sToP(s string) *string { + return &s +} + +func bToP(b bool) *bool { + return &b +} -- cgit v1.2.3-1-g7c22