- Adapter">type Adapter
- CasbinRule">type CasbinRule
- Filter">type Filter
- demo
- auto save
casbin的gorm适配器
文档:https://godoc.org/github.com/casbin/gorm-adapter
type Adapter
适配器表示用于策略存储的Gorm适配器
type Adapter struct {// contains filtered or unexported fields}// 数据库名和表名是用户定义的。它们的默认值是“casbin”和“casbin_rule”// gormadapter.NewAdapter("mysql", "root:root@xx@tcp(xxx:3306)/study", true)// params如果为false 会自动创建库// params如果为true 不会自动创建库,如果指定的库不存在会报错func NewAdapter(driverName string, dataSourceName string, params ...interface{}) (*Adapter, error)// 用现有的连创建一个适配器,默认的表明为“casbin_rule”// 当然我们可以通过 db.TableName("xxx"),先指定一个表明,再创建适配器func NewAdapterByDB(db *gorm.DB) (*Adapter, error)// NewAdapterByDBUseTableName(&db, "cms", "casbin"),则表明为"cms_casbin"func NewAdapterByDBUseTableName(db *gorm.DB, prefix string, tablename string) (*Adapter, error)
// 添加多个策略规则func (a *Adapter) AddPolicies(sec string, ptype string, rules [][]string) error// 添加规则func (a *Adapter) AddPolicy(sec string, ptype string, rule []string) error// 如果加载的策略已被筛选,IsFiltered返回truefunc (a *Adapter) IsFiltered() bool// LoadFilteredPolicy只加载与筛选器匹配的策略规则func (a *Adapter) LoadFilteredPolicy(model model.Model, filter interface{}) error// LoadPolicy从数据库加载策略func (a *Adapter) LoadPolicy(model model.Model) error// RemoveFilteredPolicy从存储中删除与筛选器匹配的策略规则func (a *Adapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error// RemovePolicy从存储中删除多个策略规则func (a *Adapter) RemovePolicies(sec string, ptype string, rules [][]string) error// RemovePolicy从存储中删除策略规则func (a *Adapter) RemovePolicy(sec string, ptype string, rule []string) error// SavePolicy将策略保存到数据库func (a *Adapter) SavePolicy(model model.Model) error
type CasbinRule
type CasbinRule struct {TablePrefix string `gorm:"-"`TableName string `gorm:"-"`PType string `gorm:"size:100"`V0 string `gorm:"size:100"`V1 string `gorm:"size:100"`V2 string `gorm:"size:100"`V3 string `gorm:"size:100"`V4 string `gorm:"size:100"`V5 string `gorm:"size:100"`}
type Filter
type Filter struct {PType []stringV0 []stringV1 []stringV2 []stringV3 []stringV4 []stringV5 []string}
demo
package mainimport ("github.com/casbin/casbin/v2"gormadapter "github.com/casbin/gorm-adapter/v3"_ "github.com/go-sql-driver/mysql")func main() {// Initialize a Gorm adapter and use it in a Casbin enforcer:// The adapter will use the MySQL database named "casbin".// If it doesn't exist, the adapter will create it automatically.// You can also use an already existing gorm instance with gormadapter.NewAdapterByDB(gormInstance)a, _ := gormadapter.NewAdapter("mysql", "mysql_username:mysql_password@tcp(127.0.0.1:3306)/") // Your driver and data source.e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)// Or you can use an existing DB "abc" like this:// The adapter will use the table named "casbin_rule".// If it doesn't exist, the adapter will create it automatically.// a := gormadapter.NewAdapter("mysql", "mysql_username:mysql_password@tcp(127.0.0.1:3306)/abc", true)// 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()}
auto save
https://casbin.org/docs/en/adapters
自动保存机制 (Auto-Save) 是适配器的特性之一。 如果当前适配器支持自动保存特性,该适配器可以向存储中添加一个策略规则,或者从存储中移除一个策略规则。 这与SavePolicy()不同,因为后者将删除存储中的所有策略规则,并将所有策略规则从Casbin enforcer保存到存储中。 因此,当策略规则的数量较多时,使用SavePolicy()可能会出现性能问题。
当适配器支持自动保存机制时,您可以通过Enforcer.EnableAutoSave() 函数来开启或关闭该机制。 默认情况下启用该选项(如果适配器支持自动保存的话)。
1Auto-Save 特性是可选的。 Adapter可以选择是否实现它。
2Auto-Save 只在Casbin enforcer使用的adapter支持它时才有效。
3See the AutoSave column in the above adapter list to see if Auto-Save is supported by an adapter.
下面是一个关于如何使用Auto-Save的例子:
import ("github.com/casbin/casbin""github.com/casbin/xorm-adapter"_ "github.com/go-sql-driver/mysql")// enforcer会默认开启AutoSave机制.a := xormadapter.NewAdapter("mysql", "mysql_username:mysql_password@tcp(127.0.0.1:3306)/")e := casbin.NewEnforcer("examples/basic_model.conf", a)// 禁用AutoSave机制e.EnableAutoSave(false)// 因为禁用了AutoSave,当前策略的改变只在内存中生效// 这些策略在持久层中仍是不变的e.AddPolicy(...)e.RemovePolicy(...)// 开启AutoSave机制e.EnableAutoSave(true)// 因为开启了AutoSave机制,现在内存中的改变会同步回写到持久层中e.AddPolicy(...)e.RemovePolicy(...)
