github.com/kotovmak/go-admin@v1.1.1/plugins/admin/modules/guard/new.go (about) 1 package guard 2 3 import ( 4 "html/template" 5 "mime/multipart" 6 "strings" 7 8 "github.com/kotovmak/go-admin/context" 9 "github.com/kotovmak/go-admin/modules/auth" 10 "github.com/kotovmak/go-admin/modules/config" 11 "github.com/kotovmak/go-admin/modules/db" 12 "github.com/kotovmak/go-admin/modules/errors" 13 "github.com/kotovmak/go-admin/plugins/admin/modules/constant" 14 "github.com/kotovmak/go-admin/plugins/admin/modules/form" 15 "github.com/kotovmak/go-admin/plugins/admin/modules/parameter" 16 "github.com/kotovmak/go-admin/plugins/admin/modules/table" 17 ) 18 19 type ShowNewFormParam struct { 20 Panel table.Table 21 Prefix string 22 Param parameter.Parameters 23 } 24 25 func (g *Guard) ShowNewForm(ctx *context.Context) { 26 27 panel, prefix := g.table(ctx) 28 29 if !panel.GetCanAdd() { 30 alert(ctx, panel, errors.OperationNotAllow, g.conn, g.navBtns) 31 ctx.Abort() 32 return 33 } 34 35 if panel.GetOnlyInfo() { 36 ctx.Redirect(config.Url("/info/" + prefix)) 37 ctx.Abort() 38 return 39 } 40 41 if panel.GetOnlyDetail() { 42 ctx.Redirect(config.Url("/info/" + prefix + "/detail")) 43 ctx.Abort() 44 return 45 } 46 47 if panel.GetOnlyUpdateForm() { 48 ctx.Redirect(config.Url("/info/" + prefix + "/edit")) 49 ctx.Abort() 50 return 51 } 52 53 ctx.SetUserValue(showNewFormParam, &ShowNewFormParam{ 54 Panel: panel, 55 Prefix: prefix, 56 Param: parameter.GetParam(ctx.Request.URL, panel.GetInfo().DefaultPageSize, panel.GetInfo().SortField, 57 panel.GetInfo().GetSort()), 58 }) 59 ctx.Next() 60 } 61 62 func GetShowNewFormParam(ctx *context.Context) *ShowNewFormParam { 63 return ctx.UserValue[showNewFormParam].(*ShowNewFormParam) 64 } 65 66 type NewFormParam struct { 67 Panel table.Table 68 Id string 69 Prefix string 70 Param parameter.Parameters 71 Path string 72 MultiForm *multipart.Form 73 PreviousPath string 74 FromList bool 75 IsIframe bool 76 IframeID string 77 Alert template.HTML 78 } 79 80 func (e NewFormParam) Value() form.Values { 81 return e.MultiForm.Value 82 } 83 84 func (g *Guard) NewForm(ctx *context.Context) { 85 86 var ( 87 previous = ctx.FormValue(form.PreviousKey) 88 panel, prefix = g.table(ctx) 89 conn = db.GetConnection(g.services) 90 token = ctx.FormValue(form.TokenKey) 91 ) 92 93 if !auth.GetTokenService(g.services.Get(auth.TokenServiceKey)).CheckToken(token) { 94 alert(ctx, panel, errors.CreateFailWrongToken, conn, g.navBtns) 95 ctx.Abort() 96 return 97 } 98 99 fromList := isInfoUrl(previous) 100 param := parameter.GetParamFromURL(previous, panel.GetInfo().DefaultPageSize, 101 panel.GetInfo().GetSort(), panel.GetPrimaryKey().Name) 102 103 if fromList { 104 previous = config.Url("/info/" + prefix + param.GetRouteParamStr()) 105 } 106 107 values := ctx.Request.MultipartForm.Value 108 109 ctx.SetUserValue(newFormParamKey, &NewFormParam{ 110 Panel: panel, 111 Id: "", 112 Prefix: prefix, 113 Param: param, 114 IsIframe: form.Values(values).Get(constant.IframeKey) == "true", 115 IframeID: form.Values(values).Get(constant.IframeIDKey), 116 Path: strings.Split(previous, "?")[0], 117 MultiForm: ctx.Request.MultipartForm, 118 PreviousPath: previous, 119 FromList: fromList, 120 }) 121 ctx.Next() 122 } 123 124 func GetNewFormParam(ctx *context.Context) *NewFormParam { 125 return ctx.UserValue[newFormParamKey].(*NewFormParam) 126 }