mongodbadapter

package module
Version: v2.0.0-...-0d37ea2 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2020 License: Apache-2.0 Imports: 10 Imported by: 0

README

MongoDB Adapter Build Status Coverage Status Godoc

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

Installation

go get -u github.com/titandc/mongodb-adapter/v2

Simple Example

package main

import (
	"github.com/casbin/casbin/v2"
	"github.com/titandc/mongodb-adapter/v2"
)

func main() {
	// Initialize a MongoDB adapter and use it in a Casbin enforcer:
	// The adapter currently requires X509 client authentication to the cluster.
	caFilePath := "/path/to/ca.crt"
	certificateKeyFilePath := "/path/to/client-cert.pem"
	replicaSet := "rs0"
	databaseName := "db1"
	mongodbServers := [...]string{mongodb-0:27017, mongodb-1:27017, mongodb-2:27017}

	a := mongodbadapter.NewAdapter(caFilePath, certificateKeyFilePath, replicaSet, databaseName, mongodbServers)
	e, err := casbin.NewEnforcer("examples/rbac_model.conf", a)
	if err != nil {
		panic(err)
	}

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

Filtered Policies

import "go.mongodb.org/mongo-driver/bson"

// This adapter also implements the FilteredAdapter interface. This allows for
// efficent, scalable enforcement of very large policies:
filter := &bson.M{"v0": "alice"}
e.LoadFilteredPolicy(filter)

// The loaded policy is now a subset of the policy in storage, containing only
// the policy lines that match the provided filter. This filter should be a
// valid MongoDB selector using BSON. A filtered policy cannot be saved.

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 represents the MongoDB adapter for policy storage.

func NewAdapter

func NewAdapter(caFilePath,
	certificateKeyFilePath,
	replicaSet,
	database string,
	servers []string) (*Adapter, error)

NewAdapter is the constructor for Adapter. If database name is not provided in the Mongo URL, 'casbin' will be used as database name.

func (*Adapter) AddPolicy

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

AddPolicy adds a policy rule to the storage.

func (*Adapter) ConnectToDB

func (a *Adapter) ConnectToDB(caFilePath, certificateKeyFilePath, replicaSet string) error

func (*Adapter) CreateDBIndex

func (a *Adapter) CreateDBIndex() error

func (*Adapter) IsFiltered

func (a *Adapter) IsFiltered() bool

IsFiltered returns true if the loaded policy has been filtered.

func (*Adapter) LoadFilteredPolicy

func (a *Adapter) LoadFilteredPolicy(model model.Model, filter interface{}) error

LoadFilteredPolicy loads matching policy lines from database. If not nil, the filter must be a valid MongoDB selector.

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 removes policy rules that match the filter from the storage.

func (*Adapter) RemovePolicy

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

RemovePolicy removes a policy rule from the storage.

func (*Adapter) SavePolicy

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

SavePolicy saves policy to database.

type CasbinRule

type CasbinRule struct {
	PType string
	V0    string
	V1    string
	V2    string
	V3    string
	V4    string
	V5    string
}

CasbinRule represents a rule in Casbin.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL