2020-12-02 21:12:14 +01:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
sq "github.com/Masterminds/squirrel"
|
2021-01-26 23:13:46 +01:00
|
|
|
"github.com/mattermost/focalboard/server/model"
|
2021-10-07 13:51:01 +02:00
|
|
|
"github.com/mattermost/focalboard/server/utils"
|
2020-12-02 21:12:14 +01:00
|
|
|
)
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
// GetActiveUserCount returns the number of users with active sessions within N seconds ago.
|
2021-10-22 12:48:53 +02:00
|
|
|
func (s *SQLStore) getActiveUserCount(db sq.BaseRunner, updatedSecondsAgo int64) (int, error) {
|
|
|
|
query := s.getQueryBuilder(db).
|
2021-01-27 19:01:24 +01:00
|
|
|
Select("count(distinct user_id)").
|
2021-04-17 09:06:57 +02:00
|
|
|
From(s.tablePrefix + "sessions").
|
2021-10-07 13:51:01 +02:00
|
|
|
Where(sq.Gt{"update_at": utils.GetMillis() - utils.SecondsToMillis(updatedSecondsAgo)})
|
2021-01-27 19:01:24 +01:00
|
|
|
|
|
|
|
row := query.QueryRow()
|
|
|
|
|
|
|
|
var count int
|
|
|
|
err := row.Scan(&count)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
|
2021-10-22 12:48:53 +02:00
|
|
|
func (s *SQLStore) getSession(db sq.BaseRunner, token string, expireTimeSeconds int64) (*model.Session, error) {
|
|
|
|
query := s.getQueryBuilder(db).
|
2021-03-26 19:01:54 +01:00
|
|
|
Select("id", "token", "user_id", "auth_service", "props").
|
2021-04-17 09:06:57 +02:00
|
|
|
From(s.tablePrefix + "sessions").
|
2020-12-07 17:04:35 +01:00
|
|
|
Where(sq.Eq{"token": token}).
|
2021-10-07 13:51:01 +02:00
|
|
|
Where(sq.Gt{"update_at": utils.GetMillis() - utils.SecondsToMillis(expireTimeSeconds)})
|
2020-12-02 21:12:14 +01:00
|
|
|
|
|
|
|
row := query.QueryRow()
|
|
|
|
session := model.Session{}
|
|
|
|
|
|
|
|
var propsBytes []byte
|
2021-03-26 19:01:54 +01:00
|
|
|
err := row.Scan(&session.ID, &session.Token, &session.UserID, &session.AuthService, &propsBytes)
|
2020-12-02 21:12:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(propsBytes, &session.Props)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &session, nil
|
|
|
|
}
|
|
|
|
|
2021-10-22 12:48:53 +02:00
|
|
|
func (s *SQLStore) createSession(db sq.BaseRunner, session *model.Session) error {
|
2021-10-07 13:51:01 +02:00
|
|
|
now := utils.GetMillis()
|
2020-12-02 21:12:14 +01:00
|
|
|
|
|
|
|
propsBytes, err := json.Marshal(session.Props)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-22 12:48:53 +02:00
|
|
|
query := s.getQueryBuilder(db).Insert(s.tablePrefix+"sessions").
|
2021-03-26 19:01:54 +01:00
|
|
|
Columns("id", "token", "user_id", "auth_service", "props", "create_at", "update_at").
|
|
|
|
Values(session.ID, session.Token, session.UserID, session.AuthService, propsBytes, now, now)
|
2020-12-02 21:12:14 +01:00
|
|
|
|
|
|
|
_, err = query.Exec()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-22 12:48:53 +02:00
|
|
|
func (s *SQLStore) refreshSession(db sq.BaseRunner, session *model.Session) error {
|
2021-10-07 13:51:01 +02:00
|
|
|
now := utils.GetMillis()
|
2020-12-02 21:12:14 +01:00
|
|
|
|
2021-10-22 12:48:53 +02:00
|
|
|
query := s.getQueryBuilder(db).Update(s.tablePrefix+"sessions").
|
2021-01-27 19:01:24 +01:00
|
|
|
Where(sq.Eq{"token": session.Token}).
|
2020-12-02 21:12:14 +01:00
|
|
|
Set("update_at", now)
|
|
|
|
|
|
|
|
_, err := query.Exec()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-22 12:48:53 +02:00
|
|
|
func (s *SQLStore) updateSession(db sq.BaseRunner, session *model.Session) error {
|
2021-10-07 13:51:01 +02:00
|
|
|
now := utils.GetMillis()
|
2020-12-02 21:12:14 +01:00
|
|
|
|
|
|
|
propsBytes, err := json.Marshal(session.Props)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-22 12:48:53 +02:00
|
|
|
query := s.getQueryBuilder(db).Update(s.tablePrefix+"sessions").
|
2021-01-27 19:24:19 +01:00
|
|
|
Where(sq.Eq{"token": session.Token}).
|
2020-12-02 21:12:14 +01:00
|
|
|
Set("update_at", now).
|
|
|
|
Set("props", propsBytes)
|
|
|
|
|
|
|
|
_, err = query.Exec()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-22 12:48:53 +02:00
|
|
|
func (s *SQLStore) deleteSession(db sq.BaseRunner, sessionID string) error {
|
|
|
|
query := s.getQueryBuilder(db).Delete(s.tablePrefix + "sessions").
|
2021-07-22 13:56:25 +02:00
|
|
|
Where(sq.Eq{"id": sessionID})
|
2020-12-02 21:12:14 +01:00
|
|
|
|
|
|
|
_, err := query.Exec()
|
|
|
|
return err
|
|
|
|
}
|
2020-12-07 17:04:35 +01:00
|
|
|
|
2021-10-22 12:48:53 +02:00
|
|
|
func (s *SQLStore) cleanUpSessions(db sq.BaseRunner, expireTimeSeconds int64) error {
|
|
|
|
query := s.getQueryBuilder(db).Delete(s.tablePrefix + "sessions").
|
2021-10-07 13:51:01 +02:00
|
|
|
Where(sq.Lt{"update_at": utils.GetMillis() - utils.SecondsToMillis(expireTimeSeconds)})
|
2020-12-07 17:04:35 +01:00
|
|
|
|
|
|
|
_, err := query.Exec()
|
|
|
|
return err
|
|
|
|
}
|