2022-02-23 12:07:09 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2022-03-12 18:06:39 +00:00
|
|
|
"git.technical.kiwi/go/otfe/misc/cookie"
|
2022-02-23 12:07:09 +00:00
|
|
|
"github.com/globalsign/mgo/bson"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
}
|
|
|
|
|
|
|
|
//Session model
|
|
|
|
type Session struct {
|
|
|
|
gorm.Model
|
|
|
|
UserID bson.ObjectId
|
|
|
|
ExpireAt time.Time
|
|
|
|
IP string
|
|
|
|
}
|
|
|
|
|
|
|
|
//Create session and set LoginTime
|
|
|
|
func (s *Session) Create() error {
|
|
|
|
return create(&s)
|
|
|
|
}
|
|
|
|
|
|
|
|
//Read session
|
|
|
|
func (s *Session) Read() error {
|
|
|
|
return read(&s)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//Update LastSeenTime
|
|
|
|
func (s *Session) Update() error {
|
|
|
|
s.UpdatedAt = time.Now()
|
|
|
|
return update(&s)
|
|
|
|
}
|
|
|
|
|
|
|
|
//Delete session
|
|
|
|
func (s *Session) Delete() error {
|
|
|
|
return delete(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
//Get session from http request and check if it is valid
|
|
|
|
func (s *Session) Get(r *http.Request) error {
|
|
|
|
//Read session cookie
|
|
|
|
var err error
|
|
|
|
s.Model.ID, err = cookie.Read(r, "session")
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
err = s.Read()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|