Get Started | Apache Casbin (Incubating)
Skip to main content

Get Started

Installation​

go get github.com/casbin/casbin/v3
Migrating from v2 to v3

If you're upgrading from Casbin v2 to v3, update your import paths from github.com/casbin/casbin/v2 to github.com/casbin/casbin/v3. This applies to all subpackages as well (e.g., v2/model becomes v3/model).

For Go modules, run:

go get -u github.com/casbin/casbin/v3

Then update all imports in your code from /v2 to /v3.

Create a Casbin Enforcer​

Casbin relies on configuration files to specify the access control model.

You need two configuration files: model.conf and policy.csv. The model file defines your access control model; the policy file holds the permission rules. In practice you work with a single component—the Enforcer—which loads both files when it is created.

To create an Enforcer, provide a Model and an Adapter.

Casbin provides a FileAdapter that you can use. See Adapter for more information.

  • Example using a Model file with the default FileAdapter:
import "github.com/casbin/casbin/v3"

e, err := casbin.NewEnforcer("path/to/model.conf", "path/to/policy.csv")
  • Example using Model text with an alternative Adapter:
import (
"log"

"github.com/casbin/casbin/v3"
"github.com/casbin/casbin/v3/model"
xormadapter "github.com/casbin/xorm-adapter/v2"
_ "github.com/go-sql-driver/mysql"
)

// Initialize a Xorm adapter with MySQL database.
a, err := xormadapter.NewAdapter("mysql", "mysql_username:mysql_password@tcp(127.0.0.1:3306)/")
if err != nil {
log.Fatalf("error: adapter: %s", err)
}

m, err := model.NewModelFromString(`
[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj, act

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = r.sub == p.sub && r.obj == p.obj && r.act == p.act
`)
if err != nil {
log.Fatalf("error: model: %s", err)
}

e, err := casbin.NewEnforcer(m, a)
if err != nil {
log.Fatalf("error: enforcer: %s", err)
}

Check Permissions​

Add an enforcement check in your code immediately before the resource is accessed:

sub := "alice" // the user that wants to access a resource.
obj := "data1" // the resource that is going to be accessed.
act := "read" // the operation that the user performs on the resource.

ok, err := e.Enforce(sub, obj, act)

if err != nil {
// handle err
}

if ok == true {
// permit alice to read data1
} else {
// deny the request, show an error
}

// You could use BatchEnforce() to enforce some requests in batches.
// This method returns a bool slice, and this slice's index corresponds to the row index of the two-dimensional array.
// e.g. results[0] is the result of {"alice", "data1", "read"}
results, err := e.BatchEnforce([][]interface{}{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"jack", "data3", "read"}})

Casbin provides APIs for runtime permission management. For instance, you can retrieve all roles assigned to a user:

roles, err := e.GetRolesForUser("alice")

For more examples, see Management API and RBAC API. You can also browse the test cases in each language repository.

Comments are powered by giscus and GitHub Discussions. They load from giscus.app and GitHub only after you click the button below.