casbin

package module
v0.0.0-...-9b41c58 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2023 License: MIT Imports: 10 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.Options{
		Model:     "./example/model.conf",
		Debug:     false,
		Enable:    true,
		AutoLoad:  true,
		Table:   "casbin_policy_test",
		Link:    "mysql:root:123456@tcp(127.0.0.1:3306)/topic1",
	})

	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 (
	ErrInvalidDatabaseLink = errors.New("invalid database link")
)

Functions

This section is empty.

Types

type Enforcer

type Enforcer = casbin.Enforcer

func NewEnforcer

func NewEnforcer(opt *Options) (enforcer *Enforcer, err error)

NewEnforcer create a casbin enforcer.

type Options

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
	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
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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