MySQL adapter for Casbin

Clone this repo:

Branches

  1. acfbf28 Provide empty implementation for three optional methods. by Yang Luo · 9 years ago master
  2. 86da1f8 Simplify the error handling code. by Yang Luo · 9 years ago
  3. e102b46 Rename DBAdapter to Adapter. by Yang Luo · 9 years ago
  4. 3b1e38d Comment on the trick in test. by Yang Luo · 9 years ago
  5. f2b0732 Explain the database name in README. by Yang Luo · 9 years ago

MySQL Adapter Build Status Coverage Status Godoc

MySQL Adapter is the MySQL DB adapter for Casbin. With this library, Casbin can load policy from MySQL or save policy to it.

Installation

go get github.com/casbin/mysql-adapter

Simple Example

package main

import (
	"github.com/casbin/casbin"
	"github.com/casbin/mysql-adapter"
)

func main() {
	// Initialize a MySQL adapter and use it in a Casbin enforcer:
	// The adapter will use the database named casbin.
	// If it doesn't exist, the adapter will create it automatically.
	a := mysqladapter.NewAdapter("mysql", "mysql_username:mysql_password@tcp(127.0.0.1:3306)/") // Your MySQL driver and data source. 
	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()
}

Getting Help

License

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