matterbridge/vendor/github.com/mattermost/platform/model/session.go

139 lines
2.6 KiB
Go
Raw Normal View History

2016-04-10 21:39:38 +00:00
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package model
import (
"encoding/json"
"io"
2016-09-17 13:19:18 +00:00
"strings"
2016-04-10 21:39:38 +00:00
)
const (
2016-05-15 21:02:30 +00:00
SESSION_COOKIE_TOKEN = "MMAUTHTOKEN"
2017-03-25 20:04:10 +00:00
SESSION_CACHE_SIZE = 35000
2016-04-10 21:39:38 +00:00
SESSION_PROP_PLATFORM = "platform"
SESSION_PROP_OS = "os"
SESSION_PROP_BROWSER = "browser"
)
type Session struct {
2016-05-15 21:02:30 +00:00
Id string `json:"id"`
Token string `json:"token"`
CreateAt int64 `json:"create_at"`
ExpiresAt int64 `json:"expires_at"`
LastActivityAt int64 `json:"last_activity_at"`
UserId string `json:"user_id"`
DeviceId string `json:"device_id"`
Roles string `json:"roles"`
IsOAuth bool `json:"is_oauth"`
Props StringMap `json:"props"`
TeamMembers []*TeamMember `json:"team_members" db:"-"`
2016-04-10 21:39:38 +00:00
}
func (me *Session) ToJson() string {
b, err := json.Marshal(me)
if err != nil {
return ""
} else {
return string(b)
}
}
func SessionFromJson(data io.Reader) *Session {
decoder := json.NewDecoder(data)
var me Session
err := decoder.Decode(&me)
if err == nil {
return &me
} else {
return nil
}
}
func (me *Session) PreSave() {
if me.Id == "" {
me.Id = NewId()
}
me.Token = NewId()
me.CreateAt = GetMillis()
me.LastActivityAt = me.CreateAt
if me.Props == nil {
me.Props = make(map[string]string)
}
}
func (me *Session) Sanitize() {
me.Token = ""
}
func (me *Session) IsExpired() bool {
if me.ExpiresAt <= 0 {
return false
}
if GetMillis() > me.ExpiresAt {
return true
}
return false
}
func (me *Session) SetExpireInDays(days int) {
2016-08-15 16:47:31 +00:00
if me.CreateAt == 0 {
me.ExpiresAt = GetMillis() + (1000 * 60 * 60 * 24 * int64(days))
} else {
me.ExpiresAt = me.CreateAt + (1000 * 60 * 60 * 24 * int64(days))
}
2016-04-10 21:39:38 +00:00
}
func (me *Session) AddProp(key string, value string) {
if me.Props == nil {
me.Props = make(map[string]string)
}
me.Props[key] = value
}
2016-05-15 21:02:30 +00:00
func (me *Session) GetTeamByTeamId(teamId string) *TeamMember {
for _, team := range me.TeamMembers {
if team.TeamId == teamId {
return team
}
}
return nil
}
2016-09-17 13:19:18 +00:00
func (me *Session) IsMobileApp() bool {
2017-03-25 20:04:10 +00:00
return len(me.DeviceId) > 0
2016-09-17 13:19:18 +00:00
}
2016-11-12 21:00:53 +00:00
func (me *Session) GetUserRoles() []string {
return strings.Fields(me.Roles)
}
2016-04-10 21:39:38 +00:00
func SessionsToJson(o []*Session) string {
if b, err := json.Marshal(o); err != nil {
return "[]"
} else {
return string(b)
}
}
func SessionsFromJson(data io.Reader) []*Session {
decoder := json.NewDecoder(data)
var o []*Session
err := decoder.Decode(&o)
if err == nil {
return o
} else {
return nil
}
}