Skip to content
Pure MongoDB adapter for Casbin
TypeScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci omitted check Oct 24, 2019
.vscode Initial commit Oct 23, 2019
casbin-files WiP Writing test Oct 23, 2019
src fixed lint errors Oct 24, 2019
.editorconfig
.gitignore Initial commit Oct 23, 2019
.npmignore Initial commit Oct 23, 2019
.prettierignore Initial commit Oct 23, 2019
.travis.yml Initial commit Oct 23, 2019
LICENSE Initial commit Oct 23, 2019
README.md updated readme Oct 24, 2019
package.json
tsconfig.json Initial commit Oct 23, 2019
tsconfig.module.json Initial commit Oct 23, 2019
tslint.json deleted unused files Oct 24, 2019
yarn.lock updated test Oct 24, 2019

README.md

casbin-mongodb-adapter

MongoDB policy storage, implemented as an adapter for node-casbin.

NPM Version NPM Version Build Code Quality Build Build Build

Getting Started

Install the package as dependency in your project:

npm install --save casbin-mongodb-adapter

Require it in a place, where you are instantiating an enforcer (read more about enforcer here):

import {newEnforcer, Enforcer} from 'casbin';
import { MongoAdapter } from 'casbin-mongodb-adapter';
import * as path from 'path';

async function myFunction() {
  const model = path.resolve(__dirname, 'casbin-files/rbac_model.conf');

  const adapter = await MongoAdapter.newAdapter({
    uri: 'mongodb://localhost:27017',
    collectionName: 'casbin',
    databaseName: 'node-casbin-official'
  });

  const e = await newEnforcer(model, adapter);

  await e.loadPolicy();

  // Check the permission.
  e.enforce('alice', 'data1', 'read');

  // Modify the policy.
  // await e.addPolicy(...);
  // await e.removePolicy(...);

  // Save the policy back to DB.
  await e.savePolicy();
}

That is all what required for integrating the adapter into casbin. Casbin itself calls adapter methods to persist updates you made through it.

Configuration

You can pass mongodb-specific options when instantiating the adapter:

import { MongoAdapter } from 'casbin-mongodb-adapter';

const adapter = await MongoAdapter.newAdapter({
  uri: 'mongodb://localhost:27017',
  collectionName: 'casbin',
  databaseName: 'node-casbin-official',
  option: {
    ...mongoOptions
  }
});

Additional information regard to options you can pass in you can find in mongodb documentation

License

Apache-2.0

You can’t perform that action at this time.