diff options
author | Christopher Speller <crspeller@gmail.com> | 2015-08-28 09:14:37 -0400 |
---|---|---|
committer | Christopher Speller <crspeller@gmail.com> | 2015-08-28 09:14:37 -0400 |
commit | 75af5d4536cc414d171c2fe6dca78e455eb18b37 (patch) | |
tree | 4e00cf97b5f4f570b1901093bfa4e208749932a0 /model/user.go | |
parent | d107b392a6309a41eac6cd7d07d720a21968eb56 (diff) | |
parent | f5fec3a157e6c9146a0c4e28dd5f70e6c066affd (diff) | |
download | chat-75af5d4536cc414d171c2fe6dca78e455eb18b37.tar.gz chat-75af5d4536cc414d171c2fe6dca78e455eb18b37.tar.bz2 chat-75af5d4536cc414d171c2fe6dca78e455eb18b37.zip |
Merge pull request #496 from mattermost/mm-2015
MM-2015 Added the ability to create a team with SSO services and added the ability to turn off email sign up.
Diffstat (limited to 'model/user.go')
-rw-r--r-- | model/user.go | 60 |
1 files changed, 48 insertions, 12 deletions
diff --git a/model/user.go b/model/user.go index ebefa4762..7c53593d2 100644 --- a/model/user.go +++ b/model/user.go @@ -6,6 +6,7 @@ package model import ( "code.google.com/p/go.crypto/bcrypt" "encoding/json" + "fmt" "io" "regexp" "strings" @@ -72,13 +73,7 @@ func (u *User) IsValid() *AppError { return NewAppError("User.IsValid", "Invalid team id", "") } - if len(u.Username) == 0 || len(u.Username) > 64 { - return NewAppError("User.IsValid", "Invalid username", "user_id="+u.Id) - } - - validChars, _ := regexp.Compile("^[a-z0-9\\.\\-\\_]+$") - - if !validChars.MatchString(u.Username) { + if !IsValidUsername(u.Username) { return NewAppError("User.IsValid", "Invalid username", "user_id="+u.Id) } @@ -332,17 +327,58 @@ func ComparePassword(hash string, password string) bool { func IsUsernameValid(username string) bool { - var restrictedUsernames = []string{ - BOT_USERNAME, - "all", - "channel", + return true +} + +var validUsernameChars = regexp.MustCompile(`^[a-z0-9\.\-_]+$`) + +var restrictedUsernames = []string{ + BOT_USERNAME, + "all", + "channel", +} + +func IsValidUsername(s string) bool { + if len(s) == 0 || len(s) > 64 { + return false + } + + if !validUsernameChars.MatchString(s) { + return false } for _, restrictedUsername := range restrictedUsernames { - if username == restrictedUsername { + if s == restrictedUsername { return false } } return true } + +func CleanUsername(s string) string { + s = strings.ToLower(strings.Replace(s, " ", "-", -1)) + + for _, value := range reservedName { + if s == value { + s = strings.Replace(s, value, "", -1) + } + } + + s = strings.TrimSpace(s) + + for _, c := range s { + char := fmt.Sprintf("%c", c) + if !validUsernameChars.MatchString(char) { + s = strings.Replace(s, char, "-", -1) + } + } + + s = strings.Trim(s, "-") + + if !IsValidUsername(s) { + s = "a" + NewId() + } + + return s +} |