2016-05-02 21:36:49 -04:00
|
|
|
package trantor
|
2012-08-18 02:06:43 +02:00
|
|
|
|
|
|
|
import (
|
2013-04-22 23:28:00 +02:00
|
|
|
"encoding/hex"
|
2014-08-30 13:17:50 -05:00
|
|
|
"net/http"
|
2020-12-03 15:43:47 +00:00
|
|
|
"os"
|
2014-08-30 13:17:50 -05:00
|
|
|
|
2012-12-04 23:40:46 +01:00
|
|
|
"github.com/gorilla/securecookie"
|
|
|
|
"github.com/gorilla/sessions"
|
2016-05-02 21:36:49 -04:00
|
|
|
"gitlab.com/trantor/trantor/lib/database"
|
2012-08-18 02:06:43 +02:00
|
|
|
)
|
|
|
|
|
2020-12-03 15:43:47 +00:00
|
|
|
var sesStore = sessions.NewCookieStore([]byte(os.Getenv("SESSION_KEY")))
|
2012-08-18 02:06:43 +02:00
|
|
|
|
2012-08-19 02:29:34 +02:00
|
|
|
type Notification struct {
|
|
|
|
Title string
|
|
|
|
Msg string
|
|
|
|
Type string /* error, info or success */
|
2012-08-18 02:06:43 +02:00
|
|
|
}
|
|
|
|
|
2012-08-19 02:29:34 +02:00
|
|
|
type Session struct {
|
2013-09-03 00:03:34 +02:00
|
|
|
User string
|
|
|
|
Role string
|
|
|
|
S *sessions.Session
|
2012-08-19 02:29:34 +02:00
|
|
|
}
|
|
|
|
|
2017-05-21 10:16:16 +00:00
|
|
|
func GetSession(r *http.Request, db database.DB) (s *Session) {
|
2012-08-19 02:29:34 +02:00
|
|
|
s = new(Session)
|
|
|
|
var err error
|
|
|
|
s.S, err = sesStore.Get(r, "session")
|
|
|
|
if err == nil && !s.S.IsNew {
|
|
|
|
s.User, _ = s.S.Values["user"].(string)
|
2016-07-30 07:10:33 -04:00
|
|
|
s.Role, _ = db.GetRole(s.User)
|
2012-08-18 02:06:43 +02:00
|
|
|
}
|
2013-04-22 23:28:00 +02:00
|
|
|
|
|
|
|
if s.S.IsNew {
|
|
|
|
s.S.Values["id"] = hex.EncodeToString(securecookie.GenerateRandomKey(16))
|
|
|
|
}
|
|
|
|
|
2012-08-19 02:29:34 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-09-03 00:03:34 +02:00
|
|
|
func (s *Session) GetNotif() []Notification {
|
|
|
|
session := s.S
|
|
|
|
msgs := session.Flashes("nMsg")
|
|
|
|
titles := session.Flashes("nTitle")
|
|
|
|
tpes := session.Flashes("nType")
|
|
|
|
notif := make([]Notification, len(msgs))
|
|
|
|
for i, m := range msgs {
|
|
|
|
msg, _ := m.(string)
|
|
|
|
title, _ := titles[i].(string)
|
|
|
|
tpe, _ := tpes[i].(string)
|
|
|
|
notif[i] = Notification{title, msg, tpe}
|
|
|
|
}
|
|
|
|
return notif
|
|
|
|
}
|
|
|
|
|
2012-08-19 02:29:34 +02:00
|
|
|
func (s *Session) LogIn(user string) {
|
|
|
|
s.User = user
|
|
|
|
s.S.Values["user"] = user
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) LogOut() {
|
|
|
|
s.S.Values["user"] = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) Notify(title, msg, tpe string) {
|
|
|
|
s.S.AddFlash(msg, "nMsg")
|
|
|
|
s.S.AddFlash(title, "nTitle")
|
|
|
|
s.S.AddFlash(tpe, "nType")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) Save(w http.ResponseWriter, r *http.Request) {
|
|
|
|
sesStore.Save(r, w, s.S)
|
2012-08-18 02:06:43 +02:00
|
|
|
}
|
2013-04-22 23:28:00 +02:00
|
|
|
|
|
|
|
func (s *Session) Id() string {
|
|
|
|
id, _ := s.S.Values["id"].(string)
|
|
|
|
return id
|
|
|
|
}
|
2013-06-01 20:43:23 +02:00
|
|
|
|
|
|
|
func (s *Session) IsAdmin() bool {
|
|
|
|
return s.Role == "admin"
|
|
|
|
}
|
2016-10-16 20:05:45 -04:00
|
|
|
|
|
|
|
func (s *Session) IsModerator() bool {
|
|
|
|
admin := map[string]bool{
|
|
|
|
"admin": true,
|
|
|
|
"moderator": true,
|
|
|
|
}
|
|
|
|
return admin[s.Role]
|
|
|
|
}
|