diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8253892 --- /dev/null +++ b/go.mod @@ -0,0 +1,15 @@ +module github.com/adityapandey9/rethinkdb-adapter + +require ( + github.com/Knetic/govaluate v3.0.0+incompatible // indirect + github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932 // indirect + github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect + github.com/casbin/casbin v1.7.0 + github.com/onsi/ginkgo v1.7.0 // indirect + github.com/onsi/gomega v1.4.3 // indirect + github.com/pkg/errors v0.8.0 // indirect + golang.org/x/sync v0.0.0-20181108010431-42b317875d0f // indirect + gopkg.in/airbrake/gobrake.v2 v2.0.9 // indirect + gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2 // indirect + gopkg.in/rethinkdb/rethinkdb-go.v5 v5.0.1 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7e3f415 --- /dev/null +++ b/go.sum @@ -0,0 +1,74 @@ +github.com/Knetic/govaluate v3.0.0+incompatible h1:7o6+MAPhYTCF0+fdvoz1xDedhRb4f6s9Tn1Tt7/WTEg= +github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0= +github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932 h1:mXoPYz/Ul5HYEDvkta6I8/rnYM5gSdSV2tJ6XbZuEtY= +github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932/go.mod h1:NOuUCSz6Q9T7+igc/hlvDOUdtWKryOrtFyIVABv/p7k= +github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY= +github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4= +github.com/casbin/casbin v1.7.0 h1:PuzlE8w0JBg/DhIqnkF1Dewf3z+qmUZMVN07PonvVUQ= +github.com/casbin/casbin v1.7.0/go.mod h1:c67qKN6Oum3UF5Q1+BByfFxkwKvhwW57ITjqwtzR1KE= +github.com/cenkalti/backoff v2.0.0+incompatible h1:5IIPUHhlnUZbcHQsQou5k1Tn58nJkeJL9U+ig5CHJbY= +github.com/cenkalti/backoff v2.0.0+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed h1:5upAirOpQc1Q53c0bnx2ufif5kANL7bfZWcc6VJWJd8= +github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed/go.mod h1:tMWxXQ9wFIaZeTI9F+hmhFiGpFmhOHzyShyFUhRm0H4= +github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs= +github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU= +github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= +github.com/opentracing/opentracing-go v1.0.2 h1:3jA2P6O1F9UOrWVpwrIo17pu01KWvNWg4X946/Y5Zwg= +github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= +github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= +github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.0.6 h1:hcP1GmhGigz/O7h1WVUM5KklBp1JoNS9FggWKdj/j3s= +github.com/sirupsen/logrus v1.0.6/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc= +github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +golang.org/x/crypto v0.0.0-20180820150726-614d502a4dac h1:7d7lG9fHOLdL6jZPtnV4LpI41SbohIJ1Atq7U991dMg= +golang.org/x/crypto v0.0.0-20180820150726-614d502a4dac/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d h1:g9qWBGx4puODJTMVyoPrpoxPFgVGd+z1DZwjfRu4d0I= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f h1:Bl/8QSvNqXvPGPGXa2z5xUTmV7VDcZyvRZ+QQXkXTZQ= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180828065106-d99a578cf41b h1:cmOZLU2i7CLArKNViO+ZCQ47wqYFyKEIpbGWp+b6Uoc= +golang.org/x/sys v0.0.0-20180828065106-d99a578cf41b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUkVZqzHJT5DOasTyn8Vs= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/airbrake/gobrake.v2 v2.0.9 h1:7z2uVWwn7oVeeugY1DtlPAy5H+KYgB1KeKTnqjNatLo= +gopkg.in/airbrake/gobrake.v2 v2.0.9/go.mod h1:/h5ZAUhDkGaJfjzjKLSjv6zCL6O0LLBxU4K+aSYdM/U= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fatih/pool.v2 v2.0.0 h1:xIFeWtxifuQJGk/IEPKsTduEKcKvPmhoiVDGpC40nKg= +gopkg.in/fatih/pool.v2 v2.0.0/go.mod h1:8xVGeu1/2jr2wm5V9SPuMht2H5AEmf5aFMGSQixtjTY= +gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2 h1:OAj3g0cR6Dx/R07QgQe8wkA9RNjB2u4i700xBkIT4e0= +gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2/go.mod h1:Xk6kEKp8OKb+X14hQBKWaSkCsqBpgog8nAV2xsGOxlo= +gopkg.in/rethinkdb/rethinkdb-go.v5 v5.0.1 h1:cBTUuUFTllRYtInK6FtBRa+tOBlZqpeDTnZF54xjGbg= +gopkg.in/rethinkdb/rethinkdb-go.v5 v5.0.1/go.mod h1:x+1XKi70FH0kHCpvPQ78hGBCCxoNdE7sP+kEFdKgN6A= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/rethinkadapter.go b/rethinkadapter.go index 8e53d5c..723df08 100644 --- a/rethinkadapter.go +++ b/rethinkadapter.go @@ -6,12 +6,14 @@ import ( "github.com/casbin/casbin/model" "github.com/casbin/casbin/persist" - r "gopkg.in/gorethink/gorethink.v3" + r "gopkg.in/rethinkdb/rethinkdb-go.v5" ) // adapter represents the RethinkDB adapter for policy storage. type adapter struct { - session r.QueryExecutor + session r.QueryExecutor + database string + table string } type policy struct { @@ -21,6 +23,7 @@ type policy struct { V2 string `gorethink:"v2"` V3 string `gorethink:"v3"` V4 string `gorethink:"v4"` + V5 string `gorethink:"v5"` } func finalizer(a *adapter) { @@ -29,19 +32,39 @@ func finalizer(a *adapter) { // NewAdapter is the constructor for adapter. func NewAdapter(Sessionvar r.QueryExecutor) persist.Adapter { - a := &adapter{session: Sessionvar} + a := &adapter{session: Sessionvar, database:"casbin", table: "policy"} 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("casbin").Do(r.DBCreate("casbin").Exec(a.session)).Run(a.session) + _, err := r.DBList().Contains(a.database).Do(r.DBCreate(a.database).Exec(a.session)).Run(a.session) if err != nil { return err } @@ -49,7 +72,7 @@ func (a *adapter) createDatabase() error { } func (a *adapter) createTable() error { - _, err := r.DB("casbin").TableList().Contains("policy").Do(r.DB("casbin").TableCreate("policy").Exec(a.session)).Run(a.session) + _, 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 } @@ -68,7 +91,7 @@ func (a *adapter) open() { //Erase the table data func (a *adapter) dropTable() error { - _, err := r.DB("casbin").Table("policy").Delete().Run(a.session) + _, err := r.DB(a.database).Table(a.table).Delete().Run(a.session) if err != nil { panic(err) } @@ -101,6 +124,10 @@ func loadPolicyLine(line policy, model model.Model) { tokens = append(tokens, line.V4) } + if line.V5 != "" { + tokens = append(tokens, line.V5) + } + model[sec][key].Policy = append(model[sec][key].Policy, tokens) } @@ -108,7 +135,7 @@ func loadPolicyLine(line policy, model model.Model) { func (a *adapter) LoadPolicy(model model.Model) error { a.open() - rows, errn := r.DB("casbin").Table("policy").Run(a.session) + rows, errn := r.DB(a.database).Table(a.table).Run(a.session) if errn != nil { fmt.Printf("E: %v\n", errn) return errn @@ -137,6 +164,8 @@ func (a *adapter) writeTableLine(ptype string, rule []string) policy { items.V3 = rule[i] case 3: items.V4 = rule[i] + case 4: + items.V5 = rule[i] } } return items @@ -161,7 +190,7 @@ func (a *adapter) SavePolicy(model model.Model) error { lines = append(lines, line) } } - _, err := r.DB("casbin").Table("policy").Insert(lines).Run(a.session) + _, err := r.DB(a.database).Table(a.table).Insert(lines).Run(a.session) if err != nil { fmt.Printf("Error: %v\n", err) return err @@ -172,7 +201,7 @@ func (a *adapter) SavePolicy(model model.Model) error { //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("casbin").Table("policy").Insert(line).Run(a.session) + _, err := r.DB(a.database).Table(a.table).Insert(line).Run(a.session) if err != nil { return err } @@ -182,7 +211,7 @@ func (a *adapter) AddPolicy(sec string, PTYPE string, policys []string) error { //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("casbin").Table("policy").Filter(line).Delete().Run(a.session) + _, err := r.DB(a.database).Table(a.table).Filter(line).Delete().Run(a.session) if err != nil { return err } @@ -206,8 +235,11 @@ func (a *adapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, 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("casbin").Table("policy").Filter(selector).Delete().Run(a.session) + _, err := r.DB(a.database).Table(a.table).Filter(selector).Delete().Run(a.session) if err != nil { panic(err) } diff --git a/rethinkadapter_test.go b/rethinkadapter_test.go index 15dc4f7..1c020bf 100644 --- a/rethinkadapter_test.go +++ b/rethinkadapter_test.go @@ -6,7 +6,7 @@ import ( "testing" "github.com/casbin/casbin" - r "gopkg.in/gorethink/gorethink.v4" + r "gopkg.in/rethinkdb/rethinkdb-go.v5" ) func getConnect() r.QueryExecutor {