diff options
Diffstat (limited to 'vendor/github.com/mattermost/platform/model/user.go')
-rw-r--r-- | vendor/github.com/mattermost/platform/model/user.go | 157 |
1 files changed, 88 insertions, 69 deletions
diff --git a/vendor/github.com/mattermost/platform/model/user.go b/vendor/github.com/mattermost/platform/model/user.go index d62ccccd..ab4b21e2 100644 --- a/vendor/github.com/mattermost/platform/model/user.go +++ b/vendor/github.com/mattermost/platform/model/user.go @@ -1,4 +1,4 @@ -// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See License.txt for license information. package model @@ -10,33 +10,42 @@ import ( "net/http" "regexp" "strings" - "unicode" "unicode/utf8" "golang.org/x/crypto/bcrypt" ) const ( - USER_NOTIFY_ALL = "all" - USER_NOTIFY_MENTION = "mention" - USER_NOTIFY_NONE = "none" - DESKTOP_NOTIFY_PROP = "desktop" - MARK_UNREAD_NOTIFY_PROP = "mark_unread" - PUSH_NOTIFY_PROP = "push" - EMAIL_NOTIFY_PROP = "email" - - DEFAULT_LOCALE = "en" - USER_AUTH_SERVICE_EMAIL = "email" - USER_AUTH_SERVICE_USERNAME = "username" + ME = "me" + USER_NOTIFY_ALL = "all" + USER_NOTIFY_MENTION = "mention" + USER_NOTIFY_NONE = "none" + DESKTOP_NOTIFY_PROP = "desktop" + DESKTOP_SOUND_NOTIFY_PROP = "desktop_sound" + DESKTOP_DURATION_NOTIFY_PROP = "desktop_duration" + MARK_UNREAD_NOTIFY_PROP = "mark_unread" + PUSH_NOTIFY_PROP = "push" + PUSH_STATUS_NOTIFY_PROP = "push_status" + EMAIL_NOTIFY_PROP = "email" + CHANNEL_MENTIONS_NOTIFY_PROP = "channel" + COMMENTS_NOTIFY_PROP = "comments" + MENTION_KEYS_NOTIFY_PROP = "mention_keys" + COMMENTS_NOTIFY_NEVER = "never" + COMMENTS_NOTIFY_ROOT = "root" + COMMENTS_NOTIFY_ANY = "any" + + DEFAULT_LOCALE = "en" + USER_AUTH_SERVICE_EMAIL = "email" USER_EMAIL_MAX_LENGTH = 128 USER_NICKNAME_MAX_RUNES = 64 - USER_POSITION_MAX_RUNES = 35 + USER_POSITION_MAX_RUNES = 64 USER_FIRST_NAME_MAX_RUNES = 64 USER_LAST_NAME_MAX_RUNES = 64 USER_AUTH_DATA_MAX_LENGTH = 128 USER_NAME_MAX_LENGTH = 64 USER_NAME_MIN_LENGTH = 1 + USER_PASSWORD_MAX_LENGTH = 72 ) type User struct { @@ -68,15 +77,15 @@ type User struct { } type UserPatch struct { - Username *string `json:"username"` - Nickname *string `json:"nickname"` - FirstName *string `json:"first_name"` - LastName *string `json:"last_name"` - Position *string `json:"position"` - Email *string `json:"email"` - Props *StringMap `json:"props,omitempty"` - NotifyProps *StringMap `json:"notify_props,omitempty"` - Locale *string `json:"locale"` + Username *string `json:"username"` + Nickname *string `json:"nickname"` + FirstName *string `json:"first_name"` + LastName *string `json:"last_name"` + Position *string `json:"position"` + Email *string `json:"email"` + Props StringMap `json:"props,omitempty"` + NotifyProps StringMap `json:"notify_props,omitempty"` + Locale *string `json:"locale"` } // IsValid validates the user and returns an error if it isn't configured @@ -84,56 +93,69 @@ type UserPatch struct { func (u *User) IsValid() *AppError { if len(u.Id) != 26 { - return NewAppError("User.IsValid", "model.user.is_valid.id.app_error", nil, "", http.StatusBadRequest) + return InvalidUserError("id", "") } if u.CreateAt == 0 { - return NewAppError("User.IsValid", "model.user.is_valid.create_at.app_error", nil, "user_id="+u.Id, http.StatusBadRequest) + return InvalidUserError("create_at", u.Id) } if u.UpdateAt == 0 { - return NewAppError("User.IsValid", "model.user.is_valid.update_at.app_error", nil, "user_id="+u.Id, http.StatusBadRequest) + return InvalidUserError("update_at", u.Id) } if !IsValidUsername(u.Username) { - return NewAppError("User.IsValid", "model.user.is_valid.username.app_error", nil, "user_id="+u.Id, http.StatusBadRequest) + return InvalidUserError("username", u.Id) } if len(u.Email) > USER_EMAIL_MAX_LENGTH || len(u.Email) == 0 { - return NewAppError("User.IsValid", "model.user.is_valid.email.app_error", nil, "user_id="+u.Id, http.StatusBadRequest) + return InvalidUserError("email", u.Id) } if utf8.RuneCountInString(u.Nickname) > USER_NICKNAME_MAX_RUNES { - return NewAppError("User.IsValid", "model.user.is_valid.nickname.app_error", nil, "user_id="+u.Id, http.StatusBadRequest) + return InvalidUserError("nickname", u.Id) } if utf8.RuneCountInString(u.Position) > USER_POSITION_MAX_RUNES { - return NewAppError("User.IsValid", "model.user.is_valid.position.app_error", nil, "user_id="+u.Id, http.StatusBadRequest) + return InvalidUserError("position", u.Id) } if utf8.RuneCountInString(u.FirstName) > USER_FIRST_NAME_MAX_RUNES { - return NewAppError("User.IsValid", "model.user.is_valid.first_name.app_error", nil, "user_id="+u.Id, http.StatusBadRequest) + return InvalidUserError("first_name", u.Id) } if utf8.RuneCountInString(u.LastName) > USER_LAST_NAME_MAX_RUNES { - return NewAppError("User.IsValid", "model.user.is_valid.last_name.app_error", nil, "user_id="+u.Id, http.StatusBadRequest) + return InvalidUserError("last_name", u.Id) } if u.AuthData != nil && len(*u.AuthData) > USER_AUTH_DATA_MAX_LENGTH { - return NewAppError("User.IsValid", "model.user.is_valid.auth_data.app_error", nil, "user_id="+u.Id, http.StatusBadRequest) + return InvalidUserError("auth_data", u.Id) } if u.AuthData != nil && len(*u.AuthData) > 0 && len(u.AuthService) == 0 { - return NewAppError("User.IsValid", "model.user.is_valid.auth_data_type.app_error", nil, "user_id="+u.Id, http.StatusBadRequest) + return InvalidUserError("auth_data_type", u.Id) } if len(u.Password) > 0 && u.AuthData != nil && len(*u.AuthData) > 0 { - return NewAppError("User.IsValid", "model.user.is_valid.auth_data_pwd.app_error", nil, "user_id="+u.Id, http.StatusBadRequest) + return InvalidUserError("auth_data_pwd", u.Id) + } + + if len(u.Password) > USER_PASSWORD_MAX_LENGTH { + return InvalidUserError("password_limit", u.Id) } return nil } +func InvalidUserError(fieldName string, userId string) *AppError { + id := fmt.Sprintf("model.user.is_valid.%s.app_error", fieldName) + details := "" + if userId != "" { + details = "user_id=" + userId + } + return NewAppError("User.IsValid", id, nil, details, http.StatusBadRequest) +} + // 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. @@ -143,7 +165,7 @@ func (u *User) PreSave() { } if u.Username == "" { - u.Username = "n" + NewId() + u.Username = NewId() } if u.AuthData != nil && *u.AuthData == "" { @@ -259,11 +281,11 @@ func (u *User) Patch(patch *UserPatch) { } if patch.Props != nil { - u.Props = *patch.Props + u.Props = patch.Props } if patch.NotifyProps != nil { - u.NotifyProps = *patch.NotifyProps + u.NotifyProps = patch.NotifyProps } if patch.Locale != nil { @@ -327,7 +349,6 @@ func (u *User) ClearNonProfileFields() { u.Props = StringMap{} u.NotifyProps = StringMap{} u.LastPasswordUpdate = 0 - u.LastPictureUpdate = 0 u.FailedAttempts = 0 } @@ -371,26 +392,16 @@ func (u *User) GetFullName() string { } } -func (u *User) GetDisplayName() string { - if u.Nickname != "" { - return u.Nickname - } else if fullName := u.GetFullName(); fullName != "" { - return fullName - } else { - return u.Username - } -} - -func (u *User) GetDisplayNameForPreference(nameFormat string) string { +func (u *User) GetDisplayName(nameFormat string) string { displayName := u.Username - if nameFormat == PREFERENCE_VALUE_DISPLAY_NAME_NICKNAME { + if nameFormat == SHOW_NICKNAME_FULLNAME { if u.Nickname != "" { displayName = u.Nickname } else if fullName := u.GetFullName(); fullName != "" { displayName = fullName } - } else if nameFormat == PREFERENCE_VALUE_DISPLAY_NAME_FULL { + } else if nameFormat == SHOW_FULLNAME { if fullName := u.GetFullName(); fullName != "" { displayName = fullName } @@ -445,31 +456,25 @@ func IsInRole(userRoles string, inRole string) bool { if r == inRole { return true } - } return false } func (u *User) IsSSOUser() bool { - if u.AuthService != "" && u.AuthService != USER_AUTH_SERVICE_EMAIL { - return true - } - return false + return u.AuthService != "" && u.AuthService != USER_AUTH_SERVICE_EMAIL } func (u *User) IsOAuthUser() bool { - if u.AuthService == USER_AUTH_SERVICE_GITLAB { - return true - } - return false + return u.AuthService == USER_AUTH_SERVICE_GITLAB } func (u *User) IsLDAPUser() bool { - if u.AuthService == USER_AUTH_SERVICE_LDAP { - return true - } - return false + return u.AuthService == USER_AUTH_SERVICE_LDAP +} + +func (u *User) IsSAMLUser() bool { + return u.AuthService == USER_AUTH_SERVICE_SAML } // UserFromJson will decode the input and return a User @@ -573,10 +578,6 @@ func IsValidUsername(s string) bool { return false } - if !unicode.IsLetter(rune(s[0])) { - return false - } - for _, restrictedUsername := range restrictedUsernames { if s == restrictedUsername { return false @@ -612,3 +613,21 @@ func CleanUsername(s string) string { return s } + +func IsValidUserNotifyLevel(notifyLevel string) bool { + return notifyLevel == CHANNEL_NOTIFY_ALL || + notifyLevel == CHANNEL_NOTIFY_MENTION || + notifyLevel == CHANNEL_NOTIFY_NONE +} + +func IsValidPushStatusNotifyLevel(notifyLevel string) bool { + return notifyLevel == STATUS_ONLINE || + notifyLevel == STATUS_AWAY || + notifyLevel == STATUS_OFFLINE +} + +func IsValidCommentsNotifyLevel(notifyLevel string) bool { + return notifyLevel == COMMENTS_NOTIFY_ANY || + notifyLevel == COMMENTS_NOTIFY_ROOT || + notifyLevel == COMMENTS_NOTIFY_NEVER +} |