sqladapter

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2024 License: Apache-2.0 Imports: 9 Imported by: 17

README

sql-adapter

Go Report Card Build Status Coverage Status PkgGoDev Release Sourcegraph License


The sql-adapter is a database/sql adapter for Casbin v2.

With this library, Casbin can load policy lines or save policy lines from supported databases.

Tested Databases

master branch
oracle branch

Installation

go get github.com/Blank-Xu/sql-adapter

Examples

Simple example for MySQL
package main

import (
    "database/sql"
    "log"
    "runtime"
    "time"

    sqladapter "github.com/Blank-Xu/sql-adapter"
    "github.com/casbin/casbin/v2"
    _ "github.com/go-sql-driver/mysql"
)

func main() {
    // connect to the database first.
    db, err := sql.Open("mysql", "YourUserName:YourPassword@tcp(127.0.0.1:3306)/YourDBName?charset=utf8")
    if err != nil {
        panic(err)
    }
    if err = db.Ping();err!=nil{
        panic(err)
    }
    defer db.Close()

    db.SetMaxOpenConns(20)
    db.SetMaxIdleConns(10)
    db.SetConnMaxLifetime(time.Minute * 10)

    // Initialize an adapter and use it in a Casbin enforcer:
    // The adapter will use the MySQL table name "casbin_rule_test",
    // the default table name is "casbin_rule" if it is not given.
    // If it doesn't exist, the adapter will create it automatically.
    a, err := sqladapter.NewAdapter(db, "mysql", "casbin_rule_test")
    if err != nil {
        panic(err)
    }

    e, err := casbin.NewEnforcer("test/testdata/rbac_model.conf", a)
    if err != nil {
        panic(err)
    }

    // Load the policies from DB.
    if err = e.LoadPolicy(); err != nil {
        log.Println("LoadPolicy failed, err: ", err)
    }

    // Check the permission.
    has, err := e.Enforce("alice", "data1", "read")
    if err != nil {
        log.Println("Enforce failed, err: ", err)
    }
    if !has {
        log.Println("do not have permission")
    }

    // Modify the policy.
    // e.AddPolicy(...)
    // e.RemovePolicy(...)

    // Save the policy back to DB.
    if err = e.SavePolicy(); err != nil {
        log.Println("SavePolicy failed, err: ", err)
    }
}

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 defines the database adapter for Casbin. It can load policy lines from connected database or save policy lines.

func NewAdapter

func NewAdapter(db *sql.DB, driverName, tableName string) (*Adapter, error)

NewAdapter the constructor for Adapter. db should connected to database and controlled by user. If tableName == "", the Adapter will automatically create a table named "casbin_rule".

func NewAdapterWithContext

func NewAdapterWithContext(ctx context.Context, db *sql.DB, driverName, tableName string) (*Adapter, error)

NewAdapterWithContext the constructor for Adapter. db should connected to database and controlled by user. If tableName == "", the Adapter will automatically create a table named "casbin_rule".

func (Adapter) AddPolicies

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

AddPolicies add multiple policy rules to the storage.

func (Adapter) AddPoliciesCtx added in v1.1.0

func (adapter Adapter) AddPoliciesCtx(ctx context.Context, sec string, ptype string, rules [][]string) error

AddPoliciesCtx adds policy rules to the storage. This is part of the Auto-Save feature.

func (Adapter) AddPolicy

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

AddPolicy add one policy rule to the storage.

func (Adapter) AddPolicyCtx added in v1.1.0

func (adapter Adapter) AddPolicyCtx(ctx context.Context, sec string, ptype string, rule []string) error

AddPolicyCtx adds a policy rule to the storage with context. This is part of the Auto-Save feature.

func (Adapter) IsFiltered

func (adapter Adapter) IsFiltered() bool

IsFiltered returns true if the loaded policy rules has been filtered.

func (Adapter) IsFilteredCtx added in v1.1.0

func (adapter Adapter) IsFilteredCtx(ctx context.Context) bool

IsFilteredCtx returns true if the loaded policy has been filtered.

func (*Adapter) LoadFilteredPolicy

func (adapter *Adapter) LoadFilteredPolicy(model model.Model, filterPtr interface{}) error

LoadFilteredPolicy load policy rules that match the Filter. filterPtr must be a pointer.

func (*Adapter) LoadFilteredPolicyCtx added in v1.1.0

func (adapter *Adapter) LoadFilteredPolicyCtx(ctx context.Context, model model.Model, filterPtr interface{}) error

LoadFilteredPolicyCtx loads only policy rules that match the filter.

func (*Adapter) LoadPolicy

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

LoadPolicy load all policy rules from the storage.

func (*Adapter) LoadPolicyCtx added in v1.1.0

func (adapter *Adapter) LoadPolicyCtx(ctx context.Context, model model.Model) error

LoadPolicyCtx loads all policy rules from the storage with context.

func (Adapter) RemoveFilteredPolicy

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

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

func (Adapter) RemoveFilteredPolicyCtx added in v1.1.0

func (adapter Adapter) RemoveFilteredPolicyCtx(ctx context.Context, sec string, ptype string, fieldIndex int, fieldValues ...string) error

RemoveFilteredPolicyCtx removes policy rules that match the filter from the storage with context. This is part of the Auto-Save feature.

func (Adapter) RemovePolicies

func (adapter Adapter) RemovePolicies(sec string, ptype string, rules [][]string) (err error)

RemovePolicies removes policy rules from the storage. This is part of the Auto-Save feature.

func (Adapter) RemovePoliciesCtx added in v1.1.0

func (adapter Adapter) RemovePoliciesCtx(ctx context.Context, sec string, ptype string, rules [][]string) error

RemovePoliciesCtx removes policy rules from the storage. This is part of the Auto-Save feature.

func (Adapter) RemovePolicy

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

RemovePolicy remove policy rules from the storage.

func (Adapter) RemovePolicyCtx added in v1.1.0

func (adapter Adapter) RemovePolicyCtx(ctx context.Context, sec string, ptype string, rule []string) error

RemovePolicyCtx removes a policy rule from the storage with context. This is part of the Auto-Save feature.

func (Adapter) SavePolicy

func (adapter Adapter) SavePolicy(model model.Model) error

SavePolicy save policy rules to the storage.

func (Adapter) SavePolicyCtx added in v1.1.0

func (adapter Adapter) SavePolicyCtx(ctx context.Context, model model.Model) error

SavePolicyCtx saves all policy rules to the storage with context.

func (Adapter) UpdateFilteredPolicies

func (adapter Adapter) UpdateFilteredPolicies(sec, ptype string, newRules [][]string, fieldIndex int, fieldValues ...string) ([][]string, error)

UpdateFilteredPolicies deletes old rules and adds new rules.

func (Adapter) UpdateFilteredPoliciesCtx added in v1.1.0

func (adapter Adapter) UpdateFilteredPoliciesCtx(ctx context.Context, sec string, ptype string, newRules [][]string, fieldIndex int, fieldValues ...string) (oldPolicies [][]string, err error)

UpdateFilteredPoliciesCtx deletes old rules and adds new rules.

func (Adapter) UpdatePolicies

func (adapter Adapter) UpdatePolicies(sec, ptype string, oldRules, newRules [][]string) (err error)

UpdatePolicies updates policy rules to storage.

func (Adapter) UpdatePoliciesCtx added in v1.1.0

func (adapter Adapter) UpdatePoliciesCtx(ctx context.Context, sec string, ptype string, oldRules, newRules [][]string) error

UpdatePoliciesCtx updates some policy rules to storage, like db, redis.

func (Adapter) UpdatePolicy

func (adapter Adapter) UpdatePolicy(sec, ptype string, oldRule, newRule []string) error

UpdatePolicy update a policy rule from storage. This is part of the Auto-Save feature.

func (Adapter) UpdatePolicyCtx added in v1.1.0

func (adapter Adapter) UpdatePolicyCtx(ctx context.Context, sec string, ptype string, oldRule, newRule []string) error

UpdatePolicyCtx updates a policy rule from storage. This is part of the Auto-Save feature.

type Filter

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

Filter define the filtering rules for a FilteredAdapter's policy. Empty values are ignored, but all others must match the Filter.

Jump to

Keyboard shortcuts

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