-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcasbin_wrapper.go
66 lines (57 loc) · 1.58 KB
/
casbin_wrapper.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
package organization
import (
"github.com/casbin/casbin/model"
"github.com/casbin/casbin/util"
)
// adapter interface
// LoadPolicy ...
func (org *Organization) LoadPolicy(model model.Model) error {
rs, err := org.AllRoles() // 获取所有的Role
if err != nil {
return err
}
for _, r := range rs {
org.insertNewPolicyByRole(r)
}
return nil
}
// SavePolicy ...
func (org *Organization) SavePolicy(model model.Model) error {
panic(`Organization doesn't support save policy`)
}
//
func (org *Organization) fetchAllowedTypesInRoles(rids []string) []string {
var allowedTypes []string
for _, rid := range rids {
var types []string
for _, policy := range org.enforcer.GetFilteredPolicy(0, rid) {
types = append(types, policy[1])
}
allowedTypes = append(allowedTypes, types...)
}
util.ArrayRemoveDuplicates(&allowedTypes) // 去重
return allowedTypes
}
func (org *Organization) fetchAllRolesByTypeID(tid string) []string {
var rids []string
for _, policy := range org.enforcer.GetFilteredPolicy(1, tid) {
rids = append(rids, policy[0])
}
return rids
}
func (org *Organization) insertNewPolicyByRole(r map[string]interface{}) {
permissionIDs := append(r[`upid`].([]string), r[`ppid`].([]string)...)
searchResult, err := org.PermissionByIDs(permissionIDs)
if err == nil {
id := r[`id`].(string)
for _, p := range searchResult.Data {
types := p[`rbacType`].([]string)
for _, t := range types {
org.enforcer.AddPolicy([]string{id, t, `read`})
}
}
}
}
func (org *Organization) removePolicyByRoleID(rid string) {
org.enforcer.RemoveFilteredPolicy(0, rid)
}