gorm-adapter

Gorm adapter for Casbin

Github星跟踪图

Gorm Adapter

Go Report Card
Build Status
Coverage Status
Godoc
Release
Gitter
Sourcegraph

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:

  • MySQL
  • PostgreSQL
  • Sqlite3
  • SQL Server

You may find other 3rd-party supported DBs in Gorm website or other places.

Installation

go get github.com/casbin/gorm-adapter

Simple Example

package main

import (
	"github.com/casbin/casbin/v2"
	"github.com/casbin/gorm-adapter/v2"
	_ "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()
}

Getting Help

License

This project is under Apache 2.0 License. See the LICENSE file for the full license text.

主要指标

概览
名称与所有者casbin/gorm-adapter
主编程语言Go
编程语言Go (语言数: 1)
平台
许可证Apache License 2.0
所有者活动
创建于2017-07-29 02:34:31
推送于2024-11-14 13:38:33
最后一次提交2024-11-14 21:31:43
发布数78
最新版本名称v3.32.0 (发布于 )
第一版名称v1.0.0 (发布于 )
用户参与
星数708
关注者数6
派生数210
提交数168
已启用问题?
问题数116
打开的问题数1
拉请求数95
打开的拉请求数0
关闭的拉请求数42
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?