Skip to content

Commit 0d4646d

Browse files
committed
## Changed
- AccessVerify 改成 NoAccessVerify (禁用权限限制, 默认为否, 如需禁用手动赋值true)
1 parent c303379 commit 0d4646d

File tree

11 files changed

+83
-80
lines changed

11 files changed

+83
-80
lines changed

.github/workflows/todo.yml

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
1-
name: Todo tests
2-
3-
on:
4-
push:
5-
branches: [ "main" ,"dev"]
6-
pull_request:
7-
branches: [ "main", "dev" ]
8-
9-
jobs:
10-
11-
build:
12-
runs-on: ubuntu-latest
13-
services:
14-
mysql:
15-
image: mysql:5.7.37
16-
env:
17-
MYSQL_ROOT_PASSWORD: 'yourpassword'
18-
MYSQL_DATABASE: 'my_apijson'
19-
ports:
20-
- 3306:3306
21-
options: >-
22-
--health-cmd="mysqladmin ping"
23-
--health-interval=10s
24-
--health-timeout=5s
25-
--health-retries=3
26-
steps:
27-
- uses: actions/checkout@v3
28-
29-
- name: Set up Go
30-
uses: actions/setup-go@v3
31-
with:
32-
go-version: 1.18
33-
34-
35-
36-
- name: Test
37-
run: |
38-
39-
mysql -uroot -h 127.0.0.1 --port 3306 -pyourpassword my_apijson < @demo/todo/doc/todo.sql
40-
41-
cd ./@demo/todo
42-
mv config.yaml.example config.yaml
43-
44-
cd tests
45-
go test -v
1+
#name: Todo tests
2+
#
3+
#on:
4+
# push:
5+
# branches: [ "main" ,"dev"]
6+
# pull_request:
7+
# branches: [ "main", "dev" ]
8+
#
9+
#jobs:
10+
#
11+
# build:
12+
# runs-on: ubuntu-latest
13+
# services:
14+
# mysql:
15+
# image: mysql:5.7.37
16+
# env:
17+
# MYSQL_ROOT_PASSWORD: 'yourpassword'
18+
# MYSQL_DATABASE: 'my_apijson'
19+
# ports:
20+
# - 3306:3306
21+
# options: >-
22+
# --health-cmd="mysqladmin ping"
23+
# --health-interval=10s
24+
# --health-timeout=5s
25+
# --health-retries=3
26+
# steps:
27+
# - uses: actions/checkout@v3
28+
#
29+
# - name: Set up Go
30+
# uses: actions/setup-go@v3
31+
# with:
32+
# go-version: 1.18
33+
#
34+
#
35+
#
36+
# - name: Test
37+
# run: |
38+
#
39+
# mysql -uroot -h 127.0.0.1 --port 3306 -pyourpassword my_apijson < @demo/todo/doc/todo.sql
40+
#
41+
# cd ./@demo/todo
42+
# mv config.yaml.example config.yaml
43+
#
44+
# cd tests
45+
# go test -v

action/action.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ type Action struct {
2525
children map[string]*Node
2626
keyNode map[string]*Node
2727

28-
AccessVerify bool
28+
// 关闭 access 权限验证, 默认否
29+
NoAccessVerify bool
30+
// 关闭 request 验证开关, 默认否
31+
NoRequestVerify bool
2932
}
3033

3134
func New(ctx context.Context, method string, req model.Map) *Action {

action/node.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (n *Node) parse(ctx context.Context, method string) error {
9797
return err
9898
}
9999

100-
if n.action.AccessVerify {
100+
if n.action.NoAccessVerify == false {
101101
// 1. 检查权限, 无权限就不用做参数检查了
102102
var accessRoles []string
103103

config/access.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ type AccessCondition func(ctx context.Context, req AccessConditionReq) (g.Map, e
2828
type DefaultRole func(ctx context.Context, req RoleReq) (string, error)
2929

3030
var (
31-
// AccessVerify 是否权限验证
32-
AccessVerify = true
31+
// AccessVerify 是否禁用权限验证
32+
NoAccessVerify = true
3333
// AccessConditionFunc 自定义权限限制条件
3434
AccessConditionFunc AccessCondition
3535
// DefaultRoleFunc 自定义获取节点的默认角色

config/executor/query.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ type QueryExecutor interface {
1515
EmptyResult()
1616
}
1717

18-
type queryExecutorBuilder func(ctx context.Context, accessVerify bool, role string, access *db.Access) (QueryExecutor, error)
18+
type queryExecutorBuilder func(ctx context.Context, noAccessVerify bool, role string, access *db.Access) (QueryExecutor, error)
1919

2020
var queryExecutorBuilderMap = map[string]queryExecutorBuilder{}
2121

2222
func RegQueryExecutor(name string, e queryExecutorBuilder) {
2323
queryExecutorBuilderMap[name] = e
2424
}
2525

26-
func NewQueryExecutor(name string, ctx context.Context, accessVerify bool, role string, access *db.Access) (QueryExecutor, error) {
26+
func NewQueryExecutor(name string, ctx context.Context, noAccessVerify bool, role string, access *db.Access) (QueryExecutor, error) {
2727
if v, exists := queryExecutorBuilderMap[name]; exists {
28-
return v(ctx, accessVerify, role, access)
28+
return v(ctx, noAccessVerify, role, access)
2929
}
30-
return queryExecutorBuilderMap["default"](ctx, accessVerify, role, access)
30+
return queryExecutorBuilderMap["default"](ctx, noAccessVerify, role, access)
3131
}
3232

3333
func QueryExecutorList() []string {

framework/gf_orm/query.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ type SqlExecutor struct {
3232
// 是否最终为空结果, 用于node中中断数据获取
3333
WithEmptyResult bool
3434

35-
accessVerify bool
35+
noAccessVerify bool
3636

3737
access *db.Access
3838
}
3939

40-
func New(ctx context.Context, accessVerify bool, role string, access *db.Access) (executor.QueryExecutor, error) {
40+
func New(ctx context.Context, noAccessVerify bool, role string, access *db.Access) (executor.QueryExecutor, error) {
4141

4242
return &SqlExecutor{
4343
ctx: ctx,
@@ -47,7 +47,7 @@ func New(ctx context.Context, accessVerify bool, role string, access *db.Access)
4747
Order: "",
4848
Group: "",
4949
WithEmptyResult: false,
50-
accessVerify: accessVerify,
50+
noAccessVerify: noAccessVerify,
5151
access: access,
5252
}, nil
5353
}
@@ -67,7 +67,7 @@ func (e *SqlExecutor) ParseCondition(conditions model.MapStrAny, accessVerify bo
6767
case strings.HasSuffix(key, consts.OpRegexp):
6868
e.Where = append(e.Where, []any{key[0 : len(key)-1], consts.SqlRegexp, gconv.String(condition)})
6969

70-
case key == "@raw" && !accessVerify:
70+
case key == consts.Raw && !accessVerify:
7171
e.accessCondition = condition.(model.Map)
7272

7373
default:
@@ -79,7 +79,7 @@ func (e *SqlExecutor) ParseCondition(conditions model.MapStrAny, accessVerify bo
7979
return nil
8080
}
8181

82-
if !e.accessVerify { // 可任意字段搜索
82+
if e.noAccessVerify { // 可任意字段搜索
8383
return nil
8484
}
8585

@@ -302,7 +302,7 @@ func (e *SqlExecutor) column() []string {
302302
}
303303

304304
// 过滤可访问字段
305-
if !e.accessVerify || lo.Contains(outFields, dbStyle(e.ctx, tableName, fieldName)) ||
305+
if e.noAccessVerify || lo.Contains(outFields, dbStyle(e.ctx, tableName, fieldName)) ||
306306
len(outFields) == 0 /* 数据库中未设置, 则看成全部可访问 */ {
307307
fields = append(fields, column)
308308
}

framework/handler/handler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919

2020
func Get(ctx context.Context, req model.Map) (res model.Map, err error) {
2121
q := query.New(ctx, req)
22-
q.AccessVerify = config.AccessVerify
22+
q.NoAccessVerify = config.NoAccessVerify
2323
q.AccessCondition = config.AccessConditionFunc
2424
return q.Result()
2525
}
@@ -30,19 +30,19 @@ func Head(ctx context.Context, req model.Map) (res model.Map, err error) {
3030

3131
func Post(ctx context.Context, req model.Map) (res model.Map, err error) {
3232
act := action.New(ctx, http.MethodPost, req)
33-
act.AccessVerify = config.AccessVerify
33+
act.NoAccessVerify = config.NoAccessVerify
3434
return act.Result()
3535
}
3636

3737
func Put(ctx context.Context, req model.Map) (res model.Map, err error) {
3838
act := action.New(ctx, http.MethodPut, req)
39-
act.AccessVerify = config.AccessVerify
39+
act.NoAccessVerify = config.NoAccessVerify
4040
return act.Result()
4141
}
4242

4343
func Delete(ctx context.Context, req model.Map) (res model.Map, err error) {
4444
act := action.New(ctx, http.MethodDelete, req)
45-
act.AccessVerify = config.AccessVerify
45+
act.NoAccessVerify = config.NoAccessVerify
4646
return act.Result()
4747
}
4848

go.mod

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
module github.com/glennliao/apijson-go
22

3-
go 1.18
4-
53
require (
6-
github.com/glennliao/table-sync v0.0.0-00010101000000-000000000000
7-
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.3.1
8-
github.com/gogf/gf/v2 v2.3.1
4+
github.com/glennliao/table-sync v0.2.0
5+
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.3.2
6+
github.com/gogf/gf/v2 v2.3.2
97
github.com/iancoleman/orderedmap v0.2.0
108
github.com/samber/lo v1.33.0
119
)
1210

13-
replace github.com/glennliao/table-sync => ../table-sync
14-
1511
require (
1612
github.com/BurntSushi/toml v1.1.0 // indirect
1713
github.com/clbanning/mxj/v2 v2.5.5 // indirect
@@ -36,3 +32,5 @@ require (
3632
golang.org/x/text v0.3.8-0.20211105212822-18b340fc7af2 // indirect
3733
gopkg.in/yaml.v3 v3.0.1 // indirect
3834
)
35+
36+
go 1.18

go.sum

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4
1515
github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU=
1616
github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI=
1717
github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU=
18+
github.com/glennliao/table-sync v0.2.0 h1:uxkrgNcDHCA/G27qk00+FqF2a832VVXmFX+/5zRHSkQ=
19+
github.com/glennliao/table-sync v0.2.0/go.mod h1:YgVRcaEqwZMQqjN0fuWqOCAo2vMkfbUE9OSXM6jgiYw=
1820
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
1921
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
2022
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
@@ -24,11 +26,11 @@ github.com/go-redis/redis/v8 v8.11.4/go.mod h1:2Z2wHZXdQpCDXEGzqMockDpNyYvi2l4Px
2426
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
2527
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
2628
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
27-
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.3.1 h1:uSZtDmlTFS51A98KPgZElPigSGDi7PPDN6mxeIXtsLo=
28-
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.3.1/go.mod h1:z+/0qiOwMroAnj5ESuobTv0l5P83rf+XR3r6Fj8WJyk=
29+
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.3.2 h1:BZww6QOFgiN/YvovUtN64sgnq59TIg8vtkG8AL6eSl0=
30+
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.3.2/go.mod h1:z+/0qiOwMroAnj5ESuobTv0l5P83rf+XR3r6Fj8WJyk=
2931
github.com/gogf/gf/v2 v2.0.0/go.mod h1:apktt6TleWtCIwpz63vBqUnw8MX8gWKoZyxgDpXFtgM=
30-
github.com/gogf/gf/v2 v2.3.1 h1:uptCJK47N6KSRwTBnFAqBWYnYa/OXBkZ0OlhO9CK7bQ=
31-
github.com/gogf/gf/v2 v2.3.1/go.mod h1:tsbmtwcAl2chcYoq/fP9W2FZf06aw4i89X34nbSHo9Y=
32+
github.com/gogf/gf/v2 v2.3.2 h1:nlJ0zuDWqFb93/faZmr7V+GADx/lzz5Unz/9x6OJ2u8=
33+
github.com/gogf/gf/v2 v2.3.2/go.mod h1:tsbmtwcAl2chcYoq/fP9W2FZf06aw4i89X34nbSHo9Y=
3234
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
3335
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
3436
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=

query/node.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func newNode(query *Query, key string, path string, nodeReq any) *Node {
109109
} else {
110110
node.Type = NodeTypeStruct // 结构节点下应该必须存在查询节点
111111

112-
if !query.AccessVerify {
112+
if query.NoAccessVerify == false {
113113
if lo.Contains(db.GetTableNameList(), k) {
114114
node.Type = NodeTypeQuery
115115
}
@@ -210,7 +210,7 @@ func (n *Node) parse() {
210210
case NodeTypeQuery:
211211
tableKey := parseTableKey(n.Key, n.Path)
212212

213-
access, err := db.GetAccess(tableKey, n.queryContext.AccessVerify)
213+
access, err := db.GetAccess(tableKey, n.queryContext.NoAccessVerify)
214214
if err != nil {
215215
n.err = err
216216
return
@@ -225,7 +225,7 @@ func (n *Node) parse() {
225225
return
226226
}
227227

228-
if n.queryContext.AccessVerify {
228+
if n.queryContext.NoAccessVerify != false {
229229
has, condition, err := hasAccess(n, tableKey)
230230
if err != nil {
231231
n.err = err
@@ -240,7 +240,7 @@ func (n *Node) parse() {
240240
accessWhereCondition = condition
241241
}
242242

243-
queryExecutor, err := executor.NewQueryExecutor(access.Executor, n.ctx, n.queryContext.AccessVerify, n.role, access)
243+
queryExecutor, err := executor.NewQueryExecutor(access.Executor, n.ctx, n.queryContext.NoAccessVerify, n.role, access)
244244
if err != nil {
245245
n.err = err
246246
return

0 commit comments

Comments
 (0)