Skip to content

Commit 597975e

Browse files
committed
add go.mod and support casbin 2.x
1 parent a1e83ac commit 597975e

File tree

4 files changed

+56
-20
lines changed

4 files changed

+56
-20
lines changed

adapter.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import (
55
"fmt"
66
"strconv"
77

8-
"github.com/casbin/casbin/model"
9-
"github.com/casbin/casbin/persist"
10-
"github.com/spacycoder/cosmosdb-go-sdk/cosmos"
118
"context"
9+
10+
"github.com/casbin/casbin/v2/model"
11+
"github.com/casbin/casbin/v2/persist"
12+
"github.com/spacycoder/cosmosdb-go-sdk/cosmos"
1213
)
1314

1415
// CasbinRule represents a rule in Casbin.
@@ -87,7 +88,7 @@ func (a *adapter) createCollectionIfNotExist(collection *cosmos.Collection) {
8788
if err, ok := err.(*cosmos.Error); ok {
8889
if err.NotFound() {
8990
collDef := &cosmos.CollectionDefinition{Resource: cosmos.Resource{ID: a.collectionName}, PartitionKey: cosmos.PartitionKeyDefinition{Paths: []string{"/pType"}, Kind: "Hash"}}
90-
_, err := a.db.Collections().Create(context.Background(),collDef)
91+
_, err := a.db.Collections().Create(context.Background(), collDef)
9192
if err != nil {
9293
panic(fmt.Sprintf("Creating cosmos collection caused error: %s", err.Error()))
9394
}
@@ -173,14 +174,14 @@ func (a *adapter) LoadFilteredPolicy(model model.Model, filter interface{}) erro
173174
lines := []CasbinRule{}
174175
if filter == nil {
175176
a.filtered = false
176-
res, err := a.collection.Documents().ReadAll(context.Background(),&lines, cosmos.CrossPartition())
177+
res, err := a.collection.Documents().ReadAll(context.Background(), &lines, cosmos.CrossPartition())
177178
if err != nil {
178179
return err
179180
}
180181
tokenString := res.Continuation()
181182
for tokenString != "" {
182183
newLines := []CasbinRule{}
183-
res, err := a.collection.Documents().ReadAll(context.Background(),&newLines, cosmos.CrossPartition(), cosmos.Continuation(tokenString))
184+
res, err := a.collection.Documents().ReadAll(context.Background(), &newLines, cosmos.CrossPartition(), cosmos.Continuation(tokenString))
184185
if err != nil {
185186
return err
186187
}
@@ -190,14 +191,14 @@ func (a *adapter) LoadFilteredPolicy(model model.Model, filter interface{}) erro
190191
} else {
191192
querySpec := filter.(cosmos.SqlQuerySpec)
192193
a.filtered = true
193-
res, err := a.collection.Documents().Query(context.Background(),&querySpec, &lines, cosmos.CrossPartition())
194+
res, err := a.collection.Documents().Query(context.Background(), &querySpec, &lines, cosmos.CrossPartition())
194195
if err != nil {
195196
return err
196197
}
197198
tokenString := res.Continuation()
198199
for tokenString != "" {
199200
newLines := []CasbinRule{}
200-
res, err := a.collection.Documents().Query(context.Background(),&querySpec, &newLines, cosmos.CrossPartition(), cosmos.Continuation(tokenString))
201+
res, err := a.collection.Documents().Query(context.Background(), &querySpec, &newLines, cosmos.CrossPartition(), cosmos.Continuation(tokenString))
201202
if err != nil {
202203
return err
203204
}
@@ -299,13 +300,13 @@ func (a *adapter) RemovePolicy(sec string, ptype string, rule []string) error {
299300

300301
querySpec := cosmos.SqlQuerySpec{Parameters: parameters, Query: query}
301302
var policies []CasbinRule
302-
_, err := a.collection.Documents().Query(context.Background(),&querySpec, &policies, cosmos.PartitionKey(ptype))
303+
_, err := a.collection.Documents().Query(context.Background(), &querySpec, &policies, cosmos.PartitionKey(ptype))
303304
if err != nil {
304305
return err
305306
}
306307

307308
for _, policy := range policies {
308-
_, err := a.collection.Document(policy.ID).Delete(context.Background(),cosmos.PartitionKey(policy.PType))
309+
_, err := a.collection.Document(policy.ID).Delete(context.Background(), cosmos.PartitionKey(policy.PType))
309310
if err != nil {
310311
return err
311312
}
@@ -359,13 +360,13 @@ func (a *adapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int,
359360

360361
querySpec := cosmos.SqlQuerySpec{Parameters: parameters, Query: query}
361362
var policies []CasbinRule
362-
_, err := a.collection.Documents().Query(context.Background(),&querySpec, &policies, cosmos.PartitionKey(ptype))
363+
_, err := a.collection.Documents().Query(context.Background(), &querySpec, &policies, cosmos.PartitionKey(ptype))
363364
if err != nil {
364365
return err
365366
}
366367

367368
for _, policy := range policies {
368-
_, err := a.collection.Document(policy.ID).Delete(context.Background(),cosmos.PartitionKey(policy.PType))
369+
_, err := a.collection.Document(policy.ID).Delete(context.Background(), cosmos.PartitionKey(policy.PType))
369370
if err != nil {
370371
return err
371372
}

adapter_test.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import (
2020

2121
"github.com/spacycoder/cosmosdb-go-sdk/cosmos"
2222

23-
"github.com/casbin/casbin"
24-
"github.com/casbin/casbin/util"
23+
"github.com/casbin/casbin/v2"
24+
"github.com/casbin/casbin/v2/util"
2525
)
2626

2727
var testConnString = os.Getenv("TEST_COSMOS_URL")
@@ -42,7 +42,10 @@ func testGetPolicy(t *testing.T, e *casbin.Enforcer, res [][]string) {
4242

4343
func initPolicy(t *testing.T, db, coll string) {
4444
// so we need to load the policy from the file adapter (.CSV) first.
45-
e := casbin.NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
45+
e, err := casbin.NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
46+
if err != nil {
47+
panic(err)
48+
}
4649
options := []Option{}
4750
if db != "" {
4851
options = append(options, Database(db))
@@ -54,7 +57,7 @@ func initPolicy(t *testing.T, db, coll string) {
5457
// This is a trick to save the current policy to the DB.
5558
// We can't call e.SavePolicy() because the adapter in the enforcer is still the file adapter.
5659
// The current policy means the policy in the Casbin enforcer (aka in memory).
57-
err := a.SavePolicy(e.GetModel())
60+
err = a.SavePolicy(e.GetModel())
5861
if err != nil {
5962
panic(err)
6063
}
@@ -80,7 +83,11 @@ func TestAdapter(t *testing.T) {
8083
// Create an adapter and an enforcer.
8184
// NewEnforcer() will load the policy automatically.
8285
a := NewAdapter(getConnString())
83-
e := casbin.NewEnforcer("examples/rbac_model.conf", a)
86+
e, err := casbin.NewEnforcer("examples/rbac_model.conf", a)
87+
if err != nil {
88+
t.Fatalf("Expected NewEnforcer() to be successful; got %v", err)
89+
}
90+
8491
testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}})
8592

8693
// AutoSave is enabled by default.
@@ -145,7 +152,10 @@ func TestAdapter(t *testing.T) {
145152

146153
func TestDeleteFilteredAdapter(t *testing.T) {
147154
a := NewAdapter(getConnString())
148-
e := casbin.NewEnforcer("examples/rbac_tenant_service.conf", a)
155+
e, err := casbin.NewEnforcer("examples/rbac_tenant_service.conf", a)
156+
if err != nil {
157+
t.Fatalf("Expected NewEnforcer() to be successful; got %v", err)
158+
}
149159

150160
e.AddPolicy("domain1", "alice", "data3", "read", "accept", "service1")
151161
e.AddPolicy("domain1", "alice", "data3", "write", "accept", "service2")
@@ -176,7 +186,10 @@ func TestFilteredAdapter(t *testing.T) {
176186
// Create an adapter and an enforcer.
177187
// NewEnforcer() will load the policy automatically.
178188
a := NewAdapter(getConnString())
179-
e := casbin.NewEnforcer("examples/rbac_model.conf", a)
189+
e, err := casbin.NewEnforcer("examples/rbac_model.conf", a)
190+
if err != nil {
191+
t.Fatalf("Expected NewEnforcer() to be successful; got %v", err)
192+
}
180193

181194
// Load filtered policies from the database.
182195
e.AddPolicy("alice", "data1", "write")
@@ -231,7 +244,11 @@ func TestAdapterWithOptions(t *testing.T) {
231244
// Create an adapter and an enforcer.
232245
// NewEnforcer() will load the policy automatically.
233246
a := NewAdapter(getConnString(), Database("mycasbindb"), Collection("mycasbincollection"))
234-
e := casbin.NewEnforcer("examples/rbac_model.conf", a)
247+
e, err := casbin.NewEnforcer("examples/rbac_model.conf", a)
248+
if err != nil {
249+
t.Fatalf("Expected NewEnforcer() to be successful; got %v", err)
250+
}
251+
235252
testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}})
236253

237254
// AutoSave is enabled by default.

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/spacycoder/cosmos-casbin-adapter
2+
3+
go 1.12
4+
5+
require (
6+
github.com/casbin/casbin/v2 v2.0.1
7+
github.com/spacycoder/cosmosdb-go-sdk v0.1.0
8+
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible h1:1G1pk05UrOh0NlF1oeaaix1x8XzrfjIDK47TY0Zehcw=
2+
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
3+
github.com/casbin/casbin v1.9.1 h1:ucjbS5zTrmSLtH4XogqOG920Poe6QatdXtz1FEbApeM=
4+
github.com/casbin/casbin v1.9.1/go.mod h1:z8uPsfBJGUsnkagrt3G8QvjgTKFMBJ32UP8HpZllfog=
5+
github.com/casbin/casbin/v2 v2.0.1 h1:yCfJYcr27iIGqag9IYASlW6VBKiWjJiLgQ4yLyIogSw=
6+
github.com/casbin/casbin/v2 v2.0.1/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
7+
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
8+
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
9+
github.com/spacycoder/cosmosdb-go-sdk v0.1.0 h1:AFWYOcCvCBFe9mO04fE+Yqcq0TCf6Dx6K5G8NBMw4IM=
10+
github.com/spacycoder/cosmosdb-go-sdk v0.1.0/go.mod h1:eW/wmGR3jL8QMUl6tf51DLeSAy7o15NkZ84GMCcxxvs=

0 commit comments

Comments
 (0)