github.com/wgh-/mattermost-server@v4.8.0-rc2+incompatible/model/session.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package model 5 6 import ( 7 "encoding/json" 8 "io" 9 "strings" 10 ) 11 12 const ( 13 SESSION_COOKIE_TOKEN = "MMAUTHTOKEN" 14 SESSION_COOKIE_USER = "MMUSERID" 15 SESSION_CACHE_SIZE = 35000 16 SESSION_PROP_PLATFORM = "platform" 17 SESSION_PROP_OS = "os" 18 SESSION_PROP_BROWSER = "browser" 19 SESSION_PROP_TYPE = "type" 20 SESSION_PROP_USER_ACCESS_TOKEN_ID = "user_access_token_id" 21 SESSION_TYPE_USER_ACCESS_TOKEN = "UserAccessToken" 22 SESSION_ACTIVITY_TIMEOUT = 1000 * 60 * 5 // 5 minutes 23 SESSION_USER_ACCESS_TOKEN_EXPIRY = 100 * 365 // 100 years 24 ) 25 26 type Session struct { 27 Id string `json:"id"` 28 Token string `json:"token"` 29 CreateAt int64 `json:"create_at"` 30 ExpiresAt int64 `json:"expires_at"` 31 LastActivityAt int64 `json:"last_activity_at"` 32 UserId string `json:"user_id"` 33 DeviceId string `json:"device_id"` 34 Roles string `json:"roles"` 35 IsOAuth bool `json:"is_oauth"` 36 Props StringMap `json:"props"` 37 TeamMembers []*TeamMember `json:"team_members" db:"-"` 38 } 39 40 func (me *Session) DeepCopy() *Session { 41 copy := *me 42 return © 43 } 44 45 func (me *Session) ToJson() string { 46 b, _ := json.Marshal(me) 47 return string(b) 48 } 49 50 func SessionFromJson(data io.Reader) *Session { 51 var me *Session 52 json.NewDecoder(data).Decode(&me) 53 return me 54 } 55 56 func (me *Session) PreSave() { 57 if me.Id == "" { 58 me.Id = NewId() 59 } 60 61 if me.Token == "" { 62 me.Token = NewId() 63 } 64 65 me.CreateAt = GetMillis() 66 me.LastActivityAt = me.CreateAt 67 68 if me.Props == nil { 69 me.Props = make(map[string]string) 70 } 71 } 72 73 func (me *Session) Sanitize() { 74 me.Token = "" 75 } 76 77 func (me *Session) IsExpired() bool { 78 79 if me.ExpiresAt <= 0 { 80 return false 81 } 82 83 if GetMillis() > me.ExpiresAt { 84 return true 85 } 86 87 return false 88 } 89 90 func (me *Session) SetExpireInDays(days int) { 91 if me.CreateAt == 0 { 92 me.ExpiresAt = GetMillis() + (1000 * 60 * 60 * 24 * int64(days)) 93 } else { 94 me.ExpiresAt = me.CreateAt + (1000 * 60 * 60 * 24 * int64(days)) 95 } 96 } 97 98 func (me *Session) AddProp(key string, value string) { 99 100 if me.Props == nil { 101 me.Props = make(map[string]string) 102 } 103 104 me.Props[key] = value 105 } 106 107 func (me *Session) GetTeamByTeamId(teamId string) *TeamMember { 108 for _, team := range me.TeamMembers { 109 if team.TeamId == teamId { 110 return team 111 } 112 } 113 114 return nil 115 } 116 117 func (me *Session) IsMobileApp() bool { 118 return len(me.DeviceId) > 0 119 } 120 121 func (me *Session) GetUserRoles() []string { 122 return strings.Fields(me.Roles) 123 } 124 125 func SessionsToJson(o []*Session) string { 126 if b, err := json.Marshal(o); err != nil { 127 return "[]" 128 } else { 129 return string(b) 130 } 131 } 132 133 func SessionsFromJson(data io.Reader) []*Session { 134 var o []*Session 135 json.NewDecoder(data).Decode(&o) 136 return o 137 }