gdbadapter

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

README

Gorm Adapter

Gdb Adapter is the goframe orm adapter for Casbin.

Installation

go get github.com/jxo-me/gdb-adapter

Simple Example

package main

import (
	"context"
	"github.com/casbin/casbin/v2"
	_ "github.com/gogf/gf/contrib/drivers/mysql/v2"
	"github.com/gogf/gf/v2/database/gdb"
	gdbadapter "github.com/jxo-me/gdb-adapter"
)

func main() {
	// Initialize a gdb adapter and use it in a Casbin enforcer:
	// The adapter will use the MySQL database named "casbin".
	// If it doesn't exist, the adapter will create it automatically.
	ctx := context.Background()
	a, _ := gdbadapter.NewAdapter(ctx, gdb.DefaultGroupName) // Your driver and data source.
	e, _ := casbin.NewEnforcer("examples/rbac_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(...)

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

Getting Help

License

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Adapter

type Adapter struct {
	// contains filtered or unexported fields
}

Adapter represents the Gorm adapter for policy store.

func NewAdapter

func NewAdapter(ctx context.Context, groupName string) (*Adapter, error)

NewAdapter is the constructor for Adapter.

func (*Adapter) AddPolicies

func (a *Adapter) AddPolicies(sec string, ptype string, rules [][]string) error

AddPolicies adds multiple policy rules to the store.

func (*Adapter) AddPolicy

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

AddPolicy adds a policy rule to the store.

func (*Adapter) HasTable

func (a *Adapter) HasTable(name string) (bool, error)

HasTable determine whether the table name exists in the database.

func (*Adapter) IsFiltered

func (a *Adapter) IsFiltered() bool

IsFiltered returns true if the loaded policy has been filtered.

func (*Adapter) LoadFilteredPolicy

func (a *Adapter) LoadFilteredPolicy(model model.Model, filter interface{}) error

LoadFilteredPolicy loads only policy rules that match the filter.

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 store.

func (*Adapter) RemovePolicies

func (a *Adapter) RemovePolicies(sec string, ptype string, rules [][]string) error

RemovePolicies removes multiple policy rules from the store.

func (*Adapter) RemovePolicy

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

RemovePolicy removes a policy rule from the store.

func (*Adapter) SavePolicy

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

SavePolicy saves policy to database.

func (*Adapter) UpdateFilteredPolicies

func (a *Adapter) UpdateFilteredPolicies(sec string, ptype string, newPolicies [][]string, fieldIndex int, fieldValues ...string) ([][]string, error)

func (*Adapter) UpdatePolicies

func (a *Adapter) UpdatePolicies(sec string, ptype string, oldRules, newRules [][]string) error

func (*Adapter) UpdatePolicy

func (a *Adapter) UpdatePolicy(sec string, ptype string, oldRule, newPolicy []string) error

UpdatePolicy updates a new policy rule to DB.

type CasbinRule

type CasbinRule struct {
	ID    uint   `orm:"id" json:"id"`
	PType string `orm:"p_type" json:"p_type"`
	V0    string `orm:"v0" json:"v0"`
	V1    string `orm:"v1" json:"v1"`
	V2    string `orm:"v2" json:"v2"`
	V3    string `orm:"v3" json:"v3"`
	V4    string `orm:"v4" json:"v4"`
	V5    string `orm:"v5" json:"v5"`
	V6    string `orm:"v6" json:"v6"`
	V7    string `orm:"v7" json:"v7"`
}

func (CasbinRule) TableName

func (CasbinRule) TableName() string

type Filter

type Filter struct {
	PType []string
	V0    []string
	V1    []string
	V2    []string
	V3    []string
	V4    []string
	V5    []string
	V6    []string
	V7    []string
}

type Logger

type Logger struct {
	Enable int32
	Ctx    context.Context
	Log    *glog.Logger
}

Logger is the implementation for a Logger using golang log.

func (*Logger) EnableLog

func (l *Logger) EnableLog(enable bool)

EnableLog controls whether print the message.

func (*Logger) IsEnabled

func (l *Logger) IsEnabled() bool

IsEnabled returns if logger is enabled.

func (*Logger) LogEnforce

func (l *Logger) LogEnforce(matcher string, request []interface{}, result bool, explains [][]string)

LogEnforce log info related to enforce.

func (*Logger) LogError added in v1.0.4

func (l *Logger) LogError(err error, msg ...string)

func (*Logger) LogModel

func (l *Logger) LogModel(model [][]string)

LogModel log info related to model.

func (*Logger) LogPolicy

func (l *Logger) LogPolicy(policy map[string][][]string)

LogPolicy log info related to policy.

func (*Logger) LogRole

func (l *Logger) LogRole(roles []string)

LogRole log info related to role.

Jump to

Keyboard shortcuts

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