forked from glennliao/apijson-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccess_config.go
More file actions
executable file
·98 lines (81 loc) · 2.06 KB
/
access_config.go
File metadata and controls
executable file
·98 lines (81 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package config
import (
"net/http"
"github.com/glennliao/apijson-go/consts"
"github.com/gogf/gf/v2/os/gtime"
"github.com/samber/lo"
)
type FieldsGetValue struct {
In map[string][]string
Out map[string]string
MaxCount *int // 可使用的最大分页大小,默认100
}
type AccessConfig struct {
Debug int8
Name string
Alias string
Get []string
Head []string
Gets []string
Heads []string
Post []string
Put []string
Delete []string
CreatedAt *gtime.Time
Detail string
RowKeyGen string // 主键生成策略
RowKey string
FieldsGet map[string]*FieldsGetValue
Executor string
}
func (a *AccessConfig) GetFieldsGetOutByRole(role string) []string {
var fieldsMap map[string]string
if val, exists := a.FieldsGet[role]; exists {
fieldsMap = val.Out
} else {
fieldsMap = a.FieldsGet["default"].Out
}
return lo.Keys(fieldsMap)
}
func (a *AccessConfig) GetFieldsGetInByRole(role string) map[string][]string {
var inFieldsMap map[string][]string
if val, exists := a.FieldsGet[role]; exists {
inFieldsMap = val.In
} else {
inFieldsMap = a.FieldsGet["default"].In
}
return inFieldsMap
}
func (a *Access) GetAccess(accessName string, noVerify bool) (*AccessConfig, error) {
access, ok := a.accessConfigMap[accessName]
if !ok {
if noVerify {
return &AccessConfig{
Debug: 0,
Name: accessName,
Alias: accessName,
}, nil
}
return nil, consts.NewAccessNoFoundErr(accessName)
}
return &access, nil
}
func (a *Access) GetAccessRole(accessName string, method string) ([]string, string, error) {
access, ok := a.accessConfigMap[accessName]
if !ok {
return nil, "", consts.NewAccessNoFoundErr(accessName)
}
switch method {
case http.MethodGet:
return access.Get, access.Name, nil
case http.MethodHead:
return access.Head, access.Name, nil
case http.MethodPost:
return access.Post, access.Name, nil
case http.MethodPut:
return access.Put, access.Name, nil
case http.MethodDelete:
return access.Delete, access.Name, nil
}
return []string{}, access.Name, nil
}