Casbin Objection Adapter
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({
});
const adapter = await ObjectionAdapter.newAdapter(knex, {});
const enforcer = await newEnforcer("basic_model.conf", adapter);
enforcer.enableAutoSave(true);
await enforcer.addPolicies([
["alice", "data1", "read"],
["bob", "data2", "write"],
]);
await enforcer.enforce("alice", "data1", "read");
await enforcer.enforce("bob", "data1", "read");
Advanced usage
The following options are available:
Option | Default value | Description |
---|
createTable | true | Whether or not to create the table when initialized. |
modelClass | CasbinRule | The model to use when querying policies. You can override this if you would like to control the table name |
logger | noop | An 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:%