-
Notifications
You must be signed in to change notification settings - Fork 5
/
rethinkadapter.go
256 lines (217 loc) · 5.77 KB
/
rethinkadapter.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
package rethinkadapter
import (
"fmt"
"runtime"
"github.com/casbin/casbin/model"
"github.com/casbin/casbin/persist"
r "gopkg.in/rethinkdb/rethinkdb-go.v5"
)
// adapter represents the RethinkDB adapter for policy storage.
type adapter struct {
session r.QueryExecutor
database string
table string
}
type policy struct {
ID string `gorethink:"id,omitempty"`
PTYPE string `gorethink:"ptype"`
V1 string `gorethink:"v1"`
V2 string `gorethink:"v2"`
V3 string `gorethink:"v3"`
V4 string `gorethink:"v4"`
V5 string `gorethink:"v5"`
}
func finalizer(a *adapter) {
a.close()
}
// NewAdapter is the constructor for adapter with default database and table name
func NewAdapter(Sessionvar r.QueryExecutor) persist.Adapter {
a := &adapter{session: Sessionvar, database: "casbin", table: "rethinkdbpolicy"}
a.open()
// Call the destructor when the object is released.
runtime.SetFinalizer(a, finalizer)
return a
}
// NewAdapterDB is the constructor for adapter.
func NewAdapterDB(Sessionvar r.QueryExecutor, database, table string) persist.Adapter {
a := &adapter{session: Sessionvar, database: database, table: table}
a.open()
// Call the destructor when the object is released.
runtime.SetFinalizer(a, finalizer)
return a
}
// GetDatabaseName returns the name of the database that the adapter will use
func (a *adapter) GetDatabaseName() string {
return a.database
}
// GetTableName returns the name of the table that the adapter will use
func (a *adapter) GetTableName() string {
return a.database
}
// SetDatabaseName sets the database that the adapter will use
func (a *adapter) SetDatabaseName(s string) {
a.database = s
}
// SetTableName sets the tablet that the adapter will use
func (a *adapter) SetTableName(s string) {
a.table = s
}
func (a *adapter) close() {
a.session = nil
}
func (a *adapter) createDatabase() error {
_, err := r.DBList().Contains(a.database).Do(r.DBCreate(a.database).Exec(a.session)).Run(a.session)
if err != nil {
return err
}
return nil
}
func (a *adapter) createTable() error {
_, err := r.DB(a.database).TableList().Contains(a.table).Do(r.DB(a.database).TableCreate(a.table).Exec(a.session)).Run(a.session)
if err != nil {
return err
}
return nil
}
func (a *adapter) open() {
if err := a.createDatabase(); err != nil {
panic(err)
}
if err := a.createTable(); err != nil {
panic(err)
}
}
//Erase the table data
func (a *adapter) dropTable() error {
_, err := r.DB(a.database).Table(a.table).Delete().Run(a.session)
if err != nil {
panic(err)
}
return nil
}
func loadPolicyLine(line policy, model model.Model) {
if line.PTYPE == "" {
return
}
key := line.PTYPE
sec := key[:1]
tokens := []string{}
if line.V1 != "" {
tokens = append(tokens, line.V1)
}
if line.V2 != "" {
tokens = append(tokens, line.V2)
}
if line.V3 != "" {
tokens = append(tokens, line.V3)
}
if line.V4 != "" {
tokens = append(tokens, line.V4)
}
if line.V5 != "" {
tokens = append(tokens, line.V5)
}
model[sec][key].Policy = append(model[sec][key].Policy, tokens)
}
// LoadPolicy loads policy from database.
func (a *adapter) LoadPolicy(model model.Model) error {
a.open()
rows, errn := r.DB(a.database).Table(a.table).Run(a.session)
if errn != nil {
fmt.Printf("E: %v\n", errn)
return errn
}
defer rows.Close()
var output policy
for rows.Next(&output) {
loadPolicyLine(output, model)
}
return nil
}
func (a *adapter) writeTableLine(ptype string, rule []string) policy {
items := policy{
PTYPE: ptype,
} //map[string]string{"PTYPE": ptype, "V1": "", "V2": "", "V3": "", "V4": ""}
for i := 0; i < len(rule); i++ {
switch i {
case 0:
items.V1 = rule[i]
case 1:
items.V2 = rule[i]
case 2:
items.V3 = rule[i]
case 3:
items.V4 = rule[i]
case 4:
items.V5 = rule[i]
}
}
return items
}
// SavePolicy saves policy to database.
func (a *adapter) SavePolicy(model model.Model) error {
a.open()
a.dropTable()
var lines []policy
for PTYPE, ast := range model["p"] {
for _, rule := range ast.Policy {
line := a.writeTableLine(PTYPE, rule)
lines = append(lines, line)
}
}
for PTYPE, ast := range model["g"] {
for _, rule := range ast.Policy {
line := a.writeTableLine(PTYPE, rule)
lines = append(lines, line)
}
}
_, err := r.DB(a.database).Table(a.table).Insert(lines).Run(a.session)
if err != nil {
fmt.Printf("Error: %v\n", err)
return err
}
return nil
}
//AddPolicy for adding a new policy to rethinkdb
func (a *adapter) AddPolicy(sec string, PTYPE string, policys []string) error {
line := a.writeTableLine(PTYPE, policys)
_, err := r.DB(a.database).Table(a.table).Insert(line).Run(a.session)
if err != nil {
return err
}
return nil
}
//RemovePolicy for removing a policy rule from rethinkdb
func (a *adapter) RemovePolicy(sec string, PTYPE string, policys []string) error {
line := a.writeTableLine(PTYPE, policys)
_, err := r.DB(a.database).Table(a.table).Filter(line).Delete().Run(a.session)
if err != nil {
return err
}
return nil
}
//RemoveFilteredPolicy for removing filtered policy
func (a *adapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error {
var selector policy
selector.PTYPE = ptype
if fieldIndex <= 0 && 0 < fieldIndex+len(fieldValues) {
selector.V1 = fieldValues[0-fieldIndex]
}
if fieldIndex <= 1 && 1 < fieldIndex+len(fieldValues) {
selector.V2 = fieldValues[1-fieldIndex]
}
if fieldIndex <= 2 && 2 < fieldIndex+len(fieldValues) {
selector.V3 = fieldValues[2-fieldIndex]
}
if fieldIndex <= 3 && 3 < fieldIndex+len(fieldValues) {
selector.V4 = fieldValues[3-fieldIndex]
}
if fieldIndex <= 4 && 4 < fieldIndex+len(fieldValues) {
selector.V5 = fieldValues[4-fieldIndex]
}
_, err := r.DB(a.database).Table(a.table).Filter(selector).Delete().Run(a.session)
if err != nil {
return err
}
return nil
}