summaryrefslogtreecommitdiffstats
path: root/model/user.go
diff options
context:
space:
mode:
authorJesse Hallam <jesse.hallam@gmail.com>2018-02-05 14:56:01 -0500
committerGitHub <noreply@github.com>2018-02-05 14:56:01 -0500
commitd5d1834c041d5c676a556965dc98299eef750c9c (patch)
tree9af061953f2fcf47338995c9d6d627364d1d27ea /model/user.go
parentaaccb1226e6051829c295ad192ed89a00dbd4c98 (diff)
downloadchat-d5d1834c041d5c676a556965dc98299eef750c9c.tar.gz
chat-d5d1834c041d5c676a556965dc98299eef750c9c.tar.bz2
chat-d5d1834c041d5c676a556965dc98299eef750c9c.zip
ABC-173: introduce Normalize(Username|Email) (#8183)
This centralizes the source of truth on the rules for username / email processing instead of scattering `strings.ToLower` invocations.
Diffstat (limited to 'model/user.go')
-rw-r--r--model/user.go18
1 files changed, 13 insertions, 5 deletions
diff --git a/model/user.go b/model/user.go
index 901e8d7ad..1e1d49f7d 100644
--- a/model/user.go
+++ b/model/user.go
@@ -162,6 +162,14 @@ func InvalidUserError(fieldName string, userId string) *AppError {
return NewAppError("User.IsValid", id, nil, details, http.StatusBadRequest)
}
+func NormalizeUsername(username string) string {
+ return strings.ToLower(username)
+}
+
+func NormalizeEmail(email string) string {
+ return strings.ToLower(email)
+}
+
// PreSave will set the Id and Username if missing. It will also fill
// in the CreateAt, UpdateAt times. It will also hash the password. It should
// be run before saving the user to the db.
@@ -178,8 +186,8 @@ func (u *User) PreSave() {
u.AuthData = nil
}
- u.Username = strings.ToLower(u.Username)
- u.Email = strings.ToLower(u.Email)
+ u.Username = NormalizeUsername(u.Username)
+ u.Email = NormalizeEmail(u.Email)
u.CreateAt = GetMillis()
u.UpdateAt = u.CreateAt
@@ -207,8 +215,8 @@ func (u *User) PreSave() {
// PreUpdate should be run before updating the user in the db.
func (u *User) PreUpdate() {
- u.Username = strings.ToLower(u.Username)
- u.Email = strings.ToLower(u.Email)
+ u.Username = NormalizeUsername(u.Username)
+ u.Email = NormalizeEmail(u.Email)
u.UpdateAt = GetMillis()
if u.AuthData != nil && *u.AuthData == "" {
@@ -563,7 +571,7 @@ func IsValidUsername(s string) bool {
}
func CleanUsername(s string) string {
- s = strings.ToLower(strings.Replace(s, " ", "-", -1))
+ s = NormalizeUsername(strings.Replace(s, " ", "-", -1))
for _, value := range reservedName {
if s == value {