GORM adapter for Casbin, see extended version of GORM Adapter Ex at: https://github.com/casbin/gorm-adapter-ex
GORM adapter for Casbin, see extended version of GORM Adapter Ex at: https://github.com/casbin/gorm-adapter-ex
In v3.0.3, method
NewAdapterByDBcreates table namedcasbin_rules,
we fix it tocasbin_ruleafter that.
If you used v3.0.3 and less, and you want to update it,
you might need to migrate data manually. Find out more at: https://github.com/casbin/gorm-adapter/issues/78 Gorm Adapter is the Gorm adapter for Casbin. With this library, Casbin can load policy from Gorm supported database or save policy to it.
Based on Officially Supported Databases, The current supported databases are:
gorm-adapter use
github.com/glebarez/sqliteinstead of gorm official sqlite drivergorm.io/driver/sqlitebecause the latter needscgosupport. But there is almost no difference between the two driver. If there is a difference in use, please submit an issue.
go get github.com/casbin/gorm-adapter/v3……New an adapter will use AutoMigrate by default for create table, if you want to turn it off, please use API TurnOffAutoMigrate(db *gorm.DB) *gorm.DB. See example:
db, err := gorm.Open(mysql.Open("root:@tcp(127.0.0.1:3306)/casbin"), &gorm.Config{})
TurnOffAutoMigrate(db)
// a,_ := NewAdapterByDB(...)
// a,_ := NewAdapterByDBUseTableName(...)
a,_ := NewAdapterByDBWithCustomTable(...)Find out more details at gorm-adapter#162
You can change the gorm struct tags, but the table structure must stay the same.
…You can modify policies within a transaction. See the example below:
…// old verion
package main
func main() {
a, err := NewAdapterByDB(db)
e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)
err = e.GetAdapter().(*Adapter).Transaction(e, func(e casbin.IEnforcer) error {
_, err := e.AddPolicy("jack", "data1", "write")
if err != nil {
return err
}
_, err = e.AddPolicy("jack", "data2", "write")
if err != nil {
return err
}
return nil
})
if err != nil {
// handle if transaction failed
return
}
}ConditionsToGormQuery() is a function that converts multiple query conditions into a GORM query statement
You can use the GetAllowedObjectConditions() API of Casbin to get conditions,
and choose the way of combining conditions through combineType.
ConditionsToGormQuery() allows Casbin to be combined with SQL, and you can use it to implement many functions.
DataBase example:
| id | title | author | publisher | publish_data | price | category_id |
|---|---|---|---|---|---|---|
| 1 | book1 | author1 | publisher1 | 2023-04-09 16:23:42 | 10 | 1 |
| 2 | book2 | author1 | publisher1 | 2023-04-09 16:23:44 | 20 | 2 |
| 3 | book3 | author2 | publisher1 | 2023-04-09 16:23:44 | 30 | 1 |
| 4 | book4 | author2 | publisher2 | 2023-04-09 16:23:45 | 10 | 3 |
| 5 | book5 | author3 | publisher2 | 2023-04-09 16:23:45 | 50 | 1 |
| 6 | book6 | author3 | publisher2 | 2023-04-09 16:23:46 | 60 | 2 |
…gormadapter supports adapter with context, the following is a timeout control implemented using context
a, _ := gormadapter.NewAdapter("mysql", "mysql_username:mysql_password@tcp(127.0.0.1:3306)/") // Your driver and data source.
// Limited time 300s
ctx, cancel := context.WithTimeout(context.Background(), 300*time.Microsecond)
defer cancel()
err := a.AddPolicyCtx(ctx, "p", "p", []string{"alice", "data1", "read"})
if err != nil {
panic(err)
}This project is under Apache 2.0 License. See the LICENSE file for the full license text.
No open issues yet, or sync has not completed.