{"id":19298246,"url":"https://github.com/casbin/gorm-adapter-ex","last_synced_at":"2025-08-05T15:16:35.504Z","repository":{"id":59045452,"uuid":"529289342","full_name":"casbin/gorm-adapter-ex","owner":"casbin","description":"Extended version of GORM Adapter, see original version of GORM Adapter at: https://github.com/casbin/gorm-adapter","archived":false,"fork":false,"pushed_at":"2023-08-08T05:29:15.000Z","size":175,"stargazers_count":11,"open_issues_count":0,"forks_count":3,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-10-29T16:26:52.609Z","etag":null,"topics":["abac","access-control","acl","adapter","auth","casbin","database","go","golang","gorm","orm","rbac"],"latest_commit_sha":null,"homepage":"https://github.com/casbin/casbin","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/casbin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null},"funding":{"github":"casbin"}},"created_at":"2022-08-26T14:26:52.000Z","updated_at":"2024-08-14T10:45:37.000Z","dependencies_parsed_at":"2022-09-11T06:51:01.710Z","dependency_job_id":"e000f467-0430-4eeb-b9a6-6941932bf973","html_url":"https://github.com/casbin/gorm-adapter-ex","commit_stats":{"total_commits":94,"total_committers":38,"mean_commits":2.473684210526316,"dds":0.7872340425531915,"last_synced_commit":"474b4560365b406ea7cf008619a445fb7b5e24c7"},"previous_names":[],"tags_count":50,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin%2Fgorm-adapter-ex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin%2Fgorm-adapter-ex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin%2Fgorm-adapter-ex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casbin%2Fgorm-adapter-ex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/casbin","download_url":"https://codeload.github.com/casbin/gorm-adapter-ex/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223779656,"owners_count":17201287,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["abac","access-control","acl","adapter","auth","casbin","database","go","golang","gorm","orm","rbac"],"created_at":"2024-11-09T23:07:23.050Z","updated_at":"2024-11-09T23:07:23.741Z","avatar_url":"https://github.com/casbin.png","language":"Go","funding_links":["https://github.com/sponsors/casbin"],"categories":[],"sub_categories":[],"readme":"Gorm Adapter Ex\n====\nThis gorm-adapter-ex is an extended version of [Gorm-Adapter](https://github.com/casbin/gorm-adapter).\nIt introduces great and implements [proposal of customizability](https://github.com/casbin/gorm-adapter/issues/180) by reflection. The database structure can be arbitrary as long as it has `{\"ID\", \"Ptype\", \"V0\"}` three fields.\n\n```golang\n\ttype TestCasbinRule struct { //arbitrary number of variable fields\n\t\tID    uint   `gorm:\"primaryKey;autoIncrement\"`\n\t\tPtype string `gorm:\"size:16\"`\n\t\tV0    string `gorm:\"size:128\"`\n\t\tV1    string `gorm:\"size:128\"`\n\t\tV2    string `gorm:\"size:256\"`\n\t\tDeletedAt gorm.DeletedAt\n\t}\n\n\t// Create an adapter\n\ta, _ := NewAdapterByDBWithCustomTable(db, \u0026TestCasbinRule{}, \"test_casbin_rule\")\n\t//...\n```\n\nBackground for why we create a new fork:\n\n[casbin/gorm-adapter#179 (comment)](https://github.com/casbin/gorm-adapter/issues/179#issuecomment-1228530939)\n\n[casbin/gorm-adapter#168 (comment)](https://github.com/casbin/gorm-adapter/pull/168#issuecomment-1228556830) \n\n\n# About Gorm Adapter\n\nGorm Adapter is the [Gorm](https://gorm.io/gorm) adapter for [Casbin](https://github.com/casbin/casbin). With this library, Casbin can load policy from Gorm supported database or save policy to it.\n\nBased on [Officially Supported Databases](https://v1.gorm.io/docs/connecting_to_the_database.html#Supported-Databases), The current supported databases are:\n\n- MySQL\n- PostgreSQL\n- SQL Server\n- Sqlite3\n\u003e gorm-adapter use ``github.com/glebarez/sqlite`` instead of gorm official sqlite driver ``gorm.io/driver/sqlite`` because the latter needs ``cgo`` support. But there is almost no difference between the two driver. If there is a difference in use, please submit an issue.\n\n- other 3rd-party supported DBs in Gorm website or other places.\n\n## Installation\n\n    go get github.com/casbin/gorm-adapter/v3\n\n## Simple Example\n\n```go\npackage main\n\nimport (\n\t\"github.com/casbin/casbin/v2\"\n\tgormadapter \"github.com/casbin/gorm-adapter/v3\"\n\t_ \"github.com/go-sql-driver/mysql\"\n)\n\nfunc main() {\n\t// Initialize a Gorm adapter and use it in a Casbin enforcer:\n\t// The adapter will use the MySQL database named \"casbin\".\n\t// If it doesn't exist, the adapter will create it automatically.\n\t// You can also use an already existing gorm instance with gormadapter.NewAdapterByDB(gormInstance)\n\ta, _ := gormadapter.NewAdapter(\"mysql\", \"mysql_username:mysql_password@tcp(127.0.0.1:3306)/\") // Your driver and data source.\n\te, _ := casbin.NewEnforcer(\"examples/rbac_model.conf\", a)\n\t\n\t// Or you can use an existing DB \"abc\" like this:\n\t// The adapter will use the table named \"casbin_rule\".\n\t// If it doesn't exist, the adapter will create it automatically.\n\t// a := gormadapter.NewAdapter(\"mysql\", \"mysql_username:mysql_password@tcp(127.0.0.1:3306)/abc\", true)\n\n\t// Load the policy from DB.\n\te.LoadPolicy()\n\t\n\t// Check the permission.\n\te.Enforce(\"alice\", \"data1\", \"read\")\n\t\n\t// Modify the policy.\n\t// e.AddPolicy(...)\n\t// e.RemovePolicy(...)\n\t\n\t// Save the policy back to DB.\n\te.SavePolicy()\n}\n```\n## Turn off AutoMigrate\nNew 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: \n```go\ndb, err := gorm.Open(mysql.Open(\"root:@tcp(127.0.0.1:3306)/casbin\"), \u0026gorm.Config{})\nTurnOffAutoMigrate(db)\n// a,_ := NewAdapterByDB(...)\n// a,_ := NewAdapterByDBUseTableName(...)\na,_ := NewAdapterByDBWithCustomTable(...)\n```\nFind out more details at [gorm-adapter#162](https://github.com/casbin/gorm-adapter/issues/162)\n## Customize table columns example\nYou can change the gorm struct tags, but the table structure must stay the same.\n```go\npackage main\n\nimport (\n\t\"github.com/casbin/casbin/v2\"\n\tgormadapter \"github.com/casbin/gorm-adapter/v3\"\n\t\"gorm.io/gorm\"\n)\n\nfunc main() {\n\t// Increase the column size to 512.\n\ttype CasbinRule struct {\n\t\tID    uint   `gorm:\"primaryKey;autoIncrement\"`\n\t\tPtype string `gorm:\"size:512;uniqueIndex:unique_index\"`\n\t\tV0    string `gorm:\"size:512;uniqueIndex:unique_index\"`\n\t\tV1    string `gorm:\"size:512;uniqueIndex:unique_index\"`\n\t\tV2    string `gorm:\"size:512;uniqueIndex:unique_index\"`\n\t\tV3    string `gorm:\"size:512;uniqueIndex:unique_index\"`\n\t\tV4    string `gorm:\"size:512;uniqueIndex:unique_index\"`\n\t\tV5    string `gorm:\"size:512;uniqueIndex:unique_index\"`\n\t}\n\n\tdb, _ := gorm.Open(...)\n\n\t// Initialize a Gorm adapter and use it in a Casbin enforcer:\n\t// The adapter will use an existing gorm.DB instnace.\n\ta, _ := gormadapter.NewAdapterByDBWithCustomTable(db, \u0026CasbinRule{}) \n\te, _ := casbin.NewEnforcer(\"examples/rbac_model.conf\", a)\n\t\n\t// Load the policy from DB.\n\te.LoadPolicy()\n\t\n\t// Check the permission.\n\te.Enforce(\"alice\", \"data1\", \"read\")\n\t\n\t// Modify the policy.\n\t// e.AddPolicy(...)\n\t// e.RemovePolicy(...)\n\t\n\t// Save the policy back to DB.\n\te.SavePolicy()\n}\n```\n\n## Getting Help\n\n- [Casbin](https://github.com/casbin/casbin)\n\n## License\n\nThis project is under Apache 2.0 License. See the [LICENSE](LICENSE) file for the full license text.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcasbin%2Fgorm-adapter-ex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcasbin%2Fgorm-adapter-ex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcasbin%2Fgorm-adapter-ex/lists"}