Skip to content

Blank-Xu/sqlx-adapter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sqlx-adapter

Go Report Card Build Status Coverage Status PkgGoDev Release Sourcegraph License


The sqlx-adapter is a sqlx adapter for Casbin V2.

With this library, Casbin can load policy lines from sqlx supported databases or save policy lines.

It is suggest to use sql-adapter. There is a simple example to show how to use sql-adapter with sqlx.

Tested Databases

master branch

oracle branch

Installation

go get github.com/Blank-Xu/sqlx-adapter

Simple Examples

SQLite3

package main

import (
    "log"

    sqlxadapter "github.com/Blank-Xu/sqlx-adapter"
    "github.com/casbin/casbin/v2"
    "github.com/jmoiron/sqlx"
    _ "github.com/mattn/go-sqlite3"
)

func main() {
    // connect to the database first.
    db, err := sqlx.Connect("sqlite3", "file:test.db")
    if err != nil {
        panic(err)
    }

    // Initialize a Sqlx adapter and use it in a Casbin enforcer:
    // The adapter will use the Sqlite3 table name "casbin_rule_test",
    // the default table name is "casbin_rule".
    // If it doesn't exist, the adapter will create it automatically.
    a, err := sqlxadapter.NewAdapter(db, "casbin_rule_test")
    if err != nil {
        panic(err)
    }

    e, err := casbin.NewEnforcer("test/testdata/rbac_model.conf", a)
    if err != nil {
        panic(err)
    }

    // Load the policy from DB.
    if err = e.LoadPolicy(); err != nil {
        log.Println("LoadPolicy failed, err: ", err)
    }

    // Check the permission.
    has, err := e.Enforce("alice", "data1", "read")
    if err != nil {
        log.Println("Enforce failed, err: ", err)
    }
    if !has {
        log.Println("do not have permission")
    }

    // Modify the policy.
    // e.AddPolicy(...)
    // e.RemovePolicy(...)

    // Save the policy back to DB.
    if err = e.SavePolicy(); err != nil {
        log.Println("SavePolicy failed, err: ", err)
    }
}

MySQL

package main

import (
    "log"
    "runtime"
    "time"

    sqlxadapter "github.com/Blank-Xu/sqlx-adapter"
    "github.com/casbin/casbin/v2"
    _ "github.com/go-sql-driver/mysql"
    "github.com/jmoiron/sqlx"
)

func finalize(db *sqlx.DB) {
    if db == nil {
        return
    }
    err := db.Close()
    if err != nil {
        panic(err)
    }
}

func main() {
    // connect to the database first.
    db, err := sqlx.Connect("mysql", "YourUserName:YourPassword@tcp(127.0.0.1:3306)/YourDBName")
    if err != nil {
        panic(err)
    }

    db.SetMaxOpenConns(20)
    db.SetMaxIdleConns(10)
    db.SetConnMaxLifetime(time.Minute * 10)

    // need to control by user, not the package
    runtime.SetFinalizer(db, finalize)

    // Initialize a Sqlx adapter and use it in a Casbin enforcer:
    // The adapter will use the Sqlite3 table name "casbin_rule_test",
    // the default table name is "casbin_rule".
    // If it doesn't exist, the adapter will create it automatically.
    a, err := sqlxadapter.NewAdapter(db, "casbin_rule_test")
    if err != nil {
        panic(err)
    }

    e, err := casbin.NewEnforcer("test/testdata/rbac_model.conf", a)
    if err != nil {
        panic(err)
    }

    // Load the policy from DB.
    if err = e.LoadPolicy(); err != nil {
        log.Println("LoadPolicy failed, err: ", err)
    }

    // Check the permission.
    has, err := e.Enforce("alice", "data1", "read")
    if err != nil {
        log.Println("Enforce failed, err: ", err)
    }
    if !has {
        log.Println("do not have permission")
    }

    // Modify the policy.
    // e.AddPolicy(...)
    // e.RemovePolicy(...)

    // Save the policy back to DB.
    if err = e.SavePolicy(); err != nil {
        log.Println("SavePolicy failed, err: ", err)
    }
}

Getting Help

License

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