From d3a285e64d051aa8d5c4c9854597dfbcce107675 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Thu, 19 Jan 2017 09:00:13 -0500 Subject: Migrate functions to app package (#5106) * Refactor and move session logic into app package * Refactor email functions into the app package * Refactor password update into app package * Migrate user functions to app package * Move team functions into app package * Migrate channel functions into app package * Pass SiteURL through to app functions * Update based on feedback --- app/session.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 5 deletions(-) (limited to 'app/session.go') diff --git a/app/session.go b/app/session.go index 3bb167891..289bb6a2d 100644 --- a/app/session.go +++ b/app/session.go @@ -14,6 +14,18 @@ import ( var sessionCache *utils.Cache = utils.NewLru(model.SESSION_CACHE_SIZE) +func CreateSession(session *model.Session) (*model.Session, *model.AppError) { + if result := <-Srv.Store.Session().Save(session); result.Err != nil { + return nil, result.Err + } else { + session := result.Data.(*model.Session) + + AddSessionToCache(session) + + return session, nil + } +} + func GetSession(token string) (*model.Session, *model.AppError) { metrics := einterfaces.GetMetricsInterface() @@ -51,16 +63,48 @@ func GetSession(token string) (*model.Session, *model.AppError) { return session, nil } -func RemoveAllSessionsForUserId(userId string) { +func GetSessions(userId string) ([]*model.Session, *model.AppError) { + if result := <-Srv.Store.Session().GetSessions(userId); result.Err != nil { + return nil, result.Err + } else { + return result.Data.([]*model.Session), nil + } +} + +func RevokeAllSessions(userId string) *model.AppError { + if result := <-Srv.Store.Session().GetSessions(userId); result.Err != nil { + return result.Err + } else { + sessions := result.Data.([]*model.Session) + + for _, session := range sessions { + if session.IsOAuth { + RevokeAccessToken(session.Token) + } else { + if result := <-Srv.Store.Session().Remove(session.Id); result.Err != nil { + return result.Err + } + } + + RevokeWebrtcToken(session.Id) + } + } + + ClearSessionCacheForUser(userId) + + return nil +} + +func ClearSessionCacheForUser(userId string) { - RemoveAllSessionsForUserIdSkipClusterSend(userId) + ClearSessionCacheForUserSkipClusterSend(userId) if einterfaces.GetClusterInterface() != nil { - einterfaces.GetClusterInterface().RemoveAllSessionsForUserId(userId) + einterfaces.GetClusterInterface().ClearSessionCacheForUser(userId) } } -func RemoveAllSessionsForUserIdSkipClusterSend(userId string) { +func ClearSessionCacheForUserSkipClusterSend(userId string) { keys := sessionCache.Keys() for _, key := range keys { @@ -132,7 +176,7 @@ func RevokeSession(session *model.Session) *model.AppError { } RevokeWebrtcToken(session.Id) - RemoveAllSessionsForUserId(session.UserId) + ClearSessionCacheForUser(session.UserId) return nil } -- cgit v1.2.3-1-g7c22