gormadapter

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 7, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

README

Gorm Adapter

Go Report Card Godoc Release

Gorm Adapter is the Gorm adapter for FastAC. With this library, FastAC can load policy from Gorm supported database or save policy to it.

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

  • MySQL
  • PostgreSQL
  • SQL Server
  • SqLite3

You may find other 3rd-party supported DBs in Gorm website or other places.

Installation

go get github.com/abichinger/gorm-adapter

Simple Example

package main

import (
	"github.com/abichinger/fastac"
	gormadapter "github.com/abichinger/grom-adapter"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func main() {
	// Initialize a Gorm adapter and use it in a FastAC enforcer:
	// The adapter will use "fastac_rules" as the default table name.
	// If it doesn't exist, the adapter will create the table automatically.
	dsn := "user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"
  	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	a, _ := gormadapter.NewAdapter(db)
	//a := gormadapter.NewAdapterWithTable(db, "my_tablename") //It is also possible to specify your own table name
	e, _ := fastac.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()
}

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

func NewAdapter

func NewAdapter(db *gorm.DB) (*Adapter, error)

NewAdapterWithDB creates gorm-adapter by an existing Gorm instance

func NewAdapterWithTable added in v1.0.1

func NewAdapterWithTable(db *gorm.DB, tableName string) (*Adapter, error)

NewAdapterByDBUseTableName creates gorm-adapter by an existing Gorm instance and the specified table prefix and table name Example: gormadapter.NewAdapterByDBUseTableName(&db, "cms", "casbin") Automatically generate table name like this "cms_casbin"

func (*Adapter) AddLogger added in v1.0.1

func (a *Adapter) AddLogger(l logger.Interface)

AddLogger adds logger to db

func (*Adapter) AddRule added in v1.0.1

func (a *Adapter) AddRule(rule []string) error

AddPolicy adds a policy rule to the storage.

func (*Adapter) AddRules added in v1.0.1

func (a *Adapter) AddRules(rules [][]string) error

AddPolicies adds multiple policy rules to the storage.

func (*Adapter) Close added in v1.0.1

func (a *Adapter) Close() error

func (*Adapter) IsFiltered

func (a *Adapter) IsFiltered() bool

IsFiltered returns true if the loaded policy has been filtered.

func (*Adapter) LoadPolicy

func (a *Adapter) LoadPolicy(model api.IAddRuleBool) error

LoadPolicy loads policy from database.

func (*Adapter) RemoveRule added in v1.0.1

func (a *Adapter) RemoveRule(rule []string) error

RemovePolicy removes a policy rule from the storage.

func (*Adapter) RemoveRules added in v1.0.1

func (a *Adapter) RemoveRules(rules [][]string) error

RemovePolicies removes multiple policy rules from the storage.

func (*Adapter) SavePolicy

func (a *Adapter) SavePolicy(model api.IRangeRules) error

SavePolicy saves policy to database.

type CasbinRule

type CasbinRule struct {
	ID    uint   `gorm:"primaryKey;autoIncrement"`
	Ptype string `gorm:"size:100"`
	V0    string `gorm:"size:100"`
	V1    string `gorm:"size:100"`
	V2    string `gorm:"size:100"`
	V3    string `gorm:"size:100"`
	V4    string `gorm:"size:100"`
	V5    string `gorm:"size:100"`
	V6    string `gorm:"size:25"`
	V7    string `gorm:"size:25"`
}

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
}

Jump to

Keyboard shortcuts

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