cosmosadapter

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2021 License: Apache-2.0 Imports: 8 Imported by: 1

README

CosmosDB Adapter

CosmosDB Adapter is the cosmosDB adapter for Casbin. With this library, Casbin can load policy from CosmosDB or save policy to it.

Installation

go get github.com/spacycoder/cosmos-adapter

Note

When creating your collection for storing casbin rules you should use "/pType" as your partitionKey

Simple Example

package main

import (
	"github.com/casbin/casbin"
	cosmosadapter "github.com/spacycoder/cosmos-casbin-adapter"
)

func main() {
	// Initialize a CosmosDB adapter and use it in a Casbin enforcer:
	// The first argument is the cosmos connection string.
	// The second argument takes options as its input.
	// if not option is given the default database name is "casbin" and the default collection name is "casbin_rule"
	// The adapter will try to create the database and collection if it does not find them.
	a := cosmosadapter.NewAdapter("connstring")
	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()
}

With options

package main

import (
	"github.com/casbin/casbin"
	cosmosadapter "github.com/spacycoder/cosmos-casbin-adapter"
)

func main() {
	// Initialize a CosmosDB adapter and use it in a Casbin enforcer:
	// The adapter will try to create the database and collection if it does not find them.
	a := cosmosadapter.NewAdapter("connstring", cosmosadapter.Database("mycasbindb"), cosmosadapter.Collection("mycasbincollection"))
	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()
}

Filtered Policies

import "github.com/spacycoder/cosmosdb-go-sdk/cosmos"

// This adapter also implements the FilteredAdapter interface. This allows for
// efficent, scalable enforcement of very large policies:

filter := cosmos.Q{Query: "SELECT * FROM root WHERE root.v0 = @v0", Parameters: []cosmos.P{{Name: "@v0", Value: "bob"}}}
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.

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

func NewAdapter

func NewAdapter(connectionString string, options ...Option) persist.Adapter

NewAdapter is the constructor for Adapter. if no options are given the database name is "casbin" and the collection is named casbin_rule if the database or collection is not found it is automatically created. the database can be changed by using the Database(db string) option. the collection can be changed by using the Collection(coll string) option. see README for example

func NewFilteredAdapter

func NewFilteredAdapter(url string, options ...Option) persist.FilteredAdapter

NewFilteredAdapter is the constructor for FilteredAdapter. Casbin will not automatically call LoadPolicy() for a filtered adapter.

Types

type CasbinRule

type CasbinRule struct {
	ID    string `json:"id"`
	PType string `json:"pType"`
	V0    string `json:"v0"`
	V1    string `json:"v1"`
	V2    string `json:"v2"`
	V3    string `json:"v3"`
	V4    string `json:"v4"`
	V5    string `json:"v5"`
}

CasbinRule represents a rule in Casbin.

type Option

type Option func(*adapter)

func Collection

func Collection(coll string) Option

func CosmosClient added in v0.2.1

func CosmosClient(client *cosmos.Client) Option

func Database

func Database(db string) Option

Jump to

Keyboard shortcuts

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