github.com/kotovmak/go-admin@v1.1.1/plugins/admin/modules/guard/menu_edit.go (about) 1 package guard 2 3 import ( 4 "html/template" 5 "strconv" 6 7 "github.com/kotovmak/go-admin/context" 8 "github.com/kotovmak/go-admin/modules/auth" 9 "github.com/kotovmak/go-admin/modules/errors" 10 "github.com/kotovmak/go-admin/plugins/admin/modules/form" 11 ) 12 13 type MenuEditParam struct { 14 Id string 15 Title string 16 Header string 17 PluginName string 18 ParentId int64 19 Icon string 20 Uri string 21 Roles []string 22 Alert template.HTML 23 } 24 25 func (e MenuEditParam) HasAlert() bool { 26 return e.Alert != template.HTML("") 27 } 28 29 func (g *Guard) MenuEdit(ctx *context.Context) { 30 31 parentId := ctx.FormValue("parent_id") 32 if parentId == "" { 33 parentId = "0" 34 } 35 36 var ( 37 parentIdInt, _ = strconv.Atoi(parentId) 38 token = ctx.FormValue(form.TokenKey) 39 alert template.HTML 40 ) 41 42 if !auth.GetTokenService(g.services.Get(auth.TokenServiceKey)).CheckToken(token) { 43 alert = getAlert(errors.EditFailWrongToken) 44 } 45 46 if alert == "" { 47 alert = checkEmpty(ctx, "id", "title", "icon") 48 } 49 50 ctx.SetUserValue(editMenuParamKey, &MenuEditParam{ 51 Id: ctx.FormValue("id"), 52 Title: ctx.FormValue("title"), 53 Header: ctx.FormValue("header"), 54 PluginName: ctx.FormValue("plugin_name"), 55 ParentId: int64(parentIdInt), 56 Icon: ctx.FormValue("icon"), 57 Uri: ctx.FormValue("uri"), 58 Roles: ctx.Request.Form["roles[]"], 59 Alert: alert, 60 }) 61 ctx.Next() 62 } 63 64 func GetMenuEditParam(ctx *context.Context) *MenuEditParam { 65 return ctx.UserValue[editMenuParamKey].(*MenuEditParam) 66 } 67 68 func checkEmpty(ctx *context.Context, key ...string) template.HTML { 69 for _, k := range key { 70 if ctx.FormValue(k) == "" { 71 return getAlert("wrong " + k) 72 } 73 } 74 return template.HTML("") 75 }