Skip to content

Commit

Permalink
support goframe v2.0,support link parameters or db instance.
Browse files Browse the repository at this point in the history
  • Loading branch information
dobyte committed Mar 29, 2022
1 parent ce9cc36 commit 742aa16
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 55 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func main() {
Debug: false,
Enable: true,
AutoLoad: true,
DbTable: "casbin_policy_test",
DbLink: "mysql:root:123456@tcp(127.0.0.1:3306)/topic1",
Table: "casbin_policy_test",
Link: "mysql:root:123456@tcp(127.0.0.1:3306)/topic1",
})

if err != nil {
Expand Down
63 changes: 28 additions & 35 deletions adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,22 @@ import (

"github.com/casbin/casbin/v2/model"
"github.com/casbin/casbin/v2/persist"
"github.com/gogf/gf/database/gdb"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/database/gdb"
)

const (
defaultGroupName = "casbin_database"
defaultTableName = "casbin_policy"
dropPolicyTableSql = `DROP TABLE IF EXISTS %s`
createPolicyTableSql = `
CREATE TABLE IF NOT EXISTS %s (
ptype VARCHAR(10) NOT NULL DEFAULT '' COMMENT '',
v0 VARCHAR(256) NOT NULL DEFAULT '' COMMENT '',
v1 VARCHAR(256) NOT NULL DEFAULT '' COMMENT '',
v2 VARCHAR(256) NOT NULL DEFAULT '' COMMENT '',
v3 VARCHAR(256) NOT NULL DEFAULT '' COMMENT '',
v4 VARCHAR(256) NOT NULL DEFAULT '' COMMENT '',
v5 VARCHAR(256) NOT NULL DEFAULT '' COMMENT ''
) ENGINE = InnoDB COMMENT = 'policy table';
ptype VARCHAR(10) NOT NULL DEFAULT '',
v0 VARCHAR(256) NOT NULL DEFAULT '',
v1 VARCHAR(256) NOT NULL DEFAULT '',
v2 VARCHAR(256) NOT NULL DEFAULT '',
v3 VARCHAR(256) NOT NULL DEFAULT '',
v4 VARCHAR(256) NOT NULL DEFAULT '',
v5 VARCHAR(256) NOT NULL DEFAULT ''
) COMMENT = 'policy table';
`
)

Expand Down Expand Up @@ -71,51 +70,45 @@ type (
)

// Create a casbin adapter
func newAdapter(link, table string, debug bool) (*adapter, error) {
config := strings.SplitN(link, ":", 2)
func newAdapter(db gdb.DB, link, table string) (adp *adapter, err error) {
adp = &adapter{db, table}

if len(config) != 2 {
return nil, ErrInvalidDatabaseLink
}
if adp.db == nil {
config := strings.SplitN(link, ":", 2)

gdb.SetConfigGroup(defaultGroupName, gdb.ConfigGroup{
gdb.ConfigNode{
Debug: debug,
Type: config[0],
Link: config[1],
},
})
if len(config) != 2 {
err = ErrInvalidDatabaseLink
return
}

if table == "" {
table = defaultTableName
if adp.db, err = gdb.New(gdb.ConfigNode{Type: config[0], Link: config[1]}); err != nil {
return
}
}

a := &adapter{
db: g.DB(defaultGroupName),
table: table,
if adp.table == "" {
adp.table = defaultTableName
}

if err := a.createPolicyTable(); err != nil {
return nil, err
}
err = adp.createPolicyTable()

return a, nil
return
}

func (a *adapter) model() *gdb.Model {
return a.db.Model(a.table).Safe()
return a.db.Model(a.table).Safe().Ctx(context.TODO())
}

// create a policy table when it's not exists.
func (a *adapter) createPolicyTable() (err error) {
_, err = a.db.Exec(fmt.Sprintf(createPolicyTableSql, a.table))
_, err = a.db.Exec(context.TODO(), fmt.Sprintf(createPolicyTableSql, a.table))

return
}

// drop policy table from the storage.
func (a *adapter) dropPolicyTable() (err error) {
_, err = a.db.Exec(fmt.Sprintf("DROP TABLE IF EXISTS %s", a.table))
_, err = a.db.Exec(context.TODO(), fmt.Sprintf(dropPolicyTableSql, a.table))

return
}
Expand Down
28 changes: 16 additions & 12 deletions enforcer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,29 @@ import (
"time"

"github.com/casbin/casbin/v2"
"github.com/gogf/gf/v2/database/gdb"
)

type Enforcer = casbin.Enforcer

type Options struct {
Model string // model config file path
Debug bool // debug mode
Enable bool // enable permission
AutoLoad bool // auto load policy
Duration time.Duration // auto load duration
DbTable string // policy table name
DbLink string // database source url, example: mysql:root:12345678@tcp(127.0.0.1:3306)/test
}
type (
Enforcer = casbin.Enforcer

Options struct {
Model string // model config file path
Debug bool // debug mode
Enable bool // enable permission
AutoLoad bool // auto load policy
Duration time.Duration // auto load duration
DB gdb.DB // database instance, Choose between DB and Link parameters. If DB exists, use DB first.
Link string // database source url, Choose between DB and Link parameters. If the DB parameter does not exist, create a DB instance with the Link parameter. example: mysql:root:12345678@tcp(127.0.0.1:3306)/test
Table string // database policy table name
}
)

// NewEnforcer create a casbin enforcer.
func NewEnforcer(opt *Options) (enforcer *Enforcer, err error) {
var adp *adapter

if adp, err = newAdapter(opt.DbLink, opt.DbTable, opt.Debug); err != nil {
if adp, err = newAdapter(opt.DB, opt.Link, opt.Table); err != nil {
return
}

Expand Down
28 changes: 26 additions & 2 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,43 @@ import (
"fmt"
"log"

"github.com/gogf/gf/v2/database/gdb"

"github.com/dobyte/gf-casbin"
)

var enforcer *casbin.Enforcer

func init() {
gdb.SetConfig(gdb.Config{
"default": gdb.ConfigGroup{
gdb.ConfigNode{
Host: "127.0.0.1",
Port: "3306",
User: "root",
Pass: "123456",
Name: "topic1",
Type: "mysql",
Role: "master",
Weight: 100,
},
},
})

db, err := gdb.Instance()
if err != nil {
log.Fatalf("Database init failure:%s \n", err.Error())
}

e, err := casbin.NewEnforcer(&casbin.Options{
Model: "./example/model.conf",
Debug: true,
Enable: true,
AutoLoad: true,
DbTable: "casbin_policy_test",
DbLink: "mysql:root:123456@tcp(127.0.0.1:3306)/topic1",
Table: "casbin_policy_test",
DB: db,
// Link: "mysql:root:123456@tcp(127.0.0.1:3306)/topic1",
// Link: "pgsql:user=root password=123456 host=127.0.0.1 port=5432 dbname=topic1",
})

if err != nil {
Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
module github.com/dobyte/gf-casbin

go 1.15
go 1.16

require (
github.com/casbin/casbin/v2 v2.41.0
github.com/gogf/gf v1.16.6
github.com/gomodule/redigo v2.0.0+incompatible // indirect
github.com/mattn/go-runewidth v0.0.10 // indirect
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.0.0-20220319145450-d1f76f3834e1
github.com/gogf/gf/v2 v2.0.0
)

0 comments on commit 742aa16

Please sign in to comment.