From 357d1e7fc871bfe7ae642d828a6d3a68694678f1 Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 7 May 2022 11:23:58 +0800 Subject: [PATCH] Fix Table Guard bug --- table/table.go | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/table/table.go b/table/table.go index b5c4fce9..5cf15a80 100644 --- a/table/table.go +++ b/table/table.go @@ -24,6 +24,21 @@ import ( // Tables 已载入模型 var Tables = map[string]*Table{} +// apiNames mapping +var apiNames = map[string]string{ + "/:name/search": "search", + "/:name/find/:id": "find", + "/:name/save": "save", + "/:name/delete/:id": "delete", + "/:name/insert": "insert", + "/:name/delete/in": "delete-in", + "/:name/delete/where": "delete-where", + "/:name/update/in": "update-in", + "/:name/update/where": "update-where", + "/:name/quicksave": "quicksave", + "/:name/select": "select", +} + // Guard Table guard func Guard(c *gin.Context) { @@ -35,26 +50,42 @@ func Guard(c *gin.Context) { log.Trace("Table Guard FullPath: %s", c.FullPath()) routes := strings.Split(c.FullPath(), "/") - path := routes[len(routes)-1] + if len(routes) < 4 { + log.Trace("Table Guard Routes: %v", routes) + c.Next() + return + } + + path := "/" + strings.Join(routes[4:], "/") + apiName, has := apiNames[path] + if !has { + log.Trace("Table Guard API Name: %v", path) + c.Next() + return + } + name, has := c.Params.Get("name") if !has { + log.Trace("Table Guard Name: %v", c.Params) c.Next() return } table, has := Tables[name] if !has { + log.Trace("Table Guard Table: %s", name) c.Next() return } - api, has := table.APIs[path] + api, has := table.APIs[apiName] if !has { + log.Trace("Table Guard API: %s", apiName) c.Next() return } - log.Trace("Table Guard: %s %s %s", name, api.Guard, path) + log.Trace("Table Guard: %s %s %s %s", name, api.Guard, path, apiName) if api.Guard == "-" { c.Next()