-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.go
248 lines (206 loc) · 3.86 KB
/
auth.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package auth
import (
"net/http"
"github.com/94peter/sterna/dao"
"github.com/94peter/sterna/util"
)
type UserPerm string
func (up UserPerm) Validate() bool {
switch up {
case PermAdmin, PermOwner, PermEditor, PermViewer, PermGuest:
return true
default:
return false
}
}
const (
// 管理者
PermAdmin = UserPerm("admin")
// 會員
PermMember = UserPerm("member")
// 擁有
PermOwner = UserPerm("owner")
// 編輯
PermEditor = UserPerm("editor")
// 檢視
PermViewer = UserPerm("viewer")
// 訪客
PermGuest = UserPerm("guest")
)
const (
CtxUserInfoKey = util.CtxKey("userInfo")
)
type ReqUser interface {
dao.LogUser
Host() string
GetId() string
GetPerm() []string
GetDB() string
}
type reqUserImpl struct {
host string
id string
acc string
name string
perm []string
}
func (ru *reqUserImpl) Host() string {
return ru.host
}
func (ru reqUserImpl) GetId() string {
return ru.id
}
func (ru reqUserImpl) GetDB() string {
// ReqUser無userDB
return ""
}
func (ru reqUserImpl) GetName() string {
return ru.name
}
func (ru reqUserImpl) GetAccount() string {
return ru.acc
}
func (ru reqUserImpl) GetPerm() []string {
return ru.perm
}
func NewReqUser(host, uid, acc, name string, perm []string) ReqUser {
return &reqUserImpl{
host: host,
acc: acc,
id: uid,
name: name,
perm: perm,
}
}
type AccessGuest interface {
ReqUser
GetSource() string
GetSourceID() string
}
type accessGuestImpl struct {
host string
source string
sourceID string
dB string
account string
name string
perm []string
}
func (ru *accessGuestImpl) Host() string {
return ru.host
}
func (ru *accessGuestImpl) GetId() string {
return ""
}
func (ru *accessGuestImpl) GetDB() string {
return ru.dB
}
func (ru *accessGuestImpl) GetName() string {
return ru.name
}
func (ru *accessGuestImpl) GetAccount() string {
return ru.account
}
func (ru *accessGuestImpl) GetSource() string {
return ru.source
}
func (ru *accessGuestImpl) GetSourceID() string {
return ru.sourceID
}
func (ru *accessGuestImpl) GetPerm() []string {
return ru.perm
}
func NewAccessGuest(host, source, sid, acc, name, db string, perm []string) AccessGuest {
return &accessGuestImpl{
host: host,
source: source,
sourceID: sid,
dB: db,
account: acc,
name: name,
perm: perm,
}
}
type CompanyUser interface {
ReqUser
GetCompID() string
GetComp() string
}
type compUserImpl struct {
*reqUserImpl
CompID string
Comp string
}
func (c compUserImpl) GetDB() string {
return c.CompID
}
func (c compUserImpl) GetCompID() string {
return c.CompID
}
func (c compUserImpl) GetComp() string {
return c.Comp
}
func NewCompUser(host, uid, acc, name, compID, comp string, perm []string) CompanyUser {
return compUserImpl{
reqUserImpl: &reqUserImpl{
host: host,
acc: acc,
id: uid,
name: name,
perm: perm,
},
CompID: compID,
Comp: comp,
}
}
type guestUser struct {
host string
ip string
}
func NewGuestUser(host, ip string) ReqUser {
return &guestUser{
host: host,
ip: ip,
}
}
func (ru *guestUser) Host() string {
return ru.host
}
func (ru *guestUser) GetId() string {
return ru.ip
}
func (ru *guestUser) GetDB() string {
// ReqUser無userDB
return ""
}
func (ru *guestUser) GetName() string {
return ru.ip
}
func (ru *guestUser) GetAccount() string {
return ru.ip
}
func (ru *guestUser) GetPerm() []string {
return []string{string(PermGuest)}
}
func GetUserInfo(req *http.Request) ReqUser {
ctx := req.Context()
reqID := ctx.Value(CtxUserInfoKey)
if ret, ok := reqID.(ReqUser); ok {
return ret
}
if ret, ok := reqID.(AccessGuest); ok {
return ret
}
if ret, ok := reqID.(CompanyUser); ok {
return ret
}
return nil
}
func GetCompUserInfo(req *http.Request) CompanyUser {
ctx := req.Context()
reqID := ctx.Value(CtxUserInfoKey)
if ret, ok := reqID.(CompanyUser); ok {
return ret
}
return nil
}