adapter

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2021 License: MIT Imports: 10 Imported by: 0

README

Installation

go get github.com/kotlin2018/adapter

Usage example

package mian

import (
	"github.com/casbin/casbin/v2"
	"github.com/gogf/gf/frame/g"
	"github.com/kotlin2018/adapter"
)

func main() {
	opts := &adapter.Adapter{
		DriverName: "mysql",
		LinkInfo:   "root:root@tcp(127.0.0.1:3306)/casbin",
		TableName:  "casbin_rule",
	}

	c := &adapter.CasBinModel{
		BaseAdapter: opts,
		ModelPath:   "examples/rbac_model.conf",
	}
	// 添加权限
	c.AuthorityId = "100"
	c.Path = "v1/user"
	c.Method = "POST"
	c.AddCasBin()
	
	// 清除匹配的权限
	c.ClearCasBin()
	
	// 更新casBin权限
	c.AuthorityId = "101"
	c.ReqPolicies = []adapter.CasBinInfo{}
	c.UpdateCasBin()
	
	// 获取权限列表
	c.GetCasBinList()
	
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParamsMatch added in v0.1.2

func ParamsMatch(fullNameKey1, key2 string) interface{}

ParamsMatch 自定义规则函数

func ParamsMatchFunc added in v0.1.2

func ParamsMatchFunc(args ...interface{}) (interface{}, error)

ParamsMatchFunc 自定义规则函数

Types

type Adapter

type Adapter struct {
	DriverName string // 数据库名,例如: Mysql,PostgreSQL
	LinkInfo   string // 链接到数据库的信息,例如: "root:root@tcp(127.0.0.1:3306)/casbin"
	TableName  string // 存储策略的表
	DB         gdb.DB // 操作数据库的ORM句柄
}

Adapter 用于策略存储的gdb适配器。

func NewAdapter

func NewAdapter(driverName string, dataSourceName string) (*Adapter, error)

NewAdapter is the constructor for Adapter.

func NewAdapterFromOptions

func NewAdapterFromOptions(adapter *Adapter) (*Adapter, error)

NewAdapterFromOptions is the constructor for Adapter with existed connection

func (*Adapter) AddPolicy

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

AddPolicy adds a policy rule to the storage.

func (*Adapter) LoadPolicy

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

LoadPolicy loads policy from database.

func (*Adapter) RemoveFilteredPolicy

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

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

func (*Adapter) RemovePolicy

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

RemovePolicy removes a policy rule from the storage.

func (*Adapter) SavePolicy

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

SavePolicy saves policy to database.

type CasBinInfo added in v0.1.2

type CasBinInfo struct {
	Path   string `p:"path" json:"path"`     // 被操作的资源(即: url路径),对应: obj
	Method string `p:"method" json:"method"` // 操作的方法,例如: GET、POST,对应: act
}

CasBinInfo 请求参数

type CasBinModel added in v0.1.2

type CasBinModel struct {
	ID          uint   `json:"id"`
	PType       string `json:"pType"`    // policy缩写,对应: p
	AuthorityId string `json:"roleName"` // 操作者ID,对应: sub
	Path        string `json:"path"`     // 被操作的资源(即: url路径),对应: obj
	Method      string `json:"method"`   // 操作的方法,例如: GET、POST,对应: act
	BaseAdapter *Adapter
	ReqPolicies []CasBinInfo
	ModelPath   string `json:"modelPath"` // rbac_model.conf 文件路径
}

ReqCasBin 请求参数模型,用于对数据库中policy文件,增、删、改、查。

策略文件 p = sub, obj, act

func (*CasBinModel) AddCasBin added in v0.1.2

func (c *CasBinModel) AddCasBin() (*casbin.Enforcer, bool)

AddCasBin 添加权限(添加策略规则)

func (*CasBinModel) CasBin added in v0.1.2

func (c *CasBinModel) CasBin() *casbin.Enforcer

持久化到数据库,引入自定义规则

func (*CasBinModel) ClearCasBin added in v0.1.2

func (c *CasBinModel) ClearCasBin(v int, p ...string) (*casbin.Enforcer, bool)

ClearCasBin 清除匹配的权限

从当前策略中删除授权规则,可以指定字段过滤器。底层调用了e.RemoveFilteredPolicy()

func (*CasBinModel) GetCasBinList added in v0.1.2

func (c *CasBinModel) GetCasBinList() (e *casbin.Enforcer, pathMaps []CasBinInfo)

GetCasBinList 获取权限列表

func (*CasBinModel) UpdateCasBin added in v0.1.2

func (c *CasBinModel) UpdateCasBin() (*casbin.Enforcer, error)

UpdateCasBin 更新casBin权限

Jump to

Keyboard shortcuts

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