sqadapter

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2022 License: MIT Imports: 9 Imported by: 0

README

Casbin SegmentQ Adapter

Go Report Card Go Reference Build Reliability Rating Quality Gate Status Coverage

The SegmentQ DB adapter for Casbin. You can use this library to load and save policies to an SegmentQ DB.

Installation

go get github.com/segmentq/casbin-segmentq-adapter

Simple Example

package main

import (
	"context"
	adapter "github.com/segmentq/casbin-segmentq-adapter"
	"github.com/casbin/casbin/v2"
	"github.com/segmentq/db"
)

func main() {
	// Initialize some instance of SegmentQ DB
	segmentq, _ := db.NewDB(context.Background())
	
	// Initialize a SegmentQ adapter and use it in a Casbin enforcer:
	// The adapter will use a SegmentQ DB Index named "casbin_rule".
	// If it doesn't exist, the adapter will create it automatically.
	a, _ := adapter.NewAdapter(segmentq)

	// Create the enforcer
	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()
}

Documentation

Index

Constants

View Source
const DefaultIndexName = "casbin_rule"

Variables

This section is empty.

Functions

This section is empty.

Types

type Adapter

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

func NewAdapter

func NewAdapter(db *db.DB, opts ...Option) (*Adapter, error)

func (*Adapter) AddPolicy

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

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

func (*Adapter) LoadPolicy

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

LoadPolicy loads all policy rules from the storage.

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 storage. This is part of the Auto-Save feature.

func (*Adapter) RemovePolicy

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

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

func (*Adapter) SavePolicy

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

SavePolicy saves all policy rules to the storage.

type Option

type Option func(a *Adapter)

func WithIndexName

func WithIndexName(name string) Option

Jump to

Keyboard shortcuts

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