github.com/kotovmak/go-admin@v1.1.1/plugins/admin/modules/guard/guard.go (about)

     1  package guard
     2  
     3  import (
     4  	"github.com/kotovmak/go-admin/context"
     5  	"github.com/kotovmak/go-admin/modules/db"
     6  	"github.com/kotovmak/go-admin/modules/errors"
     7  	"github.com/kotovmak/go-admin/modules/service"
     8  	"github.com/kotovmak/go-admin/plugins/admin/modules/constant"
     9  	"github.com/kotovmak/go-admin/plugins/admin/modules/response"
    10  	"github.com/kotovmak/go-admin/plugins/admin/modules/table"
    11  	"github.com/kotovmak/go-admin/template"
    12  	"github.com/kotovmak/go-admin/template/types"
    13  )
    14  
    15  type Guard struct {
    16  	services  service.List
    17  	conn      db.Connection
    18  	tableList table.GeneratorList
    19  	navBtns   *types.Buttons
    20  }
    21  
    22  func New(s service.List, c db.Connection, t table.GeneratorList, b *types.Buttons) *Guard {
    23  	return &Guard{
    24  		services:  s,
    25  		conn:      c,
    26  		tableList: t,
    27  		navBtns:   b,
    28  	}
    29  }
    30  
    31  func (g *Guard) table(ctx *context.Context) (table.Table, string) {
    32  	prefix := ctx.Query(constant.PrefixKey)
    33  	return g.tableList[prefix](ctx), prefix
    34  }
    35  
    36  func (g *Guard) CheckPrefix(ctx *context.Context) {
    37  
    38  	prefix := ctx.Query(constant.PrefixKey)
    39  
    40  	if _, ok := g.tableList[prefix]; !ok {
    41  		if ctx.Headers(constant.PjaxHeader) == "" && ctx.Method() != "GET" {
    42  			response.BadRequest(ctx, errors.Msg)
    43  		} else {
    44  			response.Alert(ctx, errors.Msg, errors.Msg, "table model not found", g.conn, g.navBtns,
    45  				template.Missing404Page)
    46  		}
    47  		ctx.Abort()
    48  		return
    49  	}
    50  
    51  	ctx.Next()
    52  }
    53  
    54  const (
    55  	editFormParamKey    = "edit_form_param"
    56  	deleteParamKey      = "delete_param"
    57  	exportParamKey      = "export_param"
    58  	serverLoginParamKey = "server_login_param"
    59  	deleteMenuParamKey  = "delete_menu_param"
    60  	editMenuParamKey    = "edit_menu_param"
    61  	newMenuParamKey     = "new_menu_param"
    62  	newFormParamKey     = "new_form_param"
    63  	updateParamKey      = "update_param"
    64  	showFormParamKey    = "show_form_param"
    65  	showNewFormParam    = "show_new_form_param"
    66  )