Ent Adapter is the Ent adapter for Casbin. With this library, Casbin can load policy from Ent-supported databases or save policy to them.
Based on Ent Supported Drivers, the current supported databases are:
go get github.com/casbin/ent-adapter
package main import ( "github.com/casbin/casbin/v3" entadapter "github.com/casbin/ent-adapter" _ "github.com/go-sql-driver/mysql" ) func main() { // Initialize an Ent adapter and use it in a Casbin enforcer: // The adapter will use the MySQL database named "casbin". // The database should be created manually before using the adapter. a, _ := entadapter.NewAdapter("mysql", "root:@tcp(127.0.0.1:3306)/casbin") // Your driver and data source. e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a) // Load the policy from DB. e.LoadPolicy() // Check the permission. e.Enforce("alice", "data1", "read") // Modify the policy. // e.AddPolicy(...) // e.RemovePolicy(...) // Save the policy back to DB. e.SavePolicy() }
package main import ( "github.com/casbin/casbin/v3" entadapter "github.com/casbin/ent-adapter" _ "github.com/lib/pq" ) func main() { // Initialize an Ent adapter and use it in a Casbin enforcer: // The adapter will use the PostgreSQL database named "casbin". // The database should be created manually before using the adapter. a, _ := entadapter.NewAdapter("postgres", "user=postgres password=postgres host=127.0.0.1 port=5432 sslmode=disable dbname=casbin") // Your driver and data source. e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a) // Load the policy from DB. e.LoadPolicy() // Check the permission. e.Enforce("alice", "data1", "read") // Modify the policy. // e.AddPolicy(...) // e.RemovePolicy(...) // Save the policy back to DB. e.SavePolicy() }
You can also create an adapter with an existing Ent client instance:
package main import ( "github.com/casbin/casbin/v3" entadapter "github.com/casbin/ent-adapter" "github.com/casbin/ent-adapter/ent" ) func main() { // Create an Ent client client, _ := ent.Open("mysql", "root:@tcp(127.0.0.1:3306)/casbin") // Initialize an Ent adapter with the client a, _ := entadapter.NewAdapterWithClient(client) e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a) // Load the policy from DB. e.LoadPolicy() // Check the permission. e.Enforce("alice", "data1", "read") // Save the policy back to DB. e.SavePolicy() }
The database used in the adapter should be created manually before calling NewAdapter. The adapter will automatically create the casbin_rule table if it doesn't exist.
This project is under Apache 2.0 License. See the LICENSE file for the full license text.