2016-11-16 13:47:49 +00:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
2021-09-14 12:11:07 +00:00
|
|
|
"context"
|
|
|
|
|
2021-11-15 14:11:38 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/ctxerr"
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2021-09-14 14:44:02 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2016-11-16 13:47:49 +00:00
|
|
|
)
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
func (d *Datastore) SessionByKey(ctx context.Context, key string) (*fleet.Session, error) {
|
2016-11-16 13:47:49 +00:00
|
|
|
sqlStatement := `
|
|
|
|
SELECT * FROM sessions
|
2016-11-16 23:12:59 +00:00
|
|
|
WHERE ` + "`key`" + ` = ? LIMIT 1
|
2016-11-16 13:47:49 +00:00
|
|
|
`
|
2021-06-06 22:07:29 +00:00
|
|
|
session := &fleet.Session{}
|
2021-09-14 14:44:02 +00:00
|
|
|
err := sqlx.GetContext(ctx, d.reader, session, sqlStatement, key)
|
2016-11-16 13:47:49 +00:00
|
|
|
if err != nil {
|
2021-11-15 14:11:38 +00:00
|
|
|
return nil, ctxerr.Wrap(ctx, err, "selecting sessions")
|
2016-11-16 13:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return session, nil
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
func (d *Datastore) SessionByID(ctx context.Context, id uint) (*fleet.Session, error) {
|
2016-11-16 13:47:49 +00:00
|
|
|
sqlStatement := `
|
|
|
|
SELECT * FROM sessions
|
|
|
|
WHERE id = ?
|
|
|
|
LIMIT 1
|
|
|
|
`
|
2021-06-06 22:07:29 +00:00
|
|
|
session := &fleet.Session{}
|
2021-09-14 14:44:02 +00:00
|
|
|
err := sqlx.GetContext(ctx, d.reader, session, sqlStatement, id)
|
2016-11-16 13:47:49 +00:00
|
|
|
if err != nil {
|
2021-11-15 14:11:38 +00:00
|
|
|
return nil, ctxerr.Wrap(ctx, err, "selecting session by id")
|
2016-11-16 13:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return session, nil
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
func (d *Datastore) ListSessionsForUser(ctx context.Context, id uint) ([]*fleet.Session, error) {
|
2016-11-16 13:47:49 +00:00
|
|
|
sqlStatement := `
|
|
|
|
SELECT * FROM sessions
|
|
|
|
WHERE user_id = ?
|
|
|
|
`
|
2021-06-06 22:07:29 +00:00
|
|
|
sessions := []*fleet.Session{}
|
2021-09-14 14:44:02 +00:00
|
|
|
err := sqlx.SelectContext(ctx, d.reader, &sessions, sqlStatement, id)
|
2016-11-16 13:47:49 +00:00
|
|
|
if err != nil {
|
2021-11-15 14:11:38 +00:00
|
|
|
return nil, ctxerr.Wrap(ctx, err, "selecting sessions for user")
|
2016-11-16 13:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return sessions, nil
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
func (d *Datastore) NewSession(ctx context.Context, session *fleet.Session) (*fleet.Session, error) {
|
2016-11-16 13:47:49 +00:00
|
|
|
sqlStatement := `
|
|
|
|
INSERT INTO sessions (
|
|
|
|
user_id,
|
2016-11-16 23:12:59 +00:00
|
|
|
` + "`key`" + `
|
2016-11-16 13:47:49 +00:00
|
|
|
)
|
2016-11-16 23:12:59 +00:00
|
|
|
VALUES(?,?)
|
2016-11-16 13:47:49 +00:00
|
|
|
`
|
2021-09-14 14:44:02 +00:00
|
|
|
result, err := d.writer.ExecContext(ctx, sqlStatement, session.UserID, session.Key)
|
2016-11-16 13:47:49 +00:00
|
|
|
if err != nil {
|
2021-11-15 14:11:38 +00:00
|
|
|
return nil, ctxerr.Wrap(ctx, err, "inserting session")
|
2016-11-16 13:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
id, _ := result.LastInsertId()
|
|
|
|
session.ID = uint(id)
|
|
|
|
return session, nil
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
func (d *Datastore) DestroySession(ctx context.Context, session *fleet.Session) error {
|
2021-09-20 17:47:06 +00:00
|
|
|
err := d.deleteEntity(ctx, sessionsTable, session.ID)
|
2016-11-16 13:47:49 +00:00
|
|
|
if err != nil {
|
2021-11-15 14:11:38 +00:00
|
|
|
return ctxerr.Wrap(ctx, err, "deleting session")
|
2016-11-16 13:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
func (d *Datastore) DestroyAllSessionsForUser(ctx context.Context, id uint) error {
|
2016-11-16 13:47:49 +00:00
|
|
|
sqlStatement := `
|
|
|
|
DELETE FROM sessions WHERE user_id = ?
|
|
|
|
`
|
2021-09-14 14:44:02 +00:00
|
|
|
_, err := d.writer.ExecContext(ctx, sqlStatement, id)
|
2016-11-16 13:47:49 +00:00
|
|
|
if err != nil {
|
2021-11-15 14:11:38 +00:00
|
|
|
return ctxerr.Wrap(ctx, err, "deleting sessions for user")
|
2016-11-16 13:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
func (d *Datastore) MarkSessionAccessed(ctx context.Context, session *fleet.Session) error {
|
2016-11-16 13:47:49 +00:00
|
|
|
sqlStatement := `
|
|
|
|
UPDATE sessions SET
|
|
|
|
accessed_at = ?
|
|
|
|
WHERE id = ?
|
|
|
|
`
|
2021-09-14 14:44:02 +00:00
|
|
|
results, err := d.writer.ExecContext(ctx, sqlStatement, d.clock.Now(), session.ID)
|
2016-11-16 13:47:49 +00:00
|
|
|
if err != nil {
|
2021-11-15 14:11:38 +00:00
|
|
|
return ctxerr.Wrap(ctx, err, "updating mark session as accessed")
|
2016-11-16 13:47:49 +00:00
|
|
|
}
|
2017-03-30 22:03:48 +00:00
|
|
|
rows, err := results.RowsAffected()
|
|
|
|
if err != nil {
|
2021-11-15 14:11:38 +00:00
|
|
|
return ctxerr.Wrap(ctx, err, "rows affected updating mark session accessed")
|
2017-03-30 22:03:48 +00:00
|
|
|
}
|
|
|
|
if rows == 0 {
|
2021-11-15 14:11:38 +00:00
|
|
|
return ctxerr.Wrap(ctx, notFound("Session").WithID(session.ID))
|
2017-03-30 22:03:48 +00:00
|
|
|
}
|
2016-11-16 13:47:49 +00:00
|
|
|
return nil
|
|
|
|
}
|