Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2
Extended version of GORM Adapter, see original version of GORM Adapter at:https://github.com/casbin/gorm-adapter
License
casbin/gorm-adapter-ex
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This gorm-adapter-ex is an extended version ofGorm-Adapter.It introduces great and implementsproposal of customizability by reflection. The database structure can be arbitrary as long as it has{"ID", "Ptype", "V0"} three fields.
typeTestCasbinRulestruct {//arbitrary number of variable fieldsIDuint`gorm:"primaryKey;autoIncrement"`Ptypestring`gorm:"size:16"`V0string`gorm:"size:128"`V1string`gorm:"size:128"`V2string`gorm:"size:256"`DeletedAt gorm.DeletedAt}// Create an adaptera,_:=NewAdapterByDBWithCustomTable(db,&TestCasbinRule{},"test_casbin_rule")//...
Background for why we create a new fork:
casbin/gorm-adapter#179 (comment)
casbin/gorm-adapter#168 (comment)
Gorm Adapter is theGorm adapter forCasbin. With this library, Casbin can load policy from Gorm supported database or save policy to it.
Based onOfficially Supported Databases, The current supported databases are:
- MySQL
- PostgreSQL
- SQL Server
- Sqlite3
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.
- other 3rd-party supported DBs in Gorm website or other places.
go get github.com/casbin/gorm-adapter/v3package mainimport ("github.com/casbin/casbin/v2"gormadapter"github.com/casbin/gorm-adapter/v3"_"github.com/go-sql-driver/mysql")funcmain() {// 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()}
New an adapter will useAutoMigrate by default for create table, if you want to turn it off, please use APITurnOffAutoMigrate(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 atgorm-adapter#162
You can change the gorm struct tags, but the table structure must stay the same.
package mainimport ("github.com/casbin/casbin/v2"gormadapter"github.com/casbin/gorm-adapter/v3""gorm.io/gorm")funcmain() {// Increase the column size to 512.typeCasbinRulestruct {IDuint`gorm:"primaryKey;autoIncrement"`Ptypestring`gorm:"size:512;uniqueIndex:unique_index"`V0string`gorm:"size:512;uniqueIndex:unique_index"`V1string`gorm:"size:512;uniqueIndex:unique_index"`V2string`gorm:"size:512;uniqueIndex:unique_index"`V3string`gorm:"size:512;uniqueIndex:unique_index"`V4string`gorm:"size:512;uniqueIndex:unique_index"`V5string`gorm:"size:512;uniqueIndex:unique_index"`}db,_:= gorm.Open(...)// Initialize a Gorm adapter and use it in a Casbin enforcer:// The adapter will use an existing gorm.DB instnace.a,_:=gormadapter.NewAdapterByDBWithCustomTable(db,&CasbinRule{})e,_:=casbin.NewEnforcer("examples/rbac_model.conf",a)// 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()}
This project is under Apache 2.0 License. See theLICENSE file for the full license text.
About
Extended version of GORM Adapter, see original version of GORM Adapter at:https://github.com/casbin/gorm-adapter
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.