/
guard.go
66 lines (57 loc) · 1.77 KB
/
guard.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
package guard
import (
"github.com/MarErm27/GoAdmin/context"
"github.com/MarErm27/GoAdmin/modules/db"
"github.com/MarErm27/GoAdmin/modules/errors"
"github.com/MarErm27/GoAdmin/modules/service"
"github.com/MarErm27/GoAdmin/plugins/admin/modules/constant"
"github.com/MarErm27/GoAdmin/plugins/admin/modules/response"
"github.com/MarErm27/GoAdmin/plugins/admin/modules/table"
"github.com/MarErm27/GoAdmin/template"
"github.com/MarErm27/GoAdmin/template/types"
)
type Guard struct {
services service.List
conn db.Connection
tableList table.GeneratorList
navBtns *types.Buttons
}
func New(s service.List, c db.Connection, t table.GeneratorList, b *types.Buttons) *Guard {
return &Guard{
services: s,
conn: c,
tableList: t,
navBtns: b,
}
}
func (g *Guard) table(ctx *context.Context) (table.Table, string) {
prefix := ctx.Query(constant.PrefixKey)
return g.tableList[prefix](ctx), prefix
}
func (g *Guard) CheckPrefix(ctx *context.Context) {
prefix := ctx.Query(constant.PrefixKey)
if _, ok := g.tableList[prefix]; !ok {
if ctx.Headers(constant.PjaxHeader) == "" && ctx.Method() != "GET" {
response.BadRequest(ctx, errors.Msg)
} else {
response.Alert(ctx, errors.Msg, errors.Msg, "table model not found", g.conn, g.navBtns,
template.Missing404Page)
}
ctx.Abort()
return
}
ctx.Next()
}
const (
editFormParamKey = "edit_form_param"
deleteParamKey = "delete_param"
exportParamKey = "export_param"
serverLoginParamKey = "server_login_param"
deleteMenuParamKey = "delete_menu_param"
editMenuParamKey = "edit_menu_param"
newMenuParamKey = "new_menu_param"
newFormParamKey = "new_form_param"
updateParamKey = "update_param"
showFormParamKey = "show_form_param"
showNewFormParam = "show_new_form_param"
)