MongoDB Adapter
MongoDB Adapter is the Mongo DB adapter for Casbin. With this library, Casbin can load policy from MongoDB or save policy to it.
Installation
go get -u github.com/casbin/mongodb-adapter/v3
Simple Example
package main
import (
"github.com/casbin/casbin/v2"
"github.com/casbin/mongodb-adapter/v3"
)
func main() {
a,err := mongodbadapter.NewAdapter("127.0.0.1:27017")
if err != nil {
panic(err)
}
e, err := casbin.NewEnforcer("examples/rbac_model.conf", a)
if err != nil {
panic(err)
}
e.LoadPolicy()
e.Enforce("alice", "data1", "read")
e.SavePolicy()
}
Advanced Example
package main
import (
"github.com/casbin/casbin/v2"
"github.com/casbin/mongodb-adapter/v3"
mongooptions "go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
mongoClientOption := mongooptions.Client().ApplyURI("mongodb://127.0.0.1:27017")
databaseName := "casbin"
a,err := mongodbadapter.NewAdapterWithClientOption(mongoClientOption, databaseName)
if err != nil {
panic(err)
}
e, err := casbin.NewEnforcer("examples/rbac_model.conf", a)
if err != nil {
panic(err)
}
e.LoadPolicy()
e.Enforce("alice", "data1", "read")
e.SavePolicy()
}
Filtered Policies
import "github.com/globalsign/mgo/bson"
filter := &bson.M{"v0": "alice"}
e.LoadFilteredPolicy(filter)
Getting Help
License
This project is under Apache 2.0 License. See the LICENSE file for the full license text.