From a7854f1b979a4b0c0588e483e36ebc9bd6d75940 Mon Sep 17 00:00:00 2001 From: Shota Gvinepadze Date: Wed, 12 Feb 2020 17:51:45 +0400 Subject: [PATCH] [MM-22037] Enable uppercase letters in the email (#13794) * Enable uppercase letters in the email * Lowercase email on every input * Remove invalid test --- api4/team.go | 9 +++++++++ api4/user.go | 5 ++++- app/import_functions.go | 1 + app/slackimport.go | 1 + app/user.go | 1 + cmd/mattermost/commands/team.go | 2 ++ cmd/mattermost/commands/user.go | 5 ++++- model/gitlab/gitlab.go | 1 + model/utils_test.go | 10 ---------- web/context.go | 4 ++-- 10 files changed, 25 insertions(+), 14 deletions(-) diff --git a/api4/team.go b/api4/team.go index d970c21ce5..4a72bd10af 100644 --- a/api4/team.go +++ b/api4/team.go @@ -79,6 +79,7 @@ func createTeam(c *Context, w http.ResponseWriter, r *http.Request) { c.SetInvalidParam("team") return } + team.Email = strings.ToLower(team.Email) if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_CREATE_TEAM) { c.Err = model.NewAppError("createTeam", "api.team.is_team_creation_allowed.disabled.app_error", nil, "", http.StatusForbidden) @@ -151,6 +152,7 @@ func updateTeam(c *Context, w http.ResponseWriter, r *http.Request) { c.SetInvalidParam("team") return } + team.Email = strings.ToLower(team.Email) // The team being updated in the payload must be the same one as indicated in the URL. if team.Id != c.Params.TeamId { @@ -969,6 +971,9 @@ func inviteUsersToTeam(c *Context, w http.ResponseWriter, r *http.Request) { } emailList := model.ArrayFromJson(r.Body) + for i := range emailList { + emailList[i] = strings.ToLower(emailList[i]) + } if len(emailList) == 0 { c.SetInvalidParam("user_email") @@ -1016,10 +1021,14 @@ func inviteGuestsToChannels(c *Context, w http.ResponseWriter, r *http.Request) } guestsInvite := model.GuestsInviteFromJson(r.Body) + for i, email := range guestsInvite.Emails { + guestsInvite.Emails[i] = strings.ToLower(email) + } if err := guestsInvite.IsValid(); err != nil { c.Err = err return } + if graceful { invitesWithError, err := c.App.InviteGuestsToChannelsGracefully(c.Params.TeamId, guestsInvite, c.App.Session.UserId) if err != nil { diff --git a/api4/user.go b/api4/user.go index d20bb1208f..78a53f6511 100644 --- a/api4/user.go +++ b/api4/user.go @@ -10,6 +10,7 @@ import ( "io/ioutil" "net/http" "strconv" + "strings" "time" "github.com/mattermost/mattermost-server/v5/app" @@ -244,7 +245,7 @@ func getUserByUsername(c *Context, w http.ResponseWriter, r *http.Request) { } func getUserByEmail(c *Context, w http.ResponseWriter, r *http.Request) { - c.RequireEmail() + c.SanitizeEmail() if c.Err != nil { return } @@ -1264,6 +1265,7 @@ func sendPasswordReset(c *Context, w http.ResponseWriter, r *http.Request) { props := model.MapFromJson(r.Body) email := props["email"] + email = strings.ToLower(email) if len(email) == 0 { c.SetInvalidParam("email") return @@ -1625,6 +1627,7 @@ func sendVerificationEmail(c *Context, w http.ResponseWriter, r *http.Request) { props := model.MapFromJson(r.Body) email := props["email"] + email = strings.ToLower(email) if len(email) == 0 { c.SetInvalidParam("email") return diff --git a/app/import_functions.go b/app/import_functions.go index 8c80ea1958..71c798b1fe 100644 --- a/app/import_functions.go +++ b/app/import_functions.go @@ -309,6 +309,7 @@ func (a *App) importUser(data *UserImportData, dryRun bool) *model.AppError { hasUserChanged = true hasUserEmailVerifiedChanged = true // Changing the email resets email verified to false by default. user.Email = *data.Email + user.Email = strings.ToLower(user.Email) } var password string diff --git a/app/slackimport.go b/app/slackimport.go index e2e8fe58dd..a8fb13d264 100644 --- a/app/slackimport.go +++ b/app/slackimport.go @@ -185,6 +185,7 @@ func (a *App) SlackAddUsers(teamId string, slackusers []SlackUser, importerLog * continue } + email = strings.ToLower(email) newUser := model.User{ Username: sUser.Username, FirstName: firstName, diff --git a/app/user.go b/app/user.go index 7df8c7deeb..b483e3705d 100644 --- a/app/user.go +++ b/app/user.go @@ -1611,6 +1611,7 @@ func (a *App) VerifyEmailFromToken(userSuppliedTokenString string) *model.AppErr return err } + tokenData.Email = strings.ToLower(tokenData.Email) if err := a.VerifyUserEmail(tokenData.UserId, tokenData.Email); err != nil { return err } diff --git a/cmd/mattermost/commands/team.go b/cmd/mattermost/commands/team.go index 739d4a1e06..d954713d08 100644 --- a/cmd/mattermost/commands/team.go +++ b/cmd/mattermost/commands/team.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "sort" + "strings" "github.com/mattermost/mattermost-server/v5/app" "github.com/mattermost/mattermost-server/v5/model" @@ -153,6 +154,7 @@ func createTeamCmdF(command *cobra.Command, args []string) error { return errors.New("Display Name is required") } email, _ := command.Flags().GetString("email") + email = strings.ToLower(email) useprivate, _ := command.Flags().GetBool("private") teamType := model.TEAM_OPEN diff --git a/cmd/mattermost/commands/user.go b/cmd/mattermost/commands/user.go index 88df41aa5f..899d995ce9 100644 --- a/cmd/mattermost/commands/user.go +++ b/cmd/mattermost/commands/user.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "io/ioutil" + "strings" "github.com/mattermost/mattermost-server/v5/app" "github.com/mattermost/mattermost-server/v5/model" @@ -340,6 +341,7 @@ func userCreateCmdF(command *cobra.Command, args []string) error { if erre != nil || email == "" { return errors.New("Email is required") } + email = strings.ToLower((email)) password, errp := command.Flags().GetString("password") if errp != nil || password == "" { return errors.New("Password is required") @@ -572,6 +574,7 @@ func userInviteCmdF(command *cobra.Command, args []string) error { } email := args[0] + email = strings.ToLower(email) if !model.IsValidEmail(email) { return errors.New("Invalid email") } @@ -640,7 +643,7 @@ func updateUserEmailCmdF(command *cobra.Command, args []string) error { } newEmail := args[1] - + newEmail = strings.ToLower(newEmail) if !model.IsValidEmail(newEmail) { return errors.New("Invalid email: '" + newEmail + "'") } diff --git a/model/gitlab/gitlab.go b/model/gitlab/gitlab.go index c9c88a9eca..9906a2ae4e 100644 --- a/model/gitlab/gitlab.go +++ b/model/gitlab/gitlab.go @@ -47,6 +47,7 @@ func userFromGitLabUser(glu *GitLabUser) *model.User { user.FirstName = glu.Name } user.Email = glu.Email + user.Email = strings.ToLower(user.Email) userId := glu.getAuthData() user.AuthData = &userId user.AuthService = model.USER_AUTH_SERVICE_GITLAB diff --git a/model/utils_test.go b/model/utils_test.go index 8c4c70cd97..5457688eac 100644 --- a/model/utils_test.go +++ b/model/utils_test.go @@ -184,16 +184,6 @@ func TestIsValidEmail(t *testing.T) { } } -func TestValidLower(t *testing.T) { - if !IsLower("corey+test@hulen.com") { - t.Error("should be valid") - } - - if IsLower("Corey+test@hulen.com") { - t.Error("should be invalid") - } -} - func TestEtag(t *testing.T) { etag := Etag("hello", 24) require.NotEqual(t, "", etag) diff --git a/web/context.go b/web/context.go index 7a2ea1d8ff..39dc8dbe24 100644 --- a/web/context.go +++ b/web/context.go @@ -393,11 +393,11 @@ func (c *Context) RequireChannelName() *Context { return c } -func (c *Context) RequireEmail() *Context { +func (c *Context) SanitizeEmail() *Context { if c.Err != nil { return c } - + c.Params.Email = strings.ToLower(c.Params.Email) if !model.IsValidEmail(c.Params.Email) { c.SetInvalidUrlParam("email") }