/
adapter.go
288 lines (270 loc) · 7.54 KB
/
adapter.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package authorization
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/casbin/casbin/v2/model"
"xorm.io/xorm"
"github.com/yockii/qscore/pkg/logger"
"github.com/yockii/qscore/pkg/util"
)
type adapter struct {
engine *xorm.Engine
}
type CasbinPolicy struct {
Id string `json:"id" xorm:"pk varchar(50)"`
PolicyType int `json:"policyType" xorm:"comment('策略类型 1-p, 2-p2')"` // p p2
SubjectId string `json:"subjectId" xorm:"comment('策略主体ID')"` // 主体
Resource string `json:"resource" xorm:"comment('资源内容')"` // 资源
Action string `json:"action" xorm:"comment('资源使用行为')"` // 方法
Effect int `json:"effect" xorm:"comment('策略行为 1-allow,2-deny')"` // 1 allow / 2 deny
Priority int `json:"priority" xorm:"comment('优先级')"` // 优先级
TenantId string `json:"tenantId" xorm:"comment('租户ID')"`
ResourceId string `json:"resourceId" xorm:"varchar(50) comment('对应的资源ID')"` // 资源ID
}
type CasbinRelationship struct {
Id string `json:"id,omitempty" xorm:"pk varchar(50)"`
RelationType int `json:"relationType,omitempty" xorm:"comment('关系类型 1-g 2-g2')"`
SubjectId string `json:"subjectId,omitempty" xorm:"comment('主体ID')"`
ParentSubjectId string `json:"parentSubjectId,omitempty" xorm:"comment('继承主体ID')"`
TenantId string `json:"tenantId" xorm:"comment('租户ID')"`
}
func NewAdapter(engine *xorm.Engine) (*adapter, error) {
if err := engine.Sync2(CasbinPolicy{}, CasbinRelationship{}); err != nil {
return nil, err
}
return &adapter{engine: engine}, nil
}
func (a *adapter) LoadPolicy(m model.Model) error {
var policies []*CasbinPolicy
if err := a.engine.Find(&policies); err != nil {
return err
}
for _, policy := range policies {
policyType := "p"
if policy.PolicyType != 1 {
policyType = fmt.Sprintf("p%d", policy.PolicyType)
}
effect := "allow"
if policy.Effect == 2 {
effect = "deny"
}
tokens := []string{
policy.SubjectId,
policy.Resource,
policy.Action,
effect,
strconv.Itoa(policy.Priority),
policy.TenantId,
policy.ResourceId,
}
mpp := m["p"][policyType]
mpp.Policy = append(mpp.Policy, tokens)
mpp.PolicyMap[strings.Join(tokens, model.DefaultSep)] = len(mpp.Policy) - 1
}
var relations []*CasbinRelationship
if err := a.engine.Find(&relations); err != nil {
return err
}
for _, relation := range relations {
relationType := "g"
if relation.RelationType != 1 {
relationType = fmt.Sprintf("g%d", relation.RelationType)
}
tokens := []string{
relation.SubjectId,
relation.ParentSubjectId,
relation.TenantId,
}
mgg := m["g"][relationType]
mgg.Policy = append(mgg.Policy, tokens)
mgg.PolicyMap[strings.Join(tokens, model.DefaultSep)] = len(mgg.Policy) - 1
}
return nil
}
// SavePolicy saves all policy rules to the storage.
func (a *adapter) SavePolicy(m model.Model) error {
sess := a.engine.NewSession()
defer sess.Close()
sess.Delete(&CasbinRelationship{})
sess.Delete(&CasbinPolicy{})
var policies []*CasbinPolicy
var roles []*CasbinRelationship
for policyType, ast := range m["p"] {
for _, rule := range ast.Policy {
if len(rule) == 7 {
policy, err := a.parsePolicy(policyType, rule)
if err != nil {
logger.Error("策略规则必须是7个元素的数组", err)
continue
}
policies = append(policies, policy)
}
}
}
for relationType, ast := range m["g"] {
for _, rule := range ast.Policy {
role, err := a.parseRelation(relationType, rule)
if err != nil {
logger.Error("关系规则必须是3个元素的数组", err)
continue
}
roles = append(roles, role)
}
}
_, err := sess.Insert(policies)
if err != nil {
return err
}
_, err = sess.Insert(roles)
if err != nil {
return err
}
return sess.Commit()
}
func (a *adapter) parseRelation(relationType string, rule []string) (*CasbinRelationship, error) {
if len(rule) != 3 {
return nil, errors.New("非法的父子关系规则数量,数量必须是3,实际" + strconv.Itoa(len(rule)))
}
gt := 1
if s := relationType[1:]; s != "" {
gt, _ = strconv.Atoi(relationType[1:])
}
role := &CasbinRelationship{
Id: util.GenerateDatabaseID(),
RelationType: gt,
SubjectId: rule[0],
ParentSubjectId: rule[1],
TenantId: rule[2],
}
return role, nil
}
func (a *adapter) parsePolicy(policyType string, rule []string) (*CasbinPolicy, error) {
if len(rule) != 7 {
return nil, errors.New("invalid policy rule ")
}
pt := 1
if s := policyType[1:]; s != "" {
pt, _ = strconv.Atoi(policyType[1:])
}
effect := 1
if rule[3] == "deny" {
effect = 2
}
priority, _ := strconv.Atoi(rule[4])
policy := &CasbinPolicy{
Id: util.GenerateDatabaseID(),
PolicyType: pt,
SubjectId: rule[0],
Resource: rule[1],
Action: rule[2],
Effect: effect,
Priority: priority,
TenantId: rule[5],
ResourceId: rule[6],
}
return policy, nil
}
// AddPolicy adds a policy rule to the storage.
func (a *adapter) AddPolicy(sec string, ptype string, rule []string) error {
if sec == "p" {
policy, err := a.parsePolicy(ptype, rule)
if err != nil {
return err
}
_, err = a.engine.InsertOne(policy)
return err
} else if sec == "g" {
role, err := a.parseRelation(ptype, rule)
if err != nil {
return err
}
_, err = a.engine.InsertOne(role)
return err
} else {
return errors.New("策略添加的sec非法! ")
}
}
// RemovePolicy removes a policy rule from the storage.
func (a *adapter) RemovePolicy(sec string, ptype string, rule []string) error {
if sec == "p" {
policy, err := a.parsePolicy(ptype, rule)
if err != nil {
return err
}
_, err = a.engine.Delete(&policy)
return err
} else if sec == "g" {
role, err := a.parseRelation(ptype, rule)
if err != nil {
return err
}
_, err = a.engine.Delete(&role)
return err
} else {
return errors.New("要删除的策略入参非法! ")
}
}
// RemoveFilteredPolicy removes policy rules that match the filter from the storage.
func (a *adapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error {
idx := fieldIndex + len(fieldValues)
if sec == "p" {
pt := 1
if s := ptype[1:]; s != "" {
pt, _ = strconv.Atoi(ptype[1:])
}
policy := CasbinPolicy{
PolicyType: pt,
}
if fieldIndex <= 0 && idx > 0 {
policy.SubjectId = fieldValues[0-fieldIndex]
}
if fieldIndex <= 1 && idx > 1 {
policy.Resource = fieldValues[1-fieldIndex]
}
if fieldIndex <= 2 && idx > 2 {
policy.Action = fieldValues[2-fieldIndex]
}
if fieldIndex <= 3 && idx > 3 {
es := fieldValues[3-fieldIndex]
effect := 1
if es == "deny" {
effect = 2
}
policy.Effect = effect
}
if fieldIndex <= 3 && idx > 3 {
policy.Priority, _ = strconv.Atoi(fieldValues[4-fieldIndex])
}
if fieldIndex <= 4 && idx > 4 {
policy.TenantId = fieldValues[5-fieldIndex]
}
if fieldIndex <= 5 && idx > 5 {
policy.ResourceId = fieldValues[6-fieldIndex]
}
_, err := a.engine.Delete(&policy)
return err
} else if sec == "g" {
gt := 1
if s := ptype[1:]; s != "" {
gt, _ = strconv.Atoi(ptype[1:])
}
role := CasbinRelationship{
RelationType: gt,
}
if fieldIndex <= 0 && idx > 0 {
role.SubjectId = fieldValues[0-fieldIndex]
}
if fieldIndex <= 1 && idx > 1 {
role.ParentSubjectId = fieldValues[1-fieldIndex]
}
if fieldIndex <= 2 && idx > 2 {
role.TenantId = fieldValues[2-fieldIndex]
}
_, err := a.engine.Delete(&role)
return err
} else {
return errors.New("要删除的策略入参非法! ")
}
}