casbin的gorm适配器
文档:https://godoc.org/github.com/casbin/gorm-adapter

type Adapter

适配器表示用于策略存储的Gorm适配器

  1. type Adapter struct {
  2. // contains filtered or unexported fields
  3. }
  4. // 数据库名和表名是用户定义的。它们的默认值是“casbin”和“casbin_rule”
  5. // gormadapter.NewAdapter("mysql", "root:root@xx@tcp(xxx:3306)/study", true)
  6. // params如果为false 会自动创建库
  7. // params如果为true 不会自动创建库,如果指定的库不存在会报错
  8. func NewAdapter(driverName string, dataSourceName string, params ...interface{}) (*Adapter, error)
  9. // 用现有的连创建一个适配器,默认的表明为“casbin_rule”
  10. // 当然我们可以通过 db.TableName("xxx"),先指定一个表明,再创建适配器
  11. func NewAdapterByDB(db *gorm.DB) (*Adapter, error)
  12. // NewAdapterByDBUseTableName(&db, "cms", "casbin"),则表明为"cms_casbin"
  13. func NewAdapterByDBUseTableName(db *gorm.DB, prefix string, tablename string) (*Adapter, error)
  1. // 添加多个策略规则
  2. func (a *Adapter) AddPolicies(sec string, ptype string, rules [][]string) error
  3. // 添加规则
  4. func (a *Adapter) AddPolicy(sec string, ptype string, rule []string) error
  5. // 如果加载的策略已被筛选,IsFiltered返回true
  6. func (a *Adapter) IsFiltered() bool
  7. // LoadFilteredPolicy只加载与筛选器匹配的策略规则
  8. func (a *Adapter) LoadFilteredPolicy(model model.Model, filter interface{}) error
  9. // LoadPolicy从数据库加载策略
  10. func (a *Adapter) LoadPolicy(model model.Model) error
  11. // RemoveFilteredPolicy从存储中删除与筛选器匹配的策略规则
  12. func (a *Adapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error
  13. // RemovePolicy从存储中删除多个策略规则
  14. func (a *Adapter) RemovePolicies(sec string, ptype string, rules [][]string) error
  15. // RemovePolicy从存储中删除策略规则
  16. func (a *Adapter) RemovePolicy(sec string, ptype string, rule []string) error
  17. // SavePolicy将策略保存到数据库
  18. func (a *Adapter) SavePolicy(model model.Model) error

type CasbinRule

  1. type CasbinRule struct {
  2. TablePrefix string `gorm:"-"`
  3. TableName string `gorm:"-"`
  4. PType string `gorm:"size:100"`
  5. V0 string `gorm:"size:100"`
  6. V1 string `gorm:"size:100"`
  7. V2 string `gorm:"size:100"`
  8. V3 string `gorm:"size:100"`
  9. V4 string `gorm:"size:100"`
  10. V5 string `gorm:"size:100"`
  11. }

type Filter

  1. type Filter struct {
  2. PType []string
  3. V0 []string
  4. V1 []string
  5. V2 []string
  6. V3 []string
  7. V4 []string
  8. V5 []string
  9. }

demo

  1. package main
  2. import (
  3. "github.com/casbin/casbin/v2"
  4. gormadapter "github.com/casbin/gorm-adapter/v3"
  5. _ "github.com/go-sql-driver/mysql"
  6. )
  7. func main() {
  8. // Initialize a Gorm adapter and use it in a Casbin enforcer:
  9. // The adapter will use the MySQL database named "casbin".
  10. // If it doesn't exist, the adapter will create it automatically.
  11. // You can also use an already existing gorm instance with gormadapter.NewAdapterByDB(gormInstance)
  12. a, _ := gormadapter.NewAdapter("mysql", "mysql_username:mysql_password@tcp(127.0.0.1:3306)/") // Your driver and data source.
  13. e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)
  14. // Or you can use an existing DB "abc" like this:
  15. // The adapter will use the table named "casbin_rule".
  16. // If it doesn't exist, the adapter will create it automatically.
  17. // a := gormadapter.NewAdapter("mysql", "mysql_username:mysql_password@tcp(127.0.0.1:3306)/abc", true)
  18. // Load the policy from DB.
  19. e.LoadPolicy()
  20. // Check the permission.
  21. e.Enforce("alice", "data1", "read")
  22. // Modify the policy.
  23. // e.AddPolicy(...)
  24. // e.RemovePolicy(...)
  25. // Save the policy back to DB.
  26. e.SavePolicy()
  27. }

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的例子:

  1. import (
  2. "github.com/casbin/casbin"
  3. "github.com/casbin/xorm-adapter"
  4. _ "github.com/go-sql-driver/mysql"
  5. )
  6. // enforcer会默认开启AutoSave机制.
  7. a := xormadapter.NewAdapter("mysql", "mysql_username:mysql_password@tcp(127.0.0.1:3306)/")
  8. e := casbin.NewEnforcer("examples/basic_model.conf", a)
  9. // 禁用AutoSave机制
  10. e.EnableAutoSave(false)
  11. // 因为禁用了AutoSave,当前策略的改变只在内存中生效
  12. // 这些策略在持久层中仍是不变的
  13. e.AddPolicy(...)
  14. e.RemovePolicy(...)
  15. // 开启AutoSave机制
  16. e.EnableAutoSave(true)
  17. // 因为开启了AutoSave机制,现在内存中的改变会同步回写到持久层中
  18. e.AddPolicy(...)
  19. e.RemovePolicy(...)