Casbin

Casbin

  • Docs
  • API
  • Help
  • Blog
  • Languages iconEnglish
    • 中文
    • Help Translate
  • GitHub

›Storage

The Basics

  • Overview
  • Get Started
  • How it Works
  • Tutorials

Model

  • Supported Models
  • Syntax for Models

Storage

  • Model Storage
  • Policy Storage

Management

  • Policy Management
  • Log & Error Handling
  • Online Editor
  • API Reference

More Resources

  • Our Adopters
Edit

Model Storage

Unlike the policy, the model can be loaded only, it cannot be saved. Because we think the model is not a dynamic component and should not be modified at run-time, so we don't implement an API to save the model into a storage.

However, the good news is, we provide several ways to load a model either statically or dynamically:

Load model from .CONF file

This is the most common way to use Casbin. It's easy to understand for beginners and convenient for sharing when you ask Casbin team for help.

```go
e := casbin.NewEnforcer("examples/basic_model.conf", "examples/basic_policy.csv")
```

Load model from code

The model can be initialized dynamically from code instead of using .CONF file. Here's an example for the RBAC model:

// Initialize the model from Go code.
m := casbin.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 files.
a := persist.NewFileAdapter("examples/rbac_policy.csv")

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

Load model from string

Or you can just get the model from a single multi-line string:

// Initialize the model from a string.
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 := NewModel(text)

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

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

The above two ways are equivalent with the following common use:

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
e := casbin.NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
Last updated on 2018-9-25
← Syntax for ModelsPolicy Storage →
  • Load model from .CONF file
  • Load model from code
  • Load model from string
Casbin
Docs
Getting StartedAPI ReferenceMiddlewares
Community
Who's using Casbin?Mailing ListStack OverflowProject Chat
Social
Casbin      jCasbin
php-Casbin    node-Casbin
Follow @CasbinNews
Copyright © 2019 Casbin contributors.