56 lines
910 B
Go
56 lines
910 B
Go
package models
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"git.1248.nz/1248/Otfe/misc/cookie"
|
|
"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
|
|
}
|