forked from GoAdminGroup/go-admin
/
middleware.go
184 lines (145 loc) · 3.82 KB
/
middleware.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
// Copyright 2019 cg33. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package auth
import (
"fmt"
"github.com/chenhg5/go-admin/context"
"github.com/chenhg5/go-admin/modules/config"
"github.com/chenhg5/go-admin/modules/language"
"github.com/chenhg5/go-admin/modules/page"
"github.com/chenhg5/go-admin/plugins/admin/models"
template2 "github.com/chenhg5/go-admin/template"
"github.com/chenhg5/go-admin/template/types"
"html/template"
"regexp"
"strings"
)
type Invoker struct {
prefix string
authFailCallback MiddlewareCallback
permissionDenyCallback MiddlewareCallback
}
var Middleware = DefaultInvoker().Middleware()
func DefaultInvoker() *Invoker {
return &Invoker{
prefix: config.Get().Prefix(),
authFailCallback: func(ctx *context.Context) {
ctx.Write(302, map[string]string{
"Location": config.Get().Url("/login"),
}, ``)
},
permissionDenyCallback: func(ctx *context.Context) {
page.SetPageContent(ctx, Auth(ctx), func() types.Panel {
alert := template2.Get(config.Get().Theme).Alert().
SetTitle(template.HTML(`<i class="icon fa fa-warning"></i> ` + language.Get("error") + `!`)).
SetTheme("warning").SetContent(template.HTML("permission denied")).GetContent()
return types.Panel{
Content: alert,
Description: "Error",
Title: "Error",
}
})
},
}
}
func SetPrefix(prefix string) *Invoker {
i := DefaultInvoker()
i.prefix = prefix
return i
}
func (invoker *Invoker) SetAuthFailCallback(callback MiddlewareCallback) *Invoker {
invoker.authFailCallback = callback
return invoker
}
func (invoker *Invoker) SetPermissionDenyCallback(callback MiddlewareCallback) *Invoker {
invoker.permissionDenyCallback = callback
return invoker
}
type MiddlewareCallback func(ctx *context.Context)
func (invoker *Invoker) Middleware() context.Handler {
return func(ctx *context.Context) {
user, authOk, permissionOk := Filter(ctx)
if authOk && permissionOk {
ctx.SetUserValue("user", user)
ctx.Next()
return
}
if !authOk {
invoker.authFailCallback(ctx)
ctx.Abort()
return
}
if !permissionOk {
ctx.SetUserValue("user", user)
invoker.permissionDenyCallback(ctx)
ctx.Abort()
return
}
}
}
func Filter(ctx *context.Context) (models.UserModel, bool, bool) {
var (
id float64
ok bool
user = models.User()
)
if id, ok = InitSession(ctx).Get("user_id").(float64); !ok {
return user, false, false
}
user, ok = GetCurUserById(int64(id))
if !ok {
return user, false, false
}
return user, true, CheckPermissions(user, ctx.Path(), ctx.Method())
}
func GetCurUserById(id int64) (user models.UserModel, ok bool) {
user = models.User().Find(id)
if user.IsEmpty() {
ok = false
return
}
if user.Avatar == "" || config.Get().Store.Prefix == "" {
user.Avatar = ""
} else {
user.Avatar = "/" + config.Get().Store.Prefix + "/" + user.Avatar
}
user = user.WithRoles().WithPermissions().WithMenus()
ok = true
return
}
func CheckPermissions(user models.UserModel, path string, method string) bool {
if path == config.Get().Url("/logout") {
return true
}
for _, v := range user.Permissions {
if v.HttpMethod[0] == "" || InMethodArr(v.HttpMethod, method) {
if v.HttpPath[0] == "*" {
return true
}
for i := 0; i < len(v.HttpPath); i++ {
matchPath := config.Get().Url(strings.TrimSpace(v.HttpPath[i]))
if matchPath == path {
return true
}
reg, err := regexp.Compile(matchPath)
if err != nil {
fmt.Println("err", err)
continue
}
if reg.FindString(path) == path {
return true
}
}
}
}
return false
}
func InMethodArr(arr []string, str string) bool {
for i := 0; i < len(arr); i++ {
if strings.EqualFold(arr[i], str) {
return true
}
}
return false
}