casbin

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2021 License: MIT Imports: 8 Imported by: 0

README

gf-casbin

GoFrame Permission Plugin

Support MySQL, SQLite, PostgreSQL, Oracle, SQL Server Power By GoFrame ORM

Use

Download and install

go get github.com/dobyte/gf-casbin

Demo

package main

import (
	"fmt"
	"log"
	"github.com/dobyte/gf-casbin"
)

func main() {
	enforcer, err := casbin.NewEnforcer(&casbin.Casbin{
		Model:          "./example/model.conf",
		Debug:          false,
		Enable:         true,
		AutoLoad:       true,
		TableName:      "casbin_policy_test",
		DatabaseDriver: "mysql",
		DatabaseSource: "root:123456@tcp(127.0.0.1:3306)/casbin_test",
	})

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

	// add a permission node for role
	ok, err := enforcer.AddPolicy("role_1", "node_1")

	if err != nil {
		log.Fatalf("Add policy exception:%s \n", err.Error())
	}

	if ok {
		log.Println("Add policy successful")
	} else {
		log.Println("Add policy failure")
	}
}

Example

View demo example/main.go

Model Demo

View demo example/model.conf

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMissingDatabaseDriver = errors.New("missing database driver")
	ErrMissingDatabaseSource = errors.New("missing database source")
)

Functions

This section is empty.

Types

type Adapter

type Adapter struct {
	TableName      string
	DatabaseDriver string
	DatabaseSource string
	// contains filtered or unexported fields
}

func NewAdapter

func NewAdapter(a *Adapter) (*Adapter, error)

Create a casbin adapter

func (*Adapter) AddPolicies

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

Adds a policy rule to the storage.

func (*Adapter) AddPolicy

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

Adds a policy rule to the storage.

func (*Adapter) LoadPolicy

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

Loads all policy rules from the storage.

func (*Adapter) RemoveFilteredPolicy

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

Removes policy rules that match the filter from the storage.

func (*Adapter) RemovePolicies

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

Removes a policy rule from the storage.

func (*Adapter) RemovePolicy

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

Removes a policy rule from the storage.

func (*Adapter) SavePolicy

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

Saves all policy rules to the storage.

func (*Adapter) UpdatePolicies

func (a *Adapter) UpdatePolicies(sec string, ptype string, oldPolicies, newPolicies [][]string) error

Updates some policy rules to storage, like db, redis.

func (*Adapter) UpdatePolicy

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

Updates a policy rule from storage.

type Casbin

type Casbin 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
	TableName      string        // policy table name
	DatabaseDriver string        // database driver,support MySQL, SQLite, PostgreSQL, Oracle, SQL Server
	DatabaseSource string        // database source url
}

type Enforcer

type Enforcer = casbin.Enforcer

func NewEnforcer

func NewEnforcer(c *Casbin) (*Enforcer, error)

Create a casbin enforcer

type Rule

type Rule struct {
	PType string `json:"ptype"`
	V0    string `json:"v0"`
	V1    string `json:"v1"`
	V2    string `json:"v2"`
	V3    string `json:"v3"`
	V4    string `json:"v4"`
	V5    string `json:"v5"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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