github.com/kotovmak/go-admin@v1.1.1/plugins/admin/modules/guard/menu_new.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 MenuNewParam struct {
    14  	Title      string
    15  	Header     string
    16  	ParentId   int64
    17  	Icon       string
    18  	PluginName string
    19  	Uri        string
    20  	Roles      []string
    21  	Alert      template.HTML
    22  }
    23  
    24  func (e MenuNewParam) HasAlert() bool {
    25  	return e.Alert != template.HTML("")
    26  }
    27  
    28  func (g *Guard) MenuNew(ctx *context.Context) {
    29  
    30  	parentId := ctx.FormValue("parent_id")
    31  	if parentId == "" {
    32  		parentId = "0"
    33  	}
    34  
    35  	var (
    36  		alert template.HTML
    37  		token = ctx.FormValue(form.TokenKey)
    38  	)
    39  
    40  	if !auth.GetTokenService(g.services.Get(auth.TokenServiceKey)).CheckToken(token) {
    41  		alert = getAlert(errors.EditFailWrongToken)
    42  	}
    43  
    44  	if alert == "" {
    45  		alert = checkEmpty(ctx, "title", "icon")
    46  	}
    47  
    48  	parentIdInt, _ := strconv.Atoi(parentId)
    49  
    50  	ctx.SetUserValue(newMenuParamKey, &MenuNewParam{
    51  		Title:      ctx.FormValue("title"),
    52  		Header:     ctx.FormValue("header"),
    53  		PluginName: ctx.FormValue("plugin_name"),
    54  		ParentId:   int64(parentIdInt),
    55  		Icon:       ctx.FormValue("icon"),
    56  		Uri:        ctx.FormValue("uri"),
    57  		Roles:      ctx.Request.Form["roles[]"],
    58  		Alert:      alert,
    59  	})
    60  	ctx.Next()
    61  }
    62  
    63  func GetMenuNewParam(ctx *context.Context) *MenuNewParam {
    64  	return ctx.UserValue[newMenuParamKey].(*MenuNewParam)
    65  }