PLT-7 adding loc db calls for session table

This commit is contained in:
=Corey Hulen
2016-01-20 10:18:11 -06:00
parent 11c035aef4
commit 3ac5ecf0e9
11 changed files with 75 additions and 72 deletions

View File

@@ -162,7 +162,7 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
if len(token) != 0 {
session := GetSession(token)
session := GetSession(c.T, token)
if session == nil || session.IsExpired() {
c.RemoveSessionCookie(w, r)
@@ -515,14 +515,14 @@ func Handle404(w http.ResponseWriter, r *http.Request) {
RenderWebError(err, w, r)
}
func GetSession(token string) *model.Session {
func GetSession(T goi18n.TranslateFunc, token string) *model.Session {
var session *model.Session
if ts, ok := sessionCache.Get(token); ok {
session = ts.(*model.Session)
}
if session == nil {
if sessionResult := <-Srv.Store.Session().Get(token); sessionResult.Err != nil {
if sessionResult := <-Srv.Store.Session().Get(T, token); sessionResult.Err != nil {
l4g.Error("Invalid session token=" + token + ", err=" + sessionResult.Err.DetailedError)
} else {
session = sessionResult.Data.(*model.Session)
@@ -551,9 +551,9 @@ func GetMultiSessionCookieTokens(r *http.Request) []string {
return []string{}
}
func FindMultiSessionForTeamId(r *http.Request, teamId string) (int64, *model.Session) {
func FindMultiSessionForTeamId(T goi18n.TranslateFunc, r *http.Request, teamId string) (int64, *model.Session) {
for index, token := range GetMultiSessionCookieTokens(r) {
s := GetSession(token)
s := GetSession(T, token)
if s != nil && !s.IsExpired() && s.TeamId == teamId {
return int64(index), s
}

View File

@@ -129,7 +129,7 @@ func allowOAuth(c *Context, w http.ResponseWriter, r *http.Request) {
func RevokeAccessToken(T goi18n.TranslateFunc, token string) *model.AppError {
schan := Srv.Store.Session().Remove(token)
schan := Srv.Store.Session().Remove(T, token)
sessionCache.Remove(token)
var accessData *model.AccessData

View File

@@ -626,7 +626,7 @@ func sendNotificationsAndForget(c *Context, post *model.Post, team *model.Team,
}
if *utils.Cfg.EmailSettings.SendPushNotifications {
sessionChan := Srv.Store.Session().GetSessions(id)
sessionChan := Srv.Store.Session().GetSessions(c.T, id)
if result := <-sessionChan; result.Err != nil {
l4g.Error("Failed to retrieve sessions in notifications id=%v, err=%v", id, result.Err)
} else {

View File

@@ -331,7 +331,7 @@ func revokeAllSessions(c *Context, w http.ResponseWriter, r *http.Request) {
props := model.MapFromJson(r.Body)
id := props["id"]
if result := <-Srv.Store.Session().Get(id); result.Err != nil {
if result := <-Srv.Store.Session().Get(c.T, id); result.Err != nil {
c.Err = result.Err
return
} else {
@@ -344,7 +344,7 @@ func revokeAllSessions(c *Context, w http.ResponseWriter, r *http.Request) {
} else {
sessionCache.Remove(session.Token)
if result := <-Srv.Store.Session().Remove(session.Id); result.Err != nil {
if result := <-Srv.Store.Session().Remove(c.T, session.Id); result.Err != nil {
c.Err = result.Err
return
} else {

View File

@@ -516,7 +516,7 @@ func Login(c *Context, w http.ResponseWriter, r *http.Request, user *model.User,
maxAge = *utils.Cfg.ServiceSettings.SessionLengthMobileInDays * 60 * 60 * 24
// A special case where we logout of all other sessions with the same Id
if result := <-Srv.Store.Session().GetSessions(user.Id); result.Err != nil {
if result := <-Srv.Store.Session().GetSessions(c.T, user.Id); result.Err != nil {
c.Err = result.Err
c.Err.StatusCode = http.StatusForbidden
return
@@ -563,7 +563,7 @@ func Login(c *Context, w http.ResponseWriter, r *http.Request, user *model.User,
session.AddProp(model.SESSION_PROP_OS, os)
session.AddProp(model.SESSION_PROP_BROWSER, fmt.Sprintf("%v/%v", bname, bversion))
if result := <-Srv.Store.Session().Save(session); result.Err != nil {
if result := <-Srv.Store.Session().Save(c.T, session); result.Err != nil {
c.Err = result.Err
c.Err.StatusCode = http.StatusForbidden
return
@@ -579,7 +579,7 @@ func Login(c *Context, w http.ResponseWriter, r *http.Request, user *model.User,
seen := make(map[string]string)
seen[session.TeamId] = session.TeamId
for _, token := range tokens {
s := GetSession(token)
s := GetSession(c.T, token)
if s != nil && !s.IsExpired() && seen[s.TeamId] == "" {
multiToken += " " + token
seen[s.TeamId] = s.TeamId
@@ -706,7 +706,7 @@ func revokeSession(c *Context, w http.ResponseWriter, r *http.Request) {
}
func RevokeSessionById(c *Context, sessionId string) {
if result := <-Srv.Store.Session().Get(sessionId); result.Err != nil {
if result := <-Srv.Store.Session().Get(c.T, sessionId); result.Err != nil {
c.Err = result.Err
} else {
session := result.Data.(*model.Session)
@@ -717,7 +717,7 @@ func RevokeSessionById(c *Context, sessionId string) {
} else {
sessionCache.Remove(session.Token)
if result := <-Srv.Store.Session().Remove(session.Id); result.Err != nil {
if result := <-Srv.Store.Session().Remove(c.T, session.Id); result.Err != nil {
c.Err = result.Err
}
}
@@ -725,7 +725,7 @@ func RevokeSessionById(c *Context, sessionId string) {
}
func RevokeAllSession(c *Context, userId string) {
if result := <-Srv.Store.Session().GetSessions(userId); result.Err != nil {
if result := <-Srv.Store.Session().GetSessions(c.T, userId); result.Err != nil {
c.Err = result.Err
return
} else {
@@ -737,7 +737,7 @@ func RevokeAllSession(c *Context, userId string) {
RevokeAccessToken(c.T, session.Token)
} else {
sessionCache.Remove(session.Token)
if result := <-Srv.Store.Session().Remove(session.Id); result.Err != nil {
if result := <-Srv.Store.Session().Remove(c.T, session.Id); result.Err != nil {
c.Err = result.Err
return
}
@@ -755,7 +755,7 @@ func getSessions(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
if result := <-Srv.Store.Session().GetSessions(id); result.Err != nil {
if result := <-Srv.Store.Session().GetSessions(c.T, id); result.Err != nil {
c.Err = result.Err
return
} else {
@@ -781,7 +781,7 @@ func logout(c *Context, w http.ResponseWriter, r *http.Request) {
func Logout(c *Context, w http.ResponseWriter, r *http.Request) {
c.LogAudit("")
c.RemoveSessionCookie(w, r)
if result := <-Srv.Store.Session().Remove(c.Session.Id); result.Err != nil {
if result := <-Srv.Store.Session().Remove(c.T, c.Session.Id); result.Err != nil {
c.Err = result.Err
return
}
@@ -1280,8 +1280,8 @@ func updateRoles(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
uchan := Srv.Store.Session().UpdateRoles(user.Id, new_roles)
gchan := Srv.Store.Session().GetSessions(user.Id)
uchan := Srv.Store.Session().UpdateRoles(c.T, user.Id, new_roles)
gchan := Srv.Store.Session().GetSessions(c.T, user.Id)
if result := <-uchan; result.Err != nil {
// soft error since the user roles were still updated
@@ -1436,7 +1436,7 @@ func PermanentDeleteUser(c *Context, user *model.User) *model.AppError {
UpdateActive(c, user, false)
if result := <-Srv.Store.Session().PermanentDeleteSessionsByUser(user.Id); result.Err != nil {
if result := <-Srv.Store.Session().PermanentDeleteSessionsByUser(c.T, user.Id); result.Err != nil {
return result.Err
}

View File

@@ -186,7 +186,7 @@ func TestLoginWithDeviceId(t *testing.T) {
t.Fatal(err)
}
if sresult := <-Srv.Store.Session().Get(sessions[0].Id); sresult.Err == nil {
if sresult := <-Srv.Store.Session().Get(utils.T, sessions[0].Id); sresult.Err == nil {
t.Fatal("session should have been removed")
}
}

View File

@@ -6,6 +6,7 @@ package store
import (
l4g "github.com/alecthomas/log4go"
"github.com/mattermost/platform/model"
goi18n "github.com/nicksnyder/go-i18n/i18n"
)
type SqlSessionStore struct {
@@ -37,7 +38,7 @@ func (me SqlSessionStore) CreateIndexesIfNotExists() {
me.CreateIndexIfNotExists("idx_sessions_token", "Sessions", "Token")
}
func (me SqlSessionStore) Save(session *model.Session) StoreChannel {
func (me SqlSessionStore) Save(T goi18n.TranslateFunc, session *model.Session) StoreChannel {
storeChannel := make(StoreChannel)
@@ -53,7 +54,7 @@ func (me SqlSessionStore) Save(session *model.Session) StoreChannel {
session.PreSave()
if cur := <-me.CleanUpExpiredSessions(session.UserId); cur.Err != nil {
if cur := <-me.CleanUpExpiredSessions(T, session.UserId); cur.Err != nil {
l4g.Error("Failed to cleanup sessions in Save err=%v", cur.Err)
}
@@ -70,7 +71,7 @@ func (me SqlSessionStore) Save(session *model.Session) StoreChannel {
return storeChannel
}
func (me SqlSessionStore) Get(sessionIdOrToken string) StoreChannel {
func (me SqlSessionStore) Get(T goi18n.TranslateFunc, sessionIdOrToken string) StoreChannel {
storeChannel := make(StoreChannel)
@@ -95,12 +96,12 @@ func (me SqlSessionStore) Get(sessionIdOrToken string) StoreChannel {
return storeChannel
}
func (me SqlSessionStore) GetSessions(userId string) StoreChannel {
func (me SqlSessionStore) GetSessions(T goi18n.TranslateFunc, userId string) StoreChannel {
storeChannel := make(StoreChannel)
go func() {
if cur := <-me.CleanUpExpiredSessions(userId); cur.Err != nil {
if cur := <-me.CleanUpExpiredSessions(T, userId); cur.Err != nil {
l4g.Error("Failed to cleanup sessions in getSessions err=%v", cur.Err)
}
@@ -122,7 +123,7 @@ func (me SqlSessionStore) GetSessions(userId string) StoreChannel {
return storeChannel
}
func (me SqlSessionStore) Remove(sessionIdOrToken string) StoreChannel {
func (me SqlSessionStore) Remove(T goi18n.TranslateFunc, sessionIdOrToken string) StoreChannel {
storeChannel := make(StoreChannel)
go func() {
@@ -140,7 +141,7 @@ func (me SqlSessionStore) Remove(sessionIdOrToken string) StoreChannel {
return storeChannel
}
func (me SqlSessionStore) RemoveAllSessionsForTeam(teamId string) StoreChannel {
func (me SqlSessionStore) RemoveAllSessionsForTeam(T goi18n.TranslateFunc, teamId string) StoreChannel {
storeChannel := make(StoreChannel)
go func() {
@@ -158,7 +159,7 @@ func (me SqlSessionStore) RemoveAllSessionsForTeam(teamId string) StoreChannel {
return storeChannel
}
func (me SqlSessionStore) PermanentDeleteSessionsByUser(userId string) StoreChannel {
func (me SqlSessionStore) PermanentDeleteSessionsByUser(T goi18n.TranslateFunc, userId string) StoreChannel {
storeChannel := make(StoreChannel)
go func() {
@@ -176,7 +177,7 @@ func (me SqlSessionStore) PermanentDeleteSessionsByUser(userId string) StoreChan
return storeChannel
}
func (me SqlSessionStore) CleanUpExpiredSessions(userId string) StoreChannel {
func (me SqlSessionStore) CleanUpExpiredSessions(T goi18n.TranslateFunc, userId string) StoreChannel {
storeChannel := make(StoreChannel)
go func() {
@@ -195,7 +196,7 @@ func (me SqlSessionStore) CleanUpExpiredSessions(userId string) StoreChannel {
return storeChannel
}
func (me SqlSessionStore) UpdateLastActivityAt(sessionId string, time int64) StoreChannel {
func (me SqlSessionStore) UpdateLastActivityAt(T goi18n.TranslateFunc, sessionId string, time int64) StoreChannel {
storeChannel := make(StoreChannel)
go func() {
@@ -214,7 +215,7 @@ func (me SqlSessionStore) UpdateLastActivityAt(sessionId string, time int64) Sto
return storeChannel
}
func (me SqlSessionStore) UpdateRoles(userId, roles string) StoreChannel {
func (me SqlSessionStore) UpdateRoles(T goi18n.TranslateFunc, userId, roles string) StoreChannel {
storeChannel := make(StoreChannel)
go func() {

View File

@@ -5,6 +5,7 @@ package store
import (
"github.com/mattermost/platform/model"
"github.com/mattermost/platform/utils"
"testing"
)
@@ -15,7 +16,7 @@ func TestSessionStoreSave(t *testing.T) {
s1.UserId = model.NewId()
s1.TeamId = model.NewId()
if err := (<-store.Session().Save(&s1)).Err; err != nil {
if err := (<-store.Session().Save(utils.T, &s1)).Err; err != nil {
t.Fatal(err)
}
}
@@ -26,20 +27,20 @@ func TestSessionGet(t *testing.T) {
s1 := model.Session{}
s1.UserId = model.NewId()
s1.TeamId = model.NewId()
Must(store.Session().Save(&s1))
Must(store.Session().Save(utils.T, &s1))
s2 := model.Session{}
s2.UserId = s1.UserId
s2.TeamId = s1.TeamId
Must(store.Session().Save(&s2))
Must(store.Session().Save(utils.T, &s2))
s3 := model.Session{}
s3.UserId = s1.UserId
s3.TeamId = s1.TeamId
s3.ExpiresAt = 1
Must(store.Session().Save(&s3))
Must(store.Session().Save(utils.T, &s3))
if rs1 := (<-store.Session().Get(s1.Id)); rs1.Err != nil {
if rs1 := (<-store.Session().Get(utils.T, s1.Id)); rs1.Err != nil {
t.Fatal(rs1.Err)
} else {
if rs1.Data.(*model.Session).Id != s1.Id {
@@ -47,7 +48,7 @@ func TestSessionGet(t *testing.T) {
}
}
if rs2 := (<-store.Session().GetSessions(s1.UserId)); rs2.Err != nil {
if rs2 := (<-store.Session().GetSessions(utils.T, s1.UserId)); rs2.Err != nil {
t.Fatal(rs2.Err)
} else {
if len(rs2.Data.([]*model.Session)) != 2 {
@@ -63,9 +64,9 @@ func TestSessionRemove(t *testing.T) {
s1 := model.Session{}
s1.UserId = model.NewId()
s1.TeamId = model.NewId()
Must(store.Session().Save(&s1))
Must(store.Session().Save(utils.T, &s1))
if rs1 := (<-store.Session().Get(s1.Id)); rs1.Err != nil {
if rs1 := (<-store.Session().Get(utils.T, s1.Id)); rs1.Err != nil {
t.Fatal(rs1.Err)
} else {
if rs1.Data.(*model.Session).Id != s1.Id {
@@ -73,9 +74,9 @@ func TestSessionRemove(t *testing.T) {
}
}
Must(store.Session().Remove(s1.Id))
Must(store.Session().Remove(utils.T, s1.Id))
if rs2 := (<-store.Session().Get(s1.Id)); rs2.Err == nil {
if rs2 := (<-store.Session().Get(utils.T, s1.Id)); rs2.Err == nil {
t.Fatal("should have been removed")
}
}
@@ -86,9 +87,9 @@ func TestSessionRemoveAll(t *testing.T) {
s1 := model.Session{}
s1.UserId = model.NewId()
s1.TeamId = model.NewId()
Must(store.Session().Save(&s1))
Must(store.Session().Save(utils.T, &s1))
if rs1 := (<-store.Session().Get(s1.Id)); rs1.Err != nil {
if rs1 := (<-store.Session().Get(utils.T, s1.Id)); rs1.Err != nil {
t.Fatal(rs1.Err)
} else {
if rs1.Data.(*model.Session).Id != s1.Id {
@@ -96,9 +97,9 @@ func TestSessionRemoveAll(t *testing.T) {
}
}
Must(store.Session().RemoveAllSessionsForTeam(s1.TeamId))
Must(store.Session().RemoveAllSessionsForTeam(utils.T, s1.TeamId))
if rs2 := (<-store.Session().Get(s1.Id)); rs2.Err == nil {
if rs2 := (<-store.Session().Get(utils.T, s1.Id)); rs2.Err == nil {
t.Fatal("should have been removed")
}
}
@@ -109,9 +110,9 @@ func TestSessionRemoveByUser(t *testing.T) {
s1 := model.Session{}
s1.UserId = model.NewId()
s1.TeamId = model.NewId()
Must(store.Session().Save(&s1))
Must(store.Session().Save(utils.T, &s1))
if rs1 := (<-store.Session().Get(s1.Id)); rs1.Err != nil {
if rs1 := (<-store.Session().Get(utils.T, s1.Id)); rs1.Err != nil {
t.Fatal(rs1.Err)
} else {
if rs1.Data.(*model.Session).Id != s1.Id {
@@ -119,9 +120,9 @@ func TestSessionRemoveByUser(t *testing.T) {
}
}
Must(store.Session().PermanentDeleteSessionsByUser(s1.UserId))
Must(store.Session().PermanentDeleteSessionsByUser(utils.T, s1.UserId))
if rs2 := (<-store.Session().Get(s1.Id)); rs2.Err == nil {
if rs2 := (<-store.Session().Get(utils.T, s1.Id)); rs2.Err == nil {
t.Fatal("should have been removed")
}
}
@@ -132,9 +133,9 @@ func TestSessionRemoveToken(t *testing.T) {
s1 := model.Session{}
s1.UserId = model.NewId()
s1.TeamId = model.NewId()
Must(store.Session().Save(&s1))
Must(store.Session().Save(utils.T, &s1))
if rs1 := (<-store.Session().Get(s1.Id)); rs1.Err != nil {
if rs1 := (<-store.Session().Get(utils.T, s1.Id)); rs1.Err != nil {
t.Fatal(rs1.Err)
} else {
if rs1.Data.(*model.Session).Id != s1.Id {
@@ -142,13 +143,13 @@ func TestSessionRemoveToken(t *testing.T) {
}
}
Must(store.Session().Remove(s1.Token))
Must(store.Session().Remove(utils.T, s1.Token))
if rs2 := (<-store.Session().Get(s1.Id)); rs2.Err == nil {
if rs2 := (<-store.Session().Get(utils.T, s1.Id)); rs2.Err == nil {
t.Fatal("should have been removed")
}
if rs3 := (<-store.Session().GetSessions(s1.UserId)); rs3.Err != nil {
if rs3 := (<-store.Session().GetSessions(utils.T, s1.UserId)); rs3.Err != nil {
t.Fatal(rs3.Err)
} else {
if len(rs3.Data.([]*model.Session)) != 0 {
@@ -163,13 +164,13 @@ func TestSessionStoreUpdateLastActivityAt(t *testing.T) {
s1 := model.Session{}
s1.UserId = model.NewId()
s1.TeamId = model.NewId()
Must(store.Session().Save(&s1))
Must(store.Session().Save(utils.T, &s1))
if err := (<-store.Session().UpdateLastActivityAt(s1.Id, 1234567890)).Err; err != nil {
if err := (<-store.Session().UpdateLastActivityAt(utils.T, s1.Id, 1234567890)).Err; err != nil {
t.Fatal(err)
}
if r1 := <-store.Session().Get(s1.Id); r1.Err != nil {
if r1 := <-store.Session().Get(utils.T, s1.Id); r1.Err != nil {
t.Fatal(r1.Err)
} else {
if r1.Data.(*model.Session).LastActivityAt != 1234567890 {

View File

@@ -5,6 +5,7 @@ package store
import (
"github.com/mattermost/platform/model"
"github.com/mattermost/platform/utils"
"strings"
"testing"
"time"
@@ -161,7 +162,7 @@ func TestUserStoreUpdateUserAndSessionActivity(t *testing.T) {
s1 := model.Session{}
s1.UserId = u1.Id
s1.TeamId = u1.TeamId
Must(store.Session().Save(&s1))
Must(store.Session().Save(utils.T, &s1))
if err := (<-store.User().UpdateUserAndSessionActivity(u1.Id, s1.Id, 1234567890)).Err; err != nil {
t.Fatal(err)
@@ -175,7 +176,7 @@ func TestUserStoreUpdateUserAndSessionActivity(t *testing.T) {
}
}
if r2 := <-store.Session().Get(s1.Id); r2.Err != nil {
if r2 := <-store.Session().Get(utils.T, s1.Id); r2.Err != nil {
t.Fatal(r2.Err)
} else {
if r2.Data.(*model.Session).LastActivityAt != 1234567890 {

View File

@@ -130,14 +130,14 @@ type UserStore interface {
}
type SessionStore interface {
Save(session *model.Session) StoreChannel
Get(sessionIdOrToken string) StoreChannel
GetSessions(userId string) StoreChannel
Remove(sessionIdOrToken string) StoreChannel
RemoveAllSessionsForTeam(teamId string) StoreChannel
PermanentDeleteSessionsByUser(teamId string) StoreChannel
UpdateLastActivityAt(sessionId string, time int64) StoreChannel
UpdateRoles(userId string, roles string) StoreChannel
Save(T goi18n.TranslateFunc, session *model.Session) StoreChannel
Get(T goi18n.TranslateFunc, sessionIdOrToken string) StoreChannel
GetSessions(T goi18n.TranslateFunc, userId string) StoreChannel
Remove(T goi18n.TranslateFunc, sessionIdOrToken string) StoreChannel
RemoveAllSessionsForTeam(T goi18n.TranslateFunc, teamId string) StoreChannel
PermanentDeleteSessionsByUser(T goi18n.TranslateFunc, teamId string) StoreChannel
UpdateLastActivityAt(T goi18n.TranslateFunc, sessionId string, time int64) StoreChannel
UpdateRoles(T goi18n.TranslateFunc, userId string, roles string) StoreChannel
}
type AuditStore interface {

View File

@@ -238,7 +238,7 @@ func login(c *api.Context, w http.ResponseWriter, r *http.Request) {
}
// We still might be able to switch to this team because we've logged in before
_, session := api.FindMultiSessionForTeamId(r, team.Id)
_, session := api.FindMultiSessionForTeamId(c.T, r, team.Id)
if session != nil {
w.Header().Set(model.HEADER_TOKEN, session.Token)
lastViewChannelName := "town-square"
@@ -496,7 +496,7 @@ func checkSessionSwitch(c *api.Context, w http.ResponseWriter, r *http.Request,
// We are logged into a different team. Lets see if we have another
// session in the cookie that will give us access.
if c.Session.TeamId != team.Id {
index, session := api.FindMultiSessionForTeamId(r, team.Id)
index, session := api.FindMultiSessionForTeamId(c.T, r, team.Id)
if session == nil {
// redirect to login
http.Redirect(w, r, c.GetSiteURL()+"/"+team.Name+"/?redirect="+url.QueryEscape(r.URL.Path), http.StatusTemporaryRedirect)
@@ -985,7 +985,7 @@ func getAccessToken(c *api.Context, w http.ResponseWriter, r *http.Request) {
session := &model.Session{UserId: user.Id, TeamId: user.TeamId, Roles: user.Roles, IsOAuth: true}
if result := <-api.Srv.Store.Session().Save(session); result.Err != nil {
if result := <-api.Srv.Store.Session().Save(c.T, session); result.Err != nil {
c.Err = model.NewAppError("getAccessToken", "server_error: Encountered internal server error while saving session to database", "")
return
} else {