본문으로 건너뛰기

Model Storage

Casbin의 모델은 **로드 전용(load-only)**입니다: 모델은 액세스 제어 로직을 정의하며 런타임 시 정적(static)으로 취급됩니다. 스토리지에 모델을 저장하거나 업데이트하는 API는 없습니다.

모델은 세 가지 방법으로 로드할 수 있습니다:

Load from a CONF file​

.conf 파일에서 로드하는 것이 일반적인 방법이며, 모델을 공유하고 논의하기 쉽습니다.

예시: examples/rbac_model.conf

[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj, act

[role_definition]
g = _, _

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

[matchers]
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act

enforcer 생성 시 사용:

e := casbin.NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")

Load from code​

파일 대신 코드로 모델을 구성할 수 있습니다:

import (
"github.com/casbin/casbin/v3"
"github.com/casbin/casbin/v3/model"
"github.com/casbin/casbin/v3/persist/file-adapter"
)

// Build the model in Go.
m := model.NewModel()
m.AddDef("r", "r", "sub, obj, act")
m.AddDef("p", "p", "sub, obj, act")
m.AddDef("g", "g", "_, _")
m.AddDef("e", "e", "some(where (p.eft == allow))")
m.AddDef("m", "m", "g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act")

// Load the policy rules from the .CSV file adapter.
// Replace it with your adapter to avoid using files.
a := fileadapter.NewAdapter("examples/rbac_policy.csv")

// Create the enforcer.
e := casbin.NewEnforcer(m, a)

Load from a string​

문자열로부터 모델을 로드할 수도 있습니다(예: 설정 파일이나 데이터베이스에서):

import (
"github.com/casbin/casbin/v3"
"github.com/casbin/casbin/v3/model"
)

// Model text (e.g. from config).
text :=
`
[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj, act

[role_definition]
g = _, _

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

[matchers]
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act
`
m, _ := model.NewModelFromString(text)

// Load the policy rules from the .CSV file adapter.
// Replace it with your adapter to avoid using files.
a := fileadapter.NewAdapter("examples/rbac_policy.csv")

// Create the enforcer.
e := casbin.NewEnforcer(m, a)

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