forked from GoAdminGroup/go-admin
/
menu_edit.go
75 lines (63 loc) · 1.69 KB
/
menu_edit.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
package guard
import (
"html/template"
"strconv"
"github.com/GoAdminGroup/go-admin/context"
"github.com/GoAdminGroup/go-admin/modules/auth"
"github.com/GoAdminGroup/go-admin/modules/errors"
"github.com/GoAdminGroup/go-admin/plugins/admin/modules/form"
)
type MenuEditParam struct {
Id string
Title string
Header string
PluginName string
ParentId int64
Icon string
Uri string
Roles []string
Alert template.HTML
}
func (e MenuEditParam) HasAlert() bool {
return e.Alert != template.HTML("")
}
func (g *Guard) MenuEdit(ctx *context.Context) {
parentId := ctx.FormValue("parent_id")
if parentId == "" {
parentId = "0"
}
var (
parentIdInt, _ = strconv.Atoi(parentId)
token = ctx.FormValue(form.TokenKey)
alert template.HTML
)
if !auth.GetTokenService(g.services.Get(auth.TokenServiceKey)).CheckToken(token) {
alert = getAlert(errors.EditFailWrongToken)
}
if alert == "" {
alert = checkEmpty(ctx, "id", "title", "icon")
}
ctx.SetUserValue(editMenuParamKey, &MenuEditParam{
Id: ctx.FormValue("id"),
Title: ctx.FormValue("title"),
Header: ctx.FormValue("header"),
PluginName: ctx.FormValue("plugin_name"),
ParentId: int64(parentIdInt),
Icon: ctx.FormValue("icon"),
Uri: ctx.FormValue("uri"),
Roles: ctx.Request.Form["roles[]"],
Alert: alert,
})
ctx.Next()
}
func GetMenuEditParam(ctx *context.Context) *MenuEditParam {
return ctx.UserValue[editMenuParamKey].(*MenuEditParam)
}
func checkEmpty(ctx *context.Context, key ...string) template.HTML {
for _, k := range key {
if ctx.FormValue(k) == "" {
return getAlert("wrong " + k)
}
}
return template.HTML("")
}