forked from extrame/goblet
/
context_login.go
93 lines (78 loc) · 1.88 KB
/
context_login.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
package goblet
import (
"fmt"
"strconv"
"time"
)
func (c *Context) GetLoginId() (string, bool) {
return c.GetLoginIdAs(USERCOOKIENAME)
}
func (c *Context) GetLoginIdAs(name string) (string, bool) {
cookie, err := c.Server.loginSaver.GetLoginIdAs(c, name)
if cookie != nil && err == nil {
return cookie.Id, true
}
return "", false
}
func (c *Context) GetLoginInfo() (*LoginContext, bool) {
return c.GetLoginInfoAs(USERCOOKIENAME)
}
func (c *Context) GetLoginInfoAs(name string) (*LoginContext, bool) {
cookie, err := c.Server.loginSaver.GetLoginIdAs(c, name)
if cookie != nil && err == nil {
return cookie, true
}
return nil, false
}
func (c *Context) AddLoginIdAs(id interface{}, name string, setter ...LoginInfoSetter) {
if name == "" {
name = "user"
}
var userid string
switch rid := id.(type) {
case string:
userid = rid
case int:
userid = strconv.FormatInt(int64(rid), 10)
case int32:
userid = strconv.FormatInt(int64(rid), 10)
case int64:
userid = strconv.FormatInt(rid, 10)
}
lctx := &LoginContext{
Name: name,
Id: userid,
}
for _, s := range setter {
s(lctx)
}
if lctx.Deadline == nil {
deadline := time.Now().AddDate(0, 0, 1)
lctx.Deadline = &deadline
}
c.Server.loginSaver.AddLoginAs(c, lctx)
}
func (c *Context) AddLoginId(id interface{}, setter ...LoginInfoSetter) {
var userid string
switch rid := id.(type) {
case string:
userid = rid
case int:
userid = strconv.FormatInt(int64(rid), 10)
case int32:
userid = strconv.FormatInt(int64(rid), 10)
case int64:
userid = strconv.FormatInt(rid, 10)
default:
userid = fmt.Sprintf("%s", id)
}
c.AddLoginIdAs(userid, "user", setter...)
}
// Delete the login cookie saved
func (c *Context) DelLogin() error {
return c.DelLoginAs("user")
}
// Delete the login cookie as specified name
func (c *Context) DelLoginAs(name string) error {
return c.Server.loginSaver.DeleteLoginAs(c, name)
}