-
-
Notifications
You must be signed in to change notification settings - Fork 211
/
common_permission.go
200 lines (186 loc) · 5.56 KB
/
common_permission.go
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package role
import (
"encoding/json"
"strings"
"github.com/admpub/copier"
"github.com/admpub/log"
"github.com/admpub/nging/v5/application/library/common"
"github.com/admpub/nging/v5/application/library/perm"
"github.com/admpub/nging/v5/application/registry/navigate"
"github.com/webx-top/com"
"github.com/webx-top/echo"
"github.com/webx-top/echo/defaults"
"github.com/webx-top/echo/param"
)
func NewCommonPermission(d *echo.KVData, c navigate.Checker) *CommonPermission {
return &CommonPermission{
DefinedType: d,
filter: navigate.NewFilter(c),
Combined: map[string]string{},
parsed: map[string]interface{}{},
}
}
type CommonPermission struct {
DefinedType *echo.KVData
Combined map[string]string
parsed map[string]interface{}
filter *navigate.Filter
}
func (r *CommonPermission) Init(roleList []PermissionsGetter) *CommonPermission {
checkeds := map[string]map[string]interface{}{}
seperators := map[string]string{}
ctx := defaults.NewMockContext()
for _, role := range roleList {
for _, rolePerm := range role.GetPermissions() {
if _, ok := checkeds[rolePerm.GetType()]; !ok {
checkeds[rolePerm.GetType()] = map[string]interface{}{}
}
if _, ok := checkeds[rolePerm.GetType()][`*`]; ok {
continue
}
permissionStr := rolePerm.GetPermission()
permissionStr = strings.TrimSpace(permissionStr)
if len(permissionStr) == 0 {
continue
}
if strings.HasPrefix(permissionStr, `{`) && strings.HasSuffix(permissionStr, `}`) {
r.combineJSON(ctx, checkeds, rolePerm.GetType(), permissionStr)
continue
}
for _, permVal := range strings.Split(permissionStr, `,`) {
if _, ok := checkeds[rolePerm.GetType()][permVal]; !ok {
checkeds[rolePerm.GetType()][permVal] = struct{}{}
r.Combined[rolePerm.GetType()] += seperators[rolePerm.GetType()] + permVal
seperators[rolePerm.GetType()] = `,`
}
}
}
}
return r
}
func (r *CommonPermission) combineBehaviorType(ctx echo.Context, checkeds map[string]map[string]interface{}, permType string, permRule string) {
item := r.DefinedType.GetItem(permType)
if item == nil {
return
}
parsed, err := item.X.(*perm.Handle).Parse(ctx, permRule)
//echo.Dump(echo.H{`rule`: permRule, `parsed`: parsed})
if err != nil {
log.Errorf(`failed to parse permission(%s): %v`, permRule, err)
return
}
r.parsed[permType] = parsed
perms, ok := parsed.(perm.BehaviorPerms)
if !ok {
return
}
for permKey, permVal := range perms {
last, ok := checkeds[permType][permKey]
if !ok {
checkeds[permType][permKey] = permVal.Value
} else if combine, ok := last.(Combiner); ok {
lastRecv := combine.Combine(permVal.Value)
checkeds[permType][permKey] = lastRecv
} else {
err = copier.Copy(last, permVal.Value)
if err != nil {
log.Errorf(`failed to copy %#v to %#v: %v`, permVal.Value, last, err)
continue
}
checkeds[permType][permKey] = last
}
}
b, _ := json.Marshal(checkeds[permType])
r.Combined[permType] = string(b)
}
func (r *CommonPermission) combineJSON(ctx echo.Context, checkeds map[string]map[string]interface{}, permType string, permRule string) {
if permType == RolePermissionTypeBehavior {
r.combineBehaviorType(ctx, checkeds, permType, permRule)
return
}
recv := echo.H{}
jsonBytes := com.Str2bytes(permRule)
if err := json.Unmarshal(jsonBytes, &recv); err != nil {
log.Error(common.JSONBytesParseError(err, jsonBytes).Error())
return
}
for permKey, permVal := range recv {
last, ok := checkeds[permType][permKey]
if !ok {
checkeds[permType][permKey] = permVal
} else if lastRecv, ok := last.(echo.H); ok {
for k, v := range param.AsStore(permVal) {
lastRecv[k] = v
}
checkeds[permType][permKey] = lastRecv
}
}
b, _ := json.Marshal(checkeds[permType])
r.Combined[permType] = string(b)
}
func (r *CommonPermission) onceParse(ctx echo.Context, typ string) bool {
if _, ok := r.parsed[typ]; !ok {
item := r.DefinedType.GetItem(typ)
if item == nil {
return false
}
var err error
r.parsed[typ], err = item.X.(*perm.Handle).Parse(ctx, r.Combined[typ])
//echo.Dump(echo.H{`combined`: r.Combined, `parsed`: r.parsed[typ]})
if err != nil {
log.Error(err)
return false
}
}
return true
}
func (r *CommonPermission) Get(ctx echo.Context, typ string) interface{} {
if !r.onceParse(ctx, typ) {
return nil
}
return r.parsed[typ]
}
func (r *CommonPermission) CheckByType(ctx echo.Context, typ string, permPath string) interface{} {
if !r.onceParse(ctx, typ) {
return nil
}
rs, err := perm.HandleCheck(ctx, r.DefinedType, permPath, typ, r.Combined[typ], r.parsed[typ])
if err != nil {
log.Error(err)
}
return rs
}
func (r *CommonPermission) Check(ctx echo.Context, permPath string) bool {
rs := r.CheckByType(ctx, RolePermissionTypePage, permPath)
if rs == nil {
return false
}
if bl, ok := rs.(bool); ok {
return bl
}
return false
}
func (r *CommonPermission) CheckCmd(ctx echo.Context, permPath string) bool {
rs := r.CheckByType(ctx, RolePermissionTypeCommand, permPath)
if rs == nil {
return false
}
if bl, ok := rs.(bool); ok {
return bl
}
return false
}
func (r *CommonPermission) CheckBehavior(ctx echo.Context, permPath string) *perm.CheckedBehavior {
rs := r.CheckByType(ctx, RolePermissionTypeBehavior, permPath)
if rs == nil {
return &perm.CheckedBehavior{}
}
if bv, ok := rs.(*perm.CheckedBehavior); ok {
return bv
}
return &perm.CheckedBehavior{}
}
// FilterNavigate 过滤导航菜单,只显示有权限的菜单
func (r *CommonPermission) FilterNavigate(ctx echo.Context, navList *navigate.List) navigate.List {
return r.filter.FilterNavigate(ctx, navList)
}