diff options
Diffstat (limited to 'model')
-rw-r--r-- | model/license.go | 85 | ||||
-rw-r--r-- | model/license_test.go | 34 | ||||
-rw-r--r-- | model/user.go | 11 | ||||
-rw-r--r-- | model/utils.go | 40 |
4 files changed, 161 insertions, 9 deletions
diff --git a/model/license.go b/model/license.go new file mode 100644 index 000000000..a271b46b7 --- /dev/null +++ b/model/license.go @@ -0,0 +1,85 @@ +// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package model + +import ( + "encoding/json" + "io" +) + +type License struct { + Id string `json:"id"` + IssuedAt int64 `json:"issued_at"` + StartsAt int64 `json:"starts_at"` + ExpiresAt int64 `json:"expires_at"` + Customer *Customer `json:"customer"` + Features *Features `json:"features"` +} + +type Customer struct { + Id string `json:"id"` + Name string `json:"name"` + Email string `json:"email"` + Company string `json:"company"` + PhoneNumber string `json:"phone_number"` +} + +type Features struct { + Users *int `json:"users"` + LDAP *bool `json:"ldap"` + GoogleSSO *bool `json:"google_sso"` +} + +func (f *Features) SetDefaults() { + if f.Users == nil { + f.Users = new(int) + *f.Users = 0 + } + + if f.LDAP == nil { + f.LDAP = new(bool) + *f.LDAP = true + } + + if f.GoogleSSO == nil { + f.GoogleSSO = new(bool) + *f.GoogleSSO = true + } +} + +func (l *License) IsExpired() bool { + now := GetMillis() + if l.ExpiresAt < now { + return true + } + return false +} + +func (l *License) IsStarted() bool { + now := GetMillis() + if l.StartsAt < now { + return true + } + return false +} + +func (l *License) ToJson() string { + b, err := json.Marshal(l) + if err != nil { + return "" + } else { + return string(b) + } +} + +func LicenseFromJson(data io.Reader) *License { + decoder := json.NewDecoder(data) + var o License + err := decoder.Decode(&o) + if err == nil { + return &o + } else { + return nil + } +} diff --git a/model/license_test.go b/model/license_test.go new file mode 100644 index 000000000..25c74a2e3 --- /dev/null +++ b/model/license_test.go @@ -0,0 +1,34 @@ +// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package model + +import ( + "testing" +) + +func TestLicenseExpired(t *testing.T) { + l1 := License{} + l1.ExpiresAt = GetMillis() - 1000 + if !l1.IsExpired() { + t.Fatal("license should be expired") + } + + l1.ExpiresAt = GetMillis() + 10000 + if l1.IsExpired() { + t.Fatal("license should not be expired") + } +} + +func TestLicenseStarted(t *testing.T) { + l1 := License{} + l1.StartsAt = GetMillis() - 1000 + if !l1.IsStarted() { + t.Fatal("license should be started") + } + + l1.StartsAt = GetMillis() + 10000 + if l1.IsStarted() { + t.Fatal("license should not be started") + } +} diff --git a/model/user.go b/model/user.go index 4ba35c6c4..7744b0073 100644 --- a/model/user.go +++ b/model/user.go @@ -6,11 +6,12 @@ package model import ( "encoding/json" "fmt" - "golang.org/x/crypto/bcrypt" "io" "regexp" "strings" "unicode/utf8" + + "golang.org/x/crypto/bcrypt" ) const ( @@ -24,6 +25,7 @@ const ( USER_NOTIFY_ALL = "all" USER_NOTIFY_MENTION = "mention" USER_NOTIFY_NONE = "none" + DEFAULT_LOCALE = "en" ) type User struct { @@ -51,6 +53,7 @@ type User struct { LastPasswordUpdate int64 `json:"last_password_update,omitempty"` LastPictureUpdate int64 `json:"last_picture_update,omitempty"` FailedAttempts int `json:"failed_attempts,omitempty"` + Locale string `json:"locale"` } // IsValid validates the user and returns an error if it isn't configured @@ -130,12 +133,17 @@ func (u *User) PreSave() { u.Username = strings.ToLower(u.Username) u.Email = strings.ToLower(u.Email) + u.Locale = strings.ToLower(u.Locale) u.CreateAt = GetMillis() u.UpdateAt = u.CreateAt u.LastPasswordUpdate = u.CreateAt + if u.Locale == "" { + u.Locale = DEFAULT_LOCALE + } + if u.Props == nil { u.Props = make(map[string]string) } @@ -153,6 +161,7 @@ func (u *User) PreSave() { func (u *User) PreUpdate() { u.Username = strings.ToLower(u.Username) u.Email = strings.ToLower(u.Email) + u.Locale = strings.ToLower(u.Locale) u.UpdateAt = GetMillis() if u.NotifyProps == nil || len(u.NotifyProps) == 0 { diff --git a/model/utils.go b/model/utils.go index 617c95efd..a18ca760e 100644 --- a/model/utils.go +++ b/model/utils.go @@ -9,13 +9,15 @@ import ( "encoding/base32" "encoding/json" "fmt" - "github.com/pborman/uuid" "io" "net/mail" "net/url" "regexp" "strings" "time" + + goi18n "github.com/nicksnyder/go-i18n/i18n" + "github.com/pborman/uuid" ) type StringInterface map[string]interface{} @@ -23,20 +25,31 @@ type StringMap map[string]string type StringArray []string type EncryptStringMap map[string]string -// AppError is returned for any http response that's not in the 200 range. type AppError struct { - Message string `json:"message"` // Message to be display to the end user without debugging information - DetailedError string `json:"detailed_error"` // Internal error string to help the developer - RequestId string `json:"request_id"` // The RequestId that's also set in the header - StatusCode int `json:"status_code"` // The http status code - Where string `json:"-"` // The function where it happened in the form of Struct.Func - IsOAuth bool `json:"is_oauth"` // Whether the error is OAuth specific + Id string `json:"id"` + Message string `json:"message"` // Message to be display to the end user without debugging information + DetailedError string `json:"detailed_error"` // Internal error string to help the developer + RequestId string `json:"request_id"` // The RequestId that's also set in the header + StatusCode int `json:"status_code"` // The http status code + Where string `json:"-"` // The function where it happened in the form of Struct.Func + IsOAuth bool `json:"is_oauth"` // Whether the error is OAuth specific + params map[string]interface{} `json:"-"` } func (er *AppError) Error() string { return er.Where + ": " + er.Message + ", " + er.DetailedError } +func (er *AppError) Translate(T goi18n.TranslateFunc) { + if len(er.Message) == 0 { + if er.params == nil { + er.Message = T(er.Id) + } else { + er.Message = T(er.Id, er.params) + } + } +} + func (er *AppError) ToJson() string { b, err := json.Marshal(er) if err != nil { @@ -68,6 +81,17 @@ func NewAppError(where string, message string, details string) *AppError { return ap } +func NewLocAppError(where string, id string, params map[string]interface{}, details string) *AppError { + ap := &AppError{} + ap.Id = id + ap.params = params + ap.Where = where + ap.DetailedError = details + ap.StatusCode = 500 + ap.IsOAuth = false + return ap +} + var encoding = base32.NewEncoding("ybndrfg8ejkmcpqxot1uwisza345h769") // NewId is a globally unique identifier. It is a [A-Z0-9] string 26 |