2017-08-16 23:37:37 +02:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
2016-04-10 23:39:38 +02:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-03-25 21:04:10 +01:00
|
|
|
"net/http"
|
2016-04-10 23:39:38 +02:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"unicode/utf8"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-08-16 23:37:37 +02:00
|
|
|
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"
|
2017-03-25 21:04:10 +01:00
|
|
|
|
|
|
|
USER_EMAIL_MAX_LENGTH = 128
|
|
|
|
USER_NICKNAME_MAX_RUNES = 64
|
2017-08-16 23:37:37 +02:00
|
|
|
USER_POSITION_MAX_RUNES = 64
|
2017-03-25 21:04:10 +01:00
|
|
|
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
|
2017-08-16 23:37:37 +02:00
|
|
|
USER_PASSWORD_MAX_LENGTH = 72
|
2016-04-10 23:39:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
CreateAt int64 `json:"create_at,omitempty"`
|
|
|
|
UpdateAt int64 `json:"update_at,omitempty"`
|
|
|
|
DeleteAt int64 `json:"delete_at"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password,omitempty"`
|
2016-05-15 23:02:30 +02:00
|
|
|
AuthData *string `json:"auth_data,omitempty"`
|
2016-04-10 23:39:38 +02:00
|
|
|
AuthService string `json:"auth_service"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
EmailVerified bool `json:"email_verified,omitempty"`
|
|
|
|
Nickname string `json:"nickname"`
|
|
|
|
FirstName string `json:"first_name"`
|
|
|
|
LastName string `json:"last_name"`
|
2017-01-16 23:59:50 +01:00
|
|
|
Position string `json:"position"`
|
2016-04-10 23:39:38 +02:00
|
|
|
Roles string `json:"roles"`
|
|
|
|
AllowMarketing bool `json:"allow_marketing,omitempty"`
|
|
|
|
Props StringMap `json:"props,omitempty"`
|
|
|
|
NotifyProps StringMap `json:"notify_props,omitempty"`
|
|
|
|
LastPasswordUpdate int64 `json:"last_password_update,omitempty"`
|
|
|
|
LastPictureUpdate int64 `json:"last_picture_update,omitempty"`
|
|
|
|
FailedAttempts int `json:"failed_attempts,omitempty"`
|
|
|
|
Locale string `json:"locale"`
|
2016-05-15 23:02:30 +02:00
|
|
|
MfaActive bool `json:"mfa_active,omitempty"`
|
|
|
|
MfaSecret string `json:"mfa_secret,omitempty"`
|
2016-09-17 15:19:18 +02:00
|
|
|
LastActivityAt int64 `db:"-" json:"last_activity_at,omitempty"`
|
2016-04-10 23:39:38 +02:00
|
|
|
}
|
|
|
|
|
2017-03-25 21:04:10 +01:00
|
|
|
type UserPatch struct {
|
2017-08-16 23:37:37 +02:00
|
|
|
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"`
|
2017-03-25 21:04:10 +01:00
|
|
|
}
|
|
|
|
|
2018-02-09 00:11:04 +01:00
|
|
|
type UserAuth struct {
|
|
|
|
Password string `json:"password,omitempty"`
|
|
|
|
AuthData *string `json:"auth_data,omitempty"`
|
|
|
|
AuthService string `json:"auth_service,omitempty"`
|
|
|
|
}
|
|
|
|
|
2016-04-10 23:39:38 +02:00
|
|
|
// IsValid validates the user and returns an error if it isn't configured
|
|
|
|
// correctly.
|
|
|
|
func (u *User) IsValid() *AppError {
|
|
|
|
|
|
|
|
if len(u.Id) != 26 {
|
2017-08-16 23:37:37 +02:00
|
|
|
return InvalidUserError("id", "")
|
2016-04-10 23:39:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if u.CreateAt == 0 {
|
2017-08-16 23:37:37 +02:00
|
|
|
return InvalidUserError("create_at", u.Id)
|
2016-04-10 23:39:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if u.UpdateAt == 0 {
|
2017-08-16 23:37:37 +02:00
|
|
|
return InvalidUserError("update_at", u.Id)
|
2016-04-10 23:39:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !IsValidUsername(u.Username) {
|
2017-08-16 23:37:37 +02:00
|
|
|
return InvalidUserError("username", u.Id)
|
2016-04-10 23:39:38 +02:00
|
|
|
}
|
|
|
|
|
2017-03-25 21:04:10 +01:00
|
|
|
if len(u.Email) > USER_EMAIL_MAX_LENGTH || len(u.Email) == 0 {
|
2017-08-16 23:37:37 +02:00
|
|
|
return InvalidUserError("email", u.Id)
|
2016-04-10 23:39:38 +02:00
|
|
|
}
|
|
|
|
|
2017-03-25 21:04:10 +01:00
|
|
|
if utf8.RuneCountInString(u.Nickname) > USER_NICKNAME_MAX_RUNES {
|
2017-08-16 23:37:37 +02:00
|
|
|
return InvalidUserError("nickname", u.Id)
|
2016-04-10 23:39:38 +02:00
|
|
|
}
|
|
|
|
|
2017-03-25 21:04:10 +01:00
|
|
|
if utf8.RuneCountInString(u.Position) > USER_POSITION_MAX_RUNES {
|
2017-08-16 23:37:37 +02:00
|
|
|
return InvalidUserError("position", u.Id)
|
2017-01-16 23:59:50 +01:00
|
|
|
}
|
|
|
|
|
2017-03-25 21:04:10 +01:00
|
|
|
if utf8.RuneCountInString(u.FirstName) > USER_FIRST_NAME_MAX_RUNES {
|
2017-08-16 23:37:37 +02:00
|
|
|
return InvalidUserError("first_name", u.Id)
|
2016-04-10 23:39:38 +02:00
|
|
|
}
|
|
|
|
|
2017-03-25 21:04:10 +01:00
|
|
|
if utf8.RuneCountInString(u.LastName) > USER_LAST_NAME_MAX_RUNES {
|
2017-08-16 23:37:37 +02:00
|
|
|
return InvalidUserError("last_name", u.Id)
|
2016-04-10 23:39:38 +02:00
|
|
|
}
|
|
|
|
|
2017-03-25 21:04:10 +01:00
|
|
|
if u.AuthData != nil && len(*u.AuthData) > USER_AUTH_DATA_MAX_LENGTH {
|
2017-08-16 23:37:37 +02:00
|
|
|
return InvalidUserError("auth_data", u.Id)
|
2016-04-10 23:39:38 +02:00
|
|
|
}
|
|
|
|
|
2016-05-15 23:02:30 +02:00
|
|
|
if u.AuthData != nil && len(*u.AuthData) > 0 && len(u.AuthService) == 0 {
|
2017-08-16 23:37:37 +02:00
|
|
|
return InvalidUserError("auth_data_type", u.Id)
|
2016-04-10 23:39:38 +02:00
|
|
|
}
|
|
|
|
|
2016-05-15 23:02:30 +02:00
|
|
|
if len(u.Password) > 0 && u.AuthData != nil && len(*u.AuthData) > 0 {
|
2017-08-16 23:37:37 +02:00
|
|
|
return InvalidUserError("auth_data_pwd", u.Id)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(u.Password) > USER_PASSWORD_MAX_LENGTH {
|
|
|
|
return InvalidUserError("password_limit", u.Id)
|
2016-04-10 23:39:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-16 23:37:37 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-04-10 23:39:38 +02:00
|
|
|
// 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.
|
|
|
|
func (u *User) PreSave() {
|
|
|
|
if u.Id == "" {
|
|
|
|
u.Id = NewId()
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Username == "" {
|
2017-08-16 23:37:37 +02:00
|
|
|
u.Username = NewId()
|
2016-04-10 23:39:38 +02:00
|
|
|
}
|
|
|
|
|
2016-05-15 23:02:30 +02:00
|
|
|
if u.AuthData != nil && *u.AuthData == "" {
|
|
|
|
u.AuthData = nil
|
|
|
|
}
|
|
|
|
|
2016-04-10 23:39:38 +02:00
|
|
|
u.Username = strings.ToLower(u.Username)
|
|
|
|
u.Email = strings.ToLower(u.Email)
|
|
|
|
|
|
|
|
u.CreateAt = GetMillis()
|
|
|
|
u.UpdateAt = u.CreateAt
|
|
|
|
|
|
|
|
u.LastPasswordUpdate = u.CreateAt
|
|
|
|
|
2016-05-15 23:02:30 +02:00
|
|
|
u.MfaActive = false
|
|
|
|
|
2016-04-10 23:39:38 +02:00
|
|
|
if u.Locale == "" {
|
|
|
|
u.Locale = DEFAULT_LOCALE
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Props == nil {
|
|
|
|
u.Props = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.NotifyProps == nil || len(u.NotifyProps) == 0 {
|
|
|
|
u.SetDefaultNotifications()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(u.Password) > 0 {
|
|
|
|
u.Password = HashPassword(u.Password)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.UpdateAt = GetMillis()
|
|
|
|
|
2016-05-15 23:02:30 +02:00
|
|
|
if u.AuthData != nil && *u.AuthData == "" {
|
|
|
|
u.AuthData = nil
|
|
|
|
}
|
|
|
|
|
2016-04-10 23:39:38 +02:00
|
|
|
if u.NotifyProps == nil || len(u.NotifyProps) == 0 {
|
|
|
|
u.SetDefaultNotifications()
|
|
|
|
} else if _, ok := u.NotifyProps["mention_keys"]; ok {
|
|
|
|
// Remove any blank mention keys
|
|
|
|
splitKeys := strings.Split(u.NotifyProps["mention_keys"], ",")
|
|
|
|
goodKeys := []string{}
|
|
|
|
for _, key := range splitKeys {
|
|
|
|
if len(key) > 0 {
|
|
|
|
goodKeys = append(goodKeys, strings.ToLower(key))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
u.NotifyProps["mention_keys"] = strings.Join(goodKeys, ",")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) SetDefaultNotifications() {
|
|
|
|
u.NotifyProps = make(map[string]string)
|
|
|
|
u.NotifyProps["email"] = "true"
|
2016-06-23 20:28:05 +02:00
|
|
|
u.NotifyProps["push"] = USER_NOTIFY_MENTION
|
2018-02-09 00:11:04 +01:00
|
|
|
u.NotifyProps["desktop"] = USER_NOTIFY_MENTION
|
2016-04-10 23:39:38 +02:00
|
|
|
u.NotifyProps["desktop_sound"] = "true"
|
|
|
|
u.NotifyProps["mention_keys"] = u.Username + ",@" + u.Username
|
|
|
|
u.NotifyProps["channel"] = "true"
|
2018-02-09 00:11:04 +01:00
|
|
|
u.NotifyProps["push_status"] = STATUS_AWAY
|
|
|
|
u.NotifyProps["comments"] = "never"
|
|
|
|
u.NotifyProps["first_name"] = "false"
|
2016-05-15 23:02:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) UpdateMentionKeysFromUsername(oldUsername string) {
|
|
|
|
nonUsernameKeys := []string{}
|
|
|
|
splitKeys := strings.Split(user.NotifyProps["mention_keys"], ",")
|
|
|
|
for _, key := range splitKeys {
|
|
|
|
if key != oldUsername && key != "@"+oldUsername {
|
|
|
|
nonUsernameKeys = append(nonUsernameKeys, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
user.NotifyProps["mention_keys"] = user.Username + ",@" + user.Username
|
|
|
|
if len(nonUsernameKeys) > 0 {
|
|
|
|
user.NotifyProps["mention_keys"] += "," + strings.Join(nonUsernameKeys, ",")
|
2016-04-10 23:39:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-25 21:04:10 +01:00
|
|
|
func (u *User) Patch(patch *UserPatch) {
|
|
|
|
if patch.Username != nil {
|
|
|
|
u.Username = *patch.Username
|
|
|
|
}
|
|
|
|
|
|
|
|
if patch.Nickname != nil {
|
|
|
|
u.Nickname = *patch.Nickname
|
|
|
|
}
|
|
|
|
|
|
|
|
if patch.FirstName != nil {
|
|
|
|
u.FirstName = *patch.FirstName
|
|
|
|
}
|
|
|
|
|
|
|
|
if patch.LastName != nil {
|
|
|
|
u.LastName = *patch.LastName
|
|
|
|
}
|
|
|
|
|
|
|
|
if patch.Position != nil {
|
|
|
|
u.Position = *patch.Position
|
|
|
|
}
|
|
|
|
|
|
|
|
if patch.Email != nil {
|
|
|
|
u.Email = *patch.Email
|
|
|
|
}
|
|
|
|
|
|
|
|
if patch.Props != nil {
|
2017-08-16 23:37:37 +02:00
|
|
|
u.Props = patch.Props
|
2017-03-25 21:04:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if patch.NotifyProps != nil {
|
2017-08-16 23:37:37 +02:00
|
|
|
u.NotifyProps = patch.NotifyProps
|
2017-03-25 21:04:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if patch.Locale != nil {
|
|
|
|
u.Locale = *patch.Locale
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-10 23:39:38 +02:00
|
|
|
// ToJson convert a User to a json string
|
|
|
|
func (u *User) ToJson() string {
|
|
|
|
b, err := json.Marshal(u)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
} else {
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-25 21:04:10 +01:00
|
|
|
func (u *UserPatch) ToJson() string {
|
|
|
|
b, err := json.Marshal(u)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
} else {
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-09 00:11:04 +01:00
|
|
|
func (u *UserAuth) ToJson() string {
|
|
|
|
b, err := json.Marshal(u)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
} else {
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-10 23:39:38 +02:00
|
|
|
// Generate a valid strong etag so the browser can cache the results
|
2016-07-22 23:14:13 +02:00
|
|
|
func (u *User) Etag(showFullName, showEmail bool) string {
|
|
|
|
return Etag(u.Id, u.UpdateAt, showFullName, showEmail)
|
2016-04-10 23:39:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove any private data from the user object
|
|
|
|
func (u *User) Sanitize(options map[string]bool) {
|
|
|
|
u.Password = ""
|
2018-02-09 00:11:04 +01:00
|
|
|
u.AuthData = NewString("")
|
2016-05-15 23:02:30 +02:00
|
|
|
u.MfaSecret = ""
|
2016-04-10 23:39:38 +02:00
|
|
|
|
|
|
|
if len(options) != 0 && !options["email"] {
|
|
|
|
u.Email = ""
|
|
|
|
}
|
|
|
|
if len(options) != 0 && !options["fullname"] {
|
|
|
|
u.FirstName = ""
|
|
|
|
u.LastName = ""
|
|
|
|
}
|
|
|
|
if len(options) != 0 && !options["passwordupdate"] {
|
|
|
|
u.LastPasswordUpdate = 0
|
|
|
|
}
|
2016-11-12 22:00:53 +01:00
|
|
|
if len(options) != 0 && !options["authservice"] {
|
|
|
|
u.AuthService = ""
|
|
|
|
}
|
2016-04-10 23:39:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) ClearNonProfileFields() {
|
|
|
|
u.Password = ""
|
2018-02-09 00:11:04 +01:00
|
|
|
u.AuthData = NewString("")
|
2016-05-15 23:02:30 +02:00
|
|
|
u.MfaSecret = ""
|
2016-04-10 23:39:38 +02:00
|
|
|
u.EmailVerified = false
|
|
|
|
u.AllowMarketing = false
|
|
|
|
u.NotifyProps = StringMap{}
|
|
|
|
u.LastPasswordUpdate = 0
|
|
|
|
u.FailedAttempts = 0
|
|
|
|
}
|
|
|
|
|
2016-09-17 15:19:18 +02:00
|
|
|
func (u *User) SanitizeProfile(options map[string]bool) {
|
|
|
|
u.ClearNonProfileFields()
|
|
|
|
|
|
|
|
u.Sanitize(options)
|
|
|
|
}
|
|
|
|
|
2016-04-10 23:39:38 +02:00
|
|
|
func (u *User) MakeNonNil() {
|
|
|
|
if u.Props == nil {
|
|
|
|
u.Props = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.NotifyProps == nil {
|
|
|
|
u.NotifyProps = make(map[string]string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) AddProp(key string, value string) {
|
|
|
|
u.MakeNonNil()
|
|
|
|
|
|
|
|
u.Props[key] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) AddNotifyProp(key string, value string) {
|
|
|
|
u.MakeNonNil()
|
|
|
|
|
|
|
|
u.NotifyProps[key] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) GetFullName() string {
|
|
|
|
if u.FirstName != "" && u.LastName != "" {
|
|
|
|
return u.FirstName + " " + u.LastName
|
|
|
|
} else if u.FirstName != "" {
|
|
|
|
return u.FirstName
|
|
|
|
} else if u.LastName != "" {
|
|
|
|
return u.LastName
|
|
|
|
} else {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-16 23:37:37 +02:00
|
|
|
func (u *User) GetDisplayName(nameFormat string) string {
|
2016-09-17 15:19:18 +02:00
|
|
|
displayName := u.Username
|
|
|
|
|
2017-08-16 23:37:37 +02:00
|
|
|
if nameFormat == SHOW_NICKNAME_FULLNAME {
|
2016-09-17 15:19:18 +02:00
|
|
|
if u.Nickname != "" {
|
|
|
|
displayName = u.Nickname
|
|
|
|
} else if fullName := u.GetFullName(); fullName != "" {
|
|
|
|
displayName = fullName
|
|
|
|
}
|
2017-08-16 23:37:37 +02:00
|
|
|
} else if nameFormat == SHOW_FULLNAME {
|
2016-09-17 15:19:18 +02:00
|
|
|
if fullName := u.GetFullName(); fullName != "" {
|
|
|
|
displayName = fullName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return displayName
|
|
|
|
}
|
|
|
|
|
2016-11-12 22:00:53 +01:00
|
|
|
func (u *User) GetRoles() []string {
|
|
|
|
return strings.Fields(u.Roles)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) GetRawRoles() string {
|
|
|
|
return u.Roles
|
|
|
|
}
|
|
|
|
|
2016-05-15 23:02:30 +02:00
|
|
|
func IsValidUserRoles(userRoles string) bool {
|
2016-04-10 23:39:38 +02:00
|
|
|
|
2016-11-12 22:00:53 +01:00
|
|
|
roles := strings.Fields(userRoles)
|
2016-04-10 23:39:38 +02:00
|
|
|
|
|
|
|
for _, r := range roles {
|
|
|
|
if !isValidRole(r) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-12 22:00:53 +01:00
|
|
|
// Exclude just the system_admin role explicitly to prevent mistakes
|
|
|
|
if len(roles) == 1 && roles[0] == "system_admin" {
|
|
|
|
return false
|
2016-04-10 23:39:38 +02:00
|
|
|
}
|
|
|
|
|
2016-11-12 22:00:53 +01:00
|
|
|
return true
|
|
|
|
}
|
2016-04-10 23:39:38 +02:00
|
|
|
|
2016-11-12 22:00:53 +01:00
|
|
|
func isValidRole(roleId string) bool {
|
2018-02-09 00:11:04 +01:00
|
|
|
_, ok := DefaultRoles[roleId]
|
2016-11-12 22:00:53 +01:00
|
|
|
return ok
|
2016-04-10 23:39:38 +02:00
|
|
|
}
|
|
|
|
|
2016-07-22 23:14:13 +02:00
|
|
|
// Make sure you acually want to use this function. In context.go there are functions to check permissions
|
2016-04-10 23:39:38 +02:00
|
|
|
// This function should not be used to check permissions.
|
|
|
|
func (u *User) IsInRole(inRole string) bool {
|
|
|
|
return IsInRole(u.Roles, inRole)
|
|
|
|
}
|
|
|
|
|
2016-07-22 23:14:13 +02:00
|
|
|
// Make sure you acually want to use this function. In context.go there are functions to check permissions
|
2016-04-10 23:39:38 +02:00
|
|
|
// This function should not be used to check permissions.
|
|
|
|
func IsInRole(userRoles string, inRole string) bool {
|
|
|
|
roles := strings.Split(userRoles, " ")
|
|
|
|
|
|
|
|
for _, r := range roles {
|
|
|
|
if r == inRole {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-03-25 21:04:10 +01:00
|
|
|
func (u *User) IsSSOUser() bool {
|
2017-08-16 23:37:37 +02:00
|
|
|
return u.AuthService != "" && u.AuthService != USER_AUTH_SERVICE_EMAIL
|
2017-03-25 21:04:10 +01:00
|
|
|
}
|
|
|
|
|
2016-05-15 23:02:30 +02:00
|
|
|
func (u *User) IsOAuthUser() bool {
|
2017-08-16 23:37:37 +02:00
|
|
|
return u.AuthService == USER_AUTH_SERVICE_GITLAB
|
2016-05-15 23:02:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) IsLDAPUser() bool {
|
2017-08-16 23:37:37 +02:00
|
|
|
return u.AuthService == USER_AUTH_SERVICE_LDAP
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) IsSAMLUser() bool {
|
|
|
|
return u.AuthService == USER_AUTH_SERVICE_SAML
|
2016-04-10 23:39:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// UserFromJson will decode the input and return a User
|
|
|
|
func UserFromJson(data io.Reader) *User {
|
|
|
|
decoder := json.NewDecoder(data)
|
|
|
|
var user User
|
|
|
|
err := decoder.Decode(&user)
|
|
|
|
if err == nil {
|
|
|
|
return &user
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-25 21:04:10 +01:00
|
|
|
func UserPatchFromJson(data io.Reader) *UserPatch {
|
|
|
|
decoder := json.NewDecoder(data)
|
|
|
|
var user UserPatch
|
|
|
|
err := decoder.Decode(&user)
|
|
|
|
if err == nil {
|
|
|
|
return &user
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-09 00:11:04 +01:00
|
|
|
func UserAuthFromJson(data io.Reader) *UserAuth {
|
|
|
|
decoder := json.NewDecoder(data)
|
|
|
|
var user UserAuth
|
|
|
|
err := decoder.Decode(&user)
|
|
|
|
if err == nil {
|
|
|
|
return &user
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-10 23:39:38 +02:00
|
|
|
func UserMapToJson(u map[string]*User) string {
|
|
|
|
b, err := json.Marshal(u)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
} else {
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func UserMapFromJson(data io.Reader) map[string]*User {
|
|
|
|
decoder := json.NewDecoder(data)
|
|
|
|
var users map[string]*User
|
|
|
|
err := decoder.Decode(&users)
|
|
|
|
if err == nil {
|
|
|
|
return users
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-12 22:00:53 +01:00
|
|
|
func UserListToJson(u []*User) string {
|
|
|
|
b, err := json.Marshal(u)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
} else {
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func UserListFromJson(data io.Reader) []*User {
|
|
|
|
decoder := json.NewDecoder(data)
|
|
|
|
var users []*User
|
|
|
|
err := decoder.Decode(&users)
|
|
|
|
if err == nil {
|
|
|
|
return users
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-10 23:39:38 +02:00
|
|
|
// HashPassword generates a hash using the bcrypt.GenerateFromPassword
|
|
|
|
func HashPassword(password string) string {
|
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), 10)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ComparePassword compares the hash
|
|
|
|
func ComparePassword(hash string, password string) bool {
|
|
|
|
|
2016-05-15 23:02:30 +02:00
|
|
|
if len(password) == 0 || len(hash) == 0 {
|
2016-04-10 23:39:38 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var validUsernameChars = regexp.MustCompile(`^[a-z0-9\.\-_]+$`)
|
|
|
|
|
|
|
|
var restrictedUsernames = []string{
|
|
|
|
"all",
|
|
|
|
"channel",
|
2016-08-15 18:47:31 +02:00
|
|
|
"matterbot",
|
2016-04-10 23:39:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func IsValidUsername(s string) bool {
|
2017-03-25 21:04:10 +01:00
|
|
|
if len(s) < USER_NAME_MIN_LENGTH || len(s) > USER_NAME_MAX_LENGTH {
|
2016-04-10 23:39:38 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !validUsernameChars.MatchString(s) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, restrictedUsername := range restrictedUsernames {
|
|
|
|
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
|
|
|
|
}
|
2017-08-16 23:37:37 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|