rethinkadapter

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2020 License: MIT Imports: 4 Imported by: 0

README

RethinkDB Adapter

RethinkDB Adapter is the Rethink DB adapter for Casbin. With this library, Casbin can load policy from RethinkDB or save policy to it.

Installation

go get github.com/adityapandey9/rethinkdb-adapter

Simple Example

package main

import (
    	"os"
    	r "gopkg.in/gorethink/gorethink.v3"
	"github.com/casbin/casbin"
	"github.com/adityapandey9/rethinkdb-adapter"
)

func getConnect() r.QueryExecutor {
	url := os.Getenv("RETHINKDB_URL") //Get the Rethinkdb url from system env

	if url == "" {
		url = "localhost:28015"
	}

	session, _ := r.Connect(r.ConnectOpts{
		Address: url,
	})

	return session
}

func main() {
	// Initialize a RethinkDB get session, add it to adapter and use it in a Casbin enforcer:
	// The adapter will use the database named "casbin".
	// If it doesn't exist, the adapter will create it automatically.
  	session := getConnect()
	a := rethinkadapter.NewAdapter(session) // Your RethinkDB Session. 
	
	e := casbin.NewEnforcer("examples/casbinmodel.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()
}

Getting Help

License

This project is under MIT License. See the LICENSE file for the full license text.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAdapter

func NewAdapter(Sessionvar r.QueryExecutor) persist.Adapter

NewAdapter is the constructor for Adapter.

Types

type Adapter

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

Adapter represents the RethinkDB Adapter for policy storage.

func (*Adapter) AddPolicy

func (a *Adapter) AddPolicy(sec string, PTYPE string, policys []string) error

AddPolicy for adding a new policy to rethinkdb

func (*Adapter) GetDatabaseName

func (a *Adapter) GetDatabaseName() string

GetDatabaseName returns the name of the database that the Adapter will use

func (*Adapter) GetTableName

func (a *Adapter) GetTableName() string

GetTableName returns the name of the table that the Adapter will use

func (*Adapter) LoadPolicy

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

LoadPolicy loads policy from database.

func (*Adapter) RemoveFilteredPolicy

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

RemoveFilteredPolicy for removing filtered policy

func (*Adapter) RemovePolicy

func (a *Adapter) RemovePolicy(sec string, PTYPE string, policys []string) error

RemovePolicy for removing a policy rule from rethinkdb

func (*Adapter) SavePolicy

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

SavePolicy saves policy to database.

func (*Adapter) SetDatabaseName

func (a *Adapter) SetDatabaseName(s string)

SetDatabaseName sets the database that the Adapter will use

func (*Adapter) SetTableName

func (a *Adapter) SetTableName(s string)

SetTableName sets the tablet that the Adapter will use

Jump to

Keyboard shortcuts

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