+ IsGuard

This commit is contained in:
Max 2021-09-29 00:05:27 +08:00
parent 11cf6ff065
commit e001c13a7c
8 changed files with 45 additions and 12 deletions

View file

@ -20,6 +20,7 @@
"apis": {
"search": {
"process": "models.service.Paginate",
"guard": "-",
"default": [
{
"withs": {

View file

@ -10,7 +10,7 @@ import (
// Guards 服务中间件
var Guards = map[string]gin.HandlerFunc{
"bearer-jwt": bearerJWT, // JWT 限校验
"bearer-jwt": bearerJWT, // JWT
}
// JwtClaims JWT claims
@ -22,6 +22,7 @@ type JwtClaims struct {
jwt.StandardClaims
}
// JWT 鉴权
func bearerJWT(c *gin.Context) {
tokenString := c.Request.Header.Get("Authorization")
if tokenString == "" {
@ -53,4 +54,5 @@ func bearerJWT(c *gin.Context) {
// fmt.Println("bearer-JWT", token.Claims.Valid())
c.JSON(403, gin.H{"code": 403, "message": "无权访问该页面"})
c.Abort()
return
}

View file

@ -3,6 +3,7 @@ package global
import (
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/yaoapp/gou"
"github.com/yaoapp/kun/any"
@ -26,6 +27,7 @@ func TestProcessSearch(t *testing.T) {
},
1,
2,
&gin.Context{},
}
process := gou.NewProcess("xiang.table.Search", args...)
response := table.ProcessSearch(process)

8
go.mod
View file

@ -22,16 +22,16 @@ require (
github.com/robertkrimen/otto v0.0.0-20210927222213-f9375a256948 // indirect
github.com/spf13/cobra v1.2.1 // indirect
github.com/stretchr/testify v1.7.0
github.com/yaoapp/gou v0.0.0-20210928034814-0953968e92c2 // indirect
github.com/yaoapp/gou v0.0.0-20210928160401-c3e8f1c2162c // indirect
github.com/yaoapp/kun v0.6.1
github.com/yaoapp/xun v0.5.2 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/net v0.0.0-20210927181540-4e4d966f7476 // indirect
golang.org/x/net v0.0.0-20210928044308-7d9f5e0b762b // indirect
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect
google.golang.org/genproto v0.0.0-20210927142257-433400c27d05 // indirect
google.golang.org/genproto v0.0.0-20210928142010-c7af6a1a74c9 // indirect
google.golang.org/grpc v1.41.0 // indirect
)
// go env -w GOPRIVATE=github.com/yaoapp/*
// replace github.com/yaoapp/gou => ../gou // gou local
replace github.com/yaoapp/gou => ../gou // gou local

8
go.sum
View file

@ -349,6 +349,10 @@ github.com/yaoapp/gou v0.0.0-20210915011439-017e751ca527 h1:PKHppgOCzzs68jdFp8IW
github.com/yaoapp/gou v0.0.0-20210915011439-017e751ca527/go.mod h1:KwCRZDnEKLCxUNgMQ2acU7WI5zpt5SGOt1sBKqNAiaE=
github.com/yaoapp/gou v0.0.0-20210928034814-0953968e92c2 h1:7ztnn/TeigYF4y43yilMo/OmwwmoUCKueMLhyYoxewU=
github.com/yaoapp/gou v0.0.0-20210928034814-0953968e92c2/go.mod h1:KwCRZDnEKLCxUNgMQ2acU7WI5zpt5SGOt1sBKqNAiaE=
github.com/yaoapp/gou v0.0.0-20210928091159-86d1b2abd60f h1:H2aQcZe/SZCuJ7kXH9BaKZHLLX9iguVmq08jdJA6duM=
github.com/yaoapp/gou v0.0.0-20210928091159-86d1b2abd60f/go.mod h1:KwCRZDnEKLCxUNgMQ2acU7WI5zpt5SGOt1sBKqNAiaE=
github.com/yaoapp/gou v0.0.0-20210928091747-b713e6eeaced h1:VcAXZFcP9AZI8Ou0CucrSlZn1dadgQDeZO6AaxNaqhw=
github.com/yaoapp/gou v0.0.0-20210928091747-b713e6eeaced/go.mod h1:KwCRZDnEKLCxUNgMQ2acU7WI5zpt5SGOt1sBKqNAiaE=
github.com/yaoapp/kun v0.6.1 h1:gbjP5wmAuJLe1RGZ2evH4rPCVWykf0iqbksaHikF/GI=
github.com/yaoapp/kun v0.6.1/go.mod h1:igsTcWDnzpp0HtRN7sBP+XOEN2tzoC5hk2MyoPFs3xA=
github.com/yaoapp/xun v0.5.2 h1:DedZ26FpcXfkLnPKXzJsrbVmpWDRx9adUo8AvOcNT+g=
@ -462,6 +466,8 @@ golang.org/x/net v0.0.0-20210913180222-943fd674d43e h1:+b/22bPvDYt4NPDcy4xAGCmON
golang.org/x/net v0.0.0-20210913180222-943fd674d43e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210927181540-4e4d966f7476 h1:s5hu7bTnLKswvidgtqc4GwsW83m9LZu8UAqzmWOZtI4=
golang.org/x/net v0.0.0-20210927181540-4e4d966f7476/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210928044308-7d9f5e0b762b h1:eB48h3HiRycXNy8E0Gf5e0hv7YT6Kt14L/D73G1fuwo=
golang.org/x/net v0.0.0-20210928044308-7d9f5e0b762b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@ -690,6 +696,8 @@ google.golang.org/genproto v0.0.0-20210909211513-a8c4777a87af h1:aLMMXFYqw01RA6X
google.golang.org/genproto v0.0.0-20210909211513-a8c4777a87af/go.mod h1:eFjDcFEctNawg4eG61bRv87N7iHBWyVhJu7u1kqDUXY=
google.golang.org/genproto v0.0.0-20210927142257-433400c27d05 h1:6/1QShroBaS9sY9NbPquolxRETG6PZhfv8ohdbLieBg=
google.golang.org/genproto v0.0.0-20210927142257-433400c27d05/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
google.golang.org/genproto v0.0.0-20210928142010-c7af6a1a74c9 h1:XTH066D35LyHehRwlYhoK3qA+Hcgvg5xREG4kFQEW1Y=
google.golang.org/genproto v0.0.0-20210928142010-c7af6a1a74c9/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
google.golang.org/grpc v1.8.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=

View file

@ -2,7 +2,9 @@ package table
import (
"fmt"
"strings"
"github.com/gin-gonic/gin"
"github.com/yaoapp/gou"
)
@ -128,3 +130,21 @@ func apiDefaultSetting(table *Table) API {
Process: fmt.Sprintf("tables.%s.Setting", table.Table),
}
}
// IsGuard 鉴权处理程序
func (api API) IsGuard(v interface{}) bool {
c, ok := v.(*gin.Context)
if !ok {
return false
}
guards := strings.Split(api.Guard, ",")
for _, guard := range guards {
guard = strings.TrimSpace(guard)
handler, has := gou.HTTPGuards[guard]
if has {
handler(c)
return c.IsAborted()
}
}
return false
}

View file

@ -19,8 +19,13 @@ func init() {
func ProcessSearch(process *gou.Process) interface{} {
// 参数处理
process.ValidateArgNums(4)
process.ValidateArgNums(5)
name := any.Of(process.Args[0]).String()
table := Select(name)
api := table.APIs["search"]
if api.IsGuard(process.Args[4]) {
return nil
}
param := gou.QueryParam{}
ok := false
@ -31,10 +36,6 @@ func ProcessSearch(process *gou.Process) interface{} {
}
}
// 执行查询器
table := Select(name)
api := table.APIs["search"]
// 查询校验
if strings.ToLower(api.Process) == "xiang.table.search" {
exception.New("循环引用 xiang.table.search", 400).Throw()

View file

@ -3,10 +3,9 @@
"version": "1.0.0",
"description": "数据表格接口API",
"group": "xiang/table",
"guard": "bearer-jwt",
"guard": "in-process",
"paths": [
{
"guard": "-",
"path": "/:name/search",
"method": "GET",
"process": "xiang.table.Search",