Skip to content
CosmosDB adapter for casbin
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

CosmosDB Adapter

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


go get

Simple Example

package main

import (
	cosmosadapter ""

func main() {
	// Initialize a CosmosDB adapter and use it in a Casbin enforcer:
	// The first argument is the cosmos connection string.
	// The second argument is the name of the database you wish to use. 
	// This database must already have been created. 
	// This will automatically create a collection under the supplied database with the name "casbin_rule".
	a := cosmosadapter.NewAdapter("connstring", "casbin") 
	// Or you can use an existing DB "abc" like this:
	// The adapter will use the table named "casbin_rule".
	// If it doesn't exist, the adapter will create it automatically.
	e := casbin.NewEnforcer("examples/rbac_model.conf", a)
	// Load the policy from DB.
	// Check the permission.
	e.Enforce("alice", "data1", "read")
	// Modify the policy.
	// e.AddPolicy(...)
	// e.RemovePolicy(...)
	// Save the policy back to DB.

Filtered Policies

import ""

// 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"}}}

// The loaded policy is now a subset of the policy in storage, containing only
// the policy lines that match the provided filter. 

Getting Help


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

You can’t perform that action at this time.