-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathadapter.go
200 lines (169 loc) · 4.42 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
// Copyright 2017 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cassandraadapter
import (
"errors"
"sort"
"strconv"
"strings"
"github.com/casbin/casbin/model"
"github.com/gocql/gocql"
)
// Adapter represents the Cassandra adapter for policy storage.
type Adapter struct {
hosts []string
session *gocql.Session
}
// NewAdapter is the constructor for Adapter.
func NewAdapter(hosts ...string) *Adapter {
a := Adapter{}
a.hosts = hosts
return &a
}
func (a *Adapter) open() {
cluster := gocql.NewCluster(a.hosts...)
session, err := cluster.CreateSession()
err = session.Query("CREATE KEYSPACE IF NOT EXISTS casbin WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 } AND DURABLE_WRITES = true ").Exec()
if err != nil {
panic(err)
}
//session.Close()
//
//cluster.Keyspace = "casbin"
//session, err = cluster.CreateSession()
//if err != nil {
// panic(err)
//}
a.session = session
}
func (a *Adapter) close() {
a.session.Close()
}
func (a *Adapter) createTable() {
err := a.session.Query("CREATE TABLE IF NOT EXISTS casbin.policy (no text PRIMARY KEY, ptype text, v1 text, v2 text, v3 text, v4 text)").Exec()
if err != nil {
panic(err)
}
}
func (a *Adapter) dropTable() {
err := a.session.Query("DROP TABLE IF EXISTS casbin.policy").Exec()
if err != nil {
panic(err)
}
}
func loadPolicyLine(line string, model model.Model) {
if line == "" {
return
}
tokens := strings.Split(line, ", ")
key := tokens[0]
sec := key[:1]
model[sec][key].Policy = append(model[sec][key].Policy, tokens[1:])
}
// LoadPolicy loads policy from database.
func (a *Adapter) LoadPolicy(model model.Model) error {
a.open()
defer a.close()
var (
no string
ptype string
v1 string
v2 string
v3 string
v4 string
)
lines := make(map[int]string)
iter := a.session.Query(`SELECT no, ptype, v1, v2, v3, v4 FROM casbin.policy`).Iter()
for iter.Scan(&no, &ptype, &v1, &v2, &v3, &v4) {
line := ptype
if v1 != "" {
line += ", " + v1
}
if v2 != "" {
line += ", " + v2
}
if v3 != "" {
line += ", " + v3
}
if v4 != "" {
line += ", " + v4
}
i, _ := strconv.Atoi(no)
lines[i] = line
// log.Println(ptype, v1, v2, v3, v4)
}
var keys []int
for k := range lines {
keys = append(keys, k)
}
sort.Ints(keys)
for _, k := range keys {
loadPolicyLine(lines[k], model)
}
if err := iter.Close(); err != nil {
return err
}
return nil
}
func (a *Adapter) writeTableLine(no int, ptype string, rule []string) error {
line := "'" + strconv.Itoa(no) + "','" + ptype + "'"
for i := range rule {
line += ",'" + rule[i] + "'"
}
for i := 0; i < 4-len(rule); i++ {
line += ",''"
}
err := a.session.Query("INSERT INTO casbin.policy (no,ptype,v1,v2,v3,v4) values(" + line + ")").Exec()
if err != nil {
return err
}
return nil
}
// SavePolicy saves policy to database.
func (a *Adapter) SavePolicy(model model.Model) error {
a.open()
defer a.close()
a.dropTable()
a.createTable()
no := 0
for ptype, ast := range model["p"] {
for _, rule := range ast.Policy {
if err := a.writeTableLine(no, ptype, rule); err != nil {
return err
}
no += 1
}
}
for ptype, ast := range model["g"] {
for _, rule := range ast.Policy {
if err := a.writeTableLine(no, ptype, rule); err != nil {
return err
}
no += 1
}
}
return nil
}
// AddPolicy adds a policy rule to the storage.
func (a *Adapter) AddPolicy(sec string, ptype string, rule []string) error {
return errors.New("not implemented")
}
// RemovePolicy removes a policy rule from the storage.
func (a *Adapter) RemovePolicy(sec string, ptype string, rule []string) error {
return errors.New("not implemented")
}
// RemoveFilteredPolicy removes policy rules that match the filter from the storage.
func (a *Adapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error {
return errors.New("not implemented")
}