casbin_gorose_adapter

package module
v0.0.0-...-17d5fd4 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2020 License: Apache-2.0 Imports: 5 Imported by: 0

README

casbin-gorose-adapter

the gorose adapter for casbin

casbin-gorose-adapter is the gorose adapter for Casbin. With this library, Casbin can load policy from gorose supported database or save policy to it.

Based on Officially Supported Databases, The current supported databases are:

MySQL
PostgreSQL
Sqlite3
SQL Server

Installation

go get github.com/gohouse/casbin-gorose-adapter

simple example

package main

import (
	"github.com/casbin/casbin/v2"
	cga "github.com/gohouse/casbin-gorose-adapter"
	"github.com/gohouse/gorose/v2"
	_ "github.com/go-sql-driver/mysql"
)

func main() {
	// init db orm
	engin, _ := gorose.Open(&gorose.Config{Driver: "mysql", 
		Dsn: "root:root@tcp(localhost:3306)/test?charset=utf8&parseTime=true"})

	// init casbin adapter
	// model can use in source code as `github.com/casbin/casbin/v2/model.NewModel().AddDef("r","r","sub, obj, act")`
	e, _ := casbin.NewEnforcer("./model.conf", cga.NewAdapter(engin))

	sub, obj, act := "fizz", "data1", "read"

	// Check permission.
	e.Enforce(sub, obj, act)

	// Load the policy from DB
	e.LoadPolicy()

	//e.AddPolicy()
	//
	//e.RemovePolicy()
	//
	//e.SavePolicy()
}

generate table

now, only mysql driver can auto generate table, the table sql like :

CREATE TABLE IF NOT EXISTS casbin_rule (
 id int(11) NOT NULL AUTO_INCREMENT,
 p_type varchar(32) NOT NULL DEFAULT '' COMMENT 'perm类型:p,g......',
 v0 varchar(64) NOT NULL DEFAULT '' COMMENT '角色名字(rbac),其他角色类型则依次存放v0-v5...',
 v1 varchar(64) NOT NULL DEFAULT '' COMMENT '对象资源(rbac),其他角色类型则依次存放v0-v5...',
 v2 varchar(64) NOT NULL DEFAULT '' COMMENT '权限值(rbac),其他角色类型则依次存放v0-v5...',
 v3 varchar(64) NOT NULL DEFAULT '' COMMENT 'ext',
 v4 varchar(64) NOT NULL DEFAULT '' COMMENT 'ext',
 v5 varchar(64) NOT NULL DEFAULT '' COMMENT 'ext',
 PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='casbin权限规则表';

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CasbinGoroseAdapter

type CasbinGoroseAdapter struct {
	*gorose.Engin
}

Adapter is the file adapter for Casbin. It can load policy from file or save policy to file.

func NewAdapter

func NewAdapter(ge *gorose.Engin) *CasbinGoroseAdapter

NewCasbinGoroseAdapter is the constructor for Adapter.

func (*CasbinGoroseAdapter) AddPolicy

func (a *CasbinGoroseAdapter) AddPolicy(sec string, ptype string, rule []string) error

AddPolicy adds a policy rule to the storage.

func (*CasbinGoroseAdapter) LoadPolicy

func (a *CasbinGoroseAdapter) LoadPolicy(model model.Model) error

LoadPolicy loads all policy rules from the storage.

func (*CasbinGoroseAdapter) RemoveFilteredPolicy

func (a *CasbinGoroseAdapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error

RemoveFilteredPolicy removes policy rules that match the filter from the storage.

func (*CasbinGoroseAdapter) RemovePolicy

func (a *CasbinGoroseAdapter) RemovePolicy(sec string, ptype string, rule []string) error

RemovePolicy removes a policy rule from the storage.

func (*CasbinGoroseAdapter) SavePolicy

func (a *CasbinGoroseAdapter) SavePolicy(model model.Model) error

SavePolicy saves all policy rules to the storage.

type CasbinRule

type CasbinRule struct {
	Id    string `gorose:"id"`
	PType string `gorose:"p_type"`
	V0    string `gorose:"v0"`
	V1    string `gorose:"v1"`
	V2    string `gorose:"v2"`
	V3    string `gorose:"v3"`
	V4    string `gorose:"v4"`
	V5    string `gorose:"v5"`
}

CasbinRule casbin_rule table

func (CasbinRule) TableName

func (CasbinRule) TableName() string

TableName return table's true name

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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