casbinbunadapter

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2024 License: MIT Imports: 16 Imported by: 0

README

Casbin Bun Adapter

GitHub release (latest by date) GitHub test Go Report Card

📖 Overview

casbin-bun-adapter is the Bun ORM adapter for Casbin.

🙌 Supported DB

The following databases supported by Bun are also supported by this adapter

  1. MySQL
  2. PostgreSQL
  3. Microsoft SQL Server
  4. SQLite

💻 Installation

go get github.com/JunNishimura/casbin-bun-adapter

👀 Example

package main

import (
	casbinbunadapter "github.com/JunNishimura/casbin-bun-adapter"
	"github.com/casbin/casbin/v2"
)

func main() {
	// initialize a Bun adapter and use it in a Casbin enforcer
	a, _ := casbinbunadapter.NewAdapter("mysql", "mysql_username:mysql_password@tcp(127.0.0.1:3306)/database")
	e, _ := casbin.NewEnforcer("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(...)
	// e.UpdatePolicy(...)

	// save the policy back to DB.
	_ = e.SavePolicy()
}

😢 Limitations

casbin-bun-adapter has following limitations.

1. Table names cannot be freely specified

To specify the table name in Bun, you need to specify it in a structure tag or call the ModelTableExpr method in the query builder.

type User struct {
  bun.BaseModel `bun:"table:users,alias:u"`
  ID    int64  `bun:"id,pk,autoincrement"`
  Name  string `bun:"name,notnull"`
}
res, err := db.NewInsert().
    Model(user).
    ModelTableExpr("custom_name") // specify table name
    Exec(ctx)

If you want to create a table with a name specified by the user, you can use ModelTableExpr, but I gave up using ModelTableExpr because I found that query build to tuncate table does not support ModelTableExpr.

If we come up with a better approach, or if Bun's specifications regarding the above change, we will modify this one accordingly.

2. Unique indexes cannot be added on columns in the casbin_policies table

For Postgres, you can specify IF NOT EXISTS to create a key only when the key does not exist, but other DBs do not support the above syntax by default.

There seems to be no way to check if the index is posted in Bun.

If I find the way to check if the index exists with SQL, it might be possible to achieve this, so I'll leave this as a future issue.

🙇‍♂️ Thanks

I would like to express my appreciation to Gorm Adapter, since casbin-bun-adapter is implemented in a way that fits the Bun ORM based on it.

🪧 License

casbin-bun-adapter is released under MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAdapter

func NewAdapter(driverName, dataSourceName string, opts ...adapterOption) (*bunAdapter, error)

func NewAdapterWithBunDB added in v1.0.1

func NewAdapterWithBunDB(db *bun.DB, opts ...adapterOption) (*bunAdapter, error)

func NewAdapterWithSqlDB added in v1.0.1

func NewAdapterWithSqlDB(sqlDB *sql.DB, driverName string, opts ...adapterOption) (*bunAdapter, error)

func NewCtxAdapter

func NewCtxAdapter(driverName string, dataSourceName string, opts ...adapterOption) (*ctxBunAdapter, error)

func WithDebugMode

func WithDebugMode() adapterOption

Types

type CasbinPolicy

type CasbinPolicy struct {
	bun.BaseModel `bun:"casbin_policies,alias:cp"`
	ID            int64  `bun:"id,pk,autoincrement"`
	PType         string `bun:"ptype,type:varchar(100),notnull"`
	V0            string `bun:"v0,type:varchar(100)"`
	V1            string `bun:"v1,type:varchar(100)"`
	V2            string `bun:"v2,type:varchar(100)"`
	V3            string `bun:"v3,type:varchar(100)"`
	V4            string `bun:"v4,type:varchar(100)"`
	V5            string `bun:"v5,type:varchar(100)"`
}

Database storage format following the below https://casbin.org/docs/policy-storage#database-storage-format

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL