forked from GoAdminGroup/go-admin
/
session.go
129 lines (110 loc) · 3.12 KB
/
session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package auth
import (
"encoding/json"
"github.com/chenhg5/go-admin/context"
"github.com/chenhg5/go-admin/modules/config"
"github.com/chenhg5/go-admin/modules/connections"
"github.com/chenhg5/go-admin/plugins/admin/modules"
"net/http"
"time"
)
var (
Driver MysqlDriver
)
type PersistenceDriver interface {
Load(string) map[string]interface{}
Update(sid string, values map[string]interface{})
}
type SessionInterface interface {
Get(string) interface{}
Set(string, interface{})
UseDatabase(PersistenceDriver)
StartCtx(*context.Context) Session
}
type Session struct {
Expires time.Duration
Cookie string
Values map[string]interface{}
Driver PersistenceDriver
Sid string
Context *context.Context
}
type Config struct {
Expires time.Duration
Cookie string
}
func (ses *Session) UpdateConfig(config Config) {
ses.Expires = config.Expires
ses.Cookie = config.Cookie
}
func (ses *Session) Get(key string) interface{} {
return ses.Values[key]
}
func (ses *Session) Set(key string, value interface{}) {
ses.Values[key] = value
ses.Driver.Update(ses.Sid, ses.Values)
cookie := http.Cookie{
Name: ses.Cookie,
Value: ses.Sid,
Domain: config.Get().DOMAIN,
Expires: time.Now().Add(ses.Expires),
HttpOnly: false,
}
ses.Context.SetCookie(&cookie)
}
func (ses *Session) Clear() {
ses.Values = map[string]interface{}{}
ses.Driver.Update(ses.Sid, ses.Values)
}
func (ses *Session) UseDatabase(driver PersistenceDriver) {
ses.Driver = driver
}
func (ses *Session) StartCtx(ctx *context.Context) *Session {
if cookie, err := ctx.Request.Cookie(ses.Cookie); err == nil && cookie.Value != "" {
ses.Sid = cookie.Value
valueFromDriver := ses.Driver.Load(cookie.Value)
if len(valueFromDriver) > 0 {
ses.Values = valueFromDriver
}
} else {
ses.Sid = modules.Uuid(15)
}
ses.Context = ctx
return ses
}
func InitSession(ctx *context.Context) *Session {
sessions := new(Session)
sessions.UpdateConfig(Config{
Expires: time.Hour * 10,
Cookie: "go_admin_session",
})
sessions.UseDatabase(&Driver)
sessions.Values = make(map[string]interface{}, 0)
return sessions.StartCtx(ctx)
}
type MysqlDriver struct{}
func (driver *MysqlDriver) Load(sid string) map[string]interface{} {
sesModel, _ := connections.GetConnection().Query("select * from goadmin_session where sid = ?", sid)
if len(sesModel) < 1 {
return map[string]interface{}{}
} else {
var values map[string]interface{}
json.Unmarshal([]byte(sesModel[0]["values"].(string)), &values)
return values
}
}
func (driver *MysqlDriver) Update(sid string, values map[string]interface{}) {
if sid != "" {
if len(values) == 0 {
connections.GetConnection().Exec("delete from goadmin_session where sid = ?", sid)
return
}
valuesByte, _ := json.Marshal(values)
sesModel, _ := connections.GetConnection().Query("select * from goadmin_session where sid = ?", sid)
if len(sesModel) < 1 {
connections.GetConnection().Exec("insert into goadmin_session (`values`, sid) values (?, ?)", string(valuesByte), sid)
} else {
connections.GetConnection().Exec("update goadmin_session set `values` = ? where sid = ?", string(valuesByte), sid)
}
}
}