Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

casbin-objection-adapter

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

casbin-objection-adapter

![tests](https://github.com/lfarrel6/casbin-objection-adapter/workflows/tests/badge.svg)

  • 0.3.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Casbin Objection Adapter

tests

  • Installation
  • Basic usage
  • Advanced usage

Installation

npm install casbin-objection-adapter --save
yarn add casbin-objection-adapter
pnpm add casbin-objection-adapter

Basic usage

See the Casbin adapters documentation for more information.

import Knex from "knex";
import { newEnforcer } from "casbin";
import { ObjectionAdapter } from "casbin-objection-adapter";

const knex = Knex({
  /* regular knex options */
});

// All configuration is optional
const adapter = await ObjectionAdapter.newAdapter(knex, {});

// Create the enforcer with the given model
const enforcer = await newEnforcer("basic_model.conf", adapter);

// Supports auto-save
// See: https://casbin.org/docs/en/adapters#autosave
enforcer.enableAutoSave(true);

// No need to save explicitly since auto-save is enabled
await enforcer.addPolicies([
  ["alice", "data1", "read"],
  ["bob", "data2", "write"],
]);

await enforcer.enforce("alice", "data1", "read"); // true
await enforcer.enforce("bob", "data1", "read"); // false

Advanced usage

The following options are available:

OptionDefault valueDescription
createTabletrueWhether or not to create the table when initialized.
modelClassCasbinRuleThe model to use when querying policies. You can override this if you would like to control the table name
loggernoopAn optional logger in case additional visiblity is needed into the adapter. The inteface should match console
Filtered policy loading

This adapter supports filtered policy loading as of v0.3.1.

Policies are filtered using the loadFilteredPolicy function on the enforcer. Note that loading a filtered policy clears the in memory policy data. This is a feature of Casbin and not this adapter.

Filter examples taken from casbin-pg-adapter

The filters take an object with keys refering to the ptype of the filter, and values containing an array of filter values.

Any empty string, undefined, or null value is ignored in the filter.

Plain strings (such as those used in the simple filter example below) are tested for simple equality. Strings prefixed with regex: or like: are tested using pattern matching.

Simple filter example:

await enforcer.loadFilteredPolicy({
  p: ["alice"],
  g: ["", "role:admin"],
});

Using the above filter, you will get:

  • all records with ptype of p, and subject of admin
  • and all records with ptype of g, and a second argument of admin

Complex filter example:

await enforcer.loadFilteredPolicy({
  p: ["regex:(role:.*)|(alice)"],
  g: ["", "like:role:%"],
});

Using the above filter you will get:

  • all records with ptype of p, and subjects that match the regex (role:.*)|(alice)
  • and all records with ptype of g, and a second argument that is like role:%

Keywords

FAQs

Package last updated on 24 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc