ข้ามไปยังเนื้อหาหลัก

Get Started

การติดตั้ง

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.

Two configuration files are required: model.conf and policy.csv. The model.conf file defines your access control model, while policy.csv contains the specific permission rules. Working with Casbin is simple—you primarily interact with one structure: the enforcer. During initialization, this structure loads both configuration files automatically.

Put simply, creating a Casbin enforcer requires providing a Model and an Adapter.

Casbin มี FileAdapter ที่คุณสามารถใช้ได้ ดู Adapter เพื่อข้อมูลเพิ่มเติม

  • 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)
}

ตรวจสอบสิทธิ์

Insert an enforcement check in your code immediately before resource access occurs:

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 additional usage examples, consult Management API and RBAC API.

Additional examples can be found in the test cases.