-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpolicy.go
More file actions
140 lines (127 loc) · 3.58 KB
/
Copy pathpolicy.go
File metadata and controls
140 lines (127 loc) · 3.58 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
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
package authorize
import (
"log"
"github.com/casbin/casbin/v2"
"github.com/casbin/casbin/v2/model"
xormadapter "github.com/casbin/xorm-adapter/v2"
)
// CasbinRule casbin policy rule
type CasbinRule struct {
PType string `xorm:"varchar(100) index not null default ''" json:"pType"`
V0 string `xorm:"varchar(100) index not null default ''" json:"v0"`
V1 string `xorm:"varchar(100) index not null default ''" json:"v1"`
V2 string `xorm:"varchar(100) index not null default ''" json:"v2"`
V3 string `xorm:"varchar(100) index not null default ''" json:"v3"`
V4 string `xorm:"varchar(100) index not null default ''" json:"v4"`
V5 string `xorm:"varchar(100) index not null default ''" json:"v5"`
}
func safeReturn(policy []string, i int) string {
if len(policy) > i {
return policy[i]
}
return ""
}
func matrixToCasbinRules(pType string, policies [][]string) []*CasbinRule {
res := []*CasbinRule{}
for _, policy := range policies {
line := CasbinRule{
PType: pType,
V0: safeReturn(policy, 0),
V1: safeReturn(policy, 1),
V2: safeReturn(policy, 2),
V3: safeReturn(policy, 3),
V4: safeReturn(policy, 4),
V5: safeReturn(policy, 5),
}
res = append(res, &line)
}
return res
}
func getEnforcer(adapter *Adapter) (*casbin.Enforcer, error) {
log.Printf("getting enforcer for %v", adapter)
a, err := xormadapter.NewAdapter(adapter.Param1, adapter.Param2, true)
if err != nil {
return nil, err
}
m, err := model.NewModelFromFile("casbin/model.conf")
if err != nil {
return nil, err
}
e, err := casbin.NewEnforcer(m, a)
return e, err
}
// GetAdapterPolicies get casbin rules of an adapter
func GetAdapterPolicies(id string) (bool, string, []*CasbinRule) {
a := GetAdapter(id)
log.Printf("got adapter %v", a.ID)
e, err := getEnforcer(a)
log.Printf("got enforcer for %v", a.ID)
if err != nil {
return false, err.Error(), nil
}
log.Printf("getting policies for %v", e)
p := e.GetPolicy()
log.Printf("got policies: %v", p)
return true, "", matrixToCasbinRules("p", p)
}
// GetAdapterGroupingPolicies get casbin group rules of an adapter
func GetAdapterGroupingPolicies(id string) (bool, string, []*CasbinRule) {
a := GetAdapter(id)
e, err := getEnforcer(a)
if err != nil {
return false, err.Error(), nil
}
return true, "", matrixToCasbinRules("g", e.GetGroupingPolicy())
}
// SetAdapterAllPolicies initialize adapter with its rules
func SetAdapterAllPolicies(id string, policies []*CasbinRule) (bool, string) {
a := GetAdapter(id)
e, err := getEnforcer(a)
if err != nil {
return false, err.Error()
}
e.ClearPolicy()
for _, policy := range policies {
if policy.PType == "p" {
_, err = e.AddPolicy(
policy.V0, policy.V1, policy.V2, policy.V3, policy.V4, policy.V5)
} else if policy.PType == "g" {
_, err = e.AddGroupingPolicy(
policy.V0, policy.V1, policy.V2, policy.V3, policy.V4, policy.V5)
}
if err != nil {
return false, err.Error()
}
}
err = e.SavePolicy()
if err != nil {
return false, err.Error()
}
return true, ""
}
// AddAdapterPolicy add new rule
func AddAdapterPolicy(id string, policy ...string) (bool, string) {
a := GetAdapter(id)
e, err := getEnforcer(a)
if err != nil {
return false, err.Error()
}
_, err = e.AddPolicy(policy)
if err != nil {
return false, err.Error()
}
return true, ""
}
// RemoveAdapterPolicy remove given rule
func RemoveAdapterPolicy(id string, policy ...string) (bool, string) {
a := GetAdapter(id)
e, err := getEnforcer(a)
if err != nil {
return false, err.Error()
}
_, err = e.RemovePolicy(policy)
if err != nil {
return false, err.Error()
}
return true, ""
}