Socket
Socket
Sign inDemoInstall

simple-mongoose-rbac

Package Overview
Dependencies
1
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    simple-mongoose-rbac

Role based access control for mongoose


Version published
Maintainers
1
Created

Readme

Source

simple-mongoose-rbac

Role based access control for mongoose, simple to use

require

"mongoose": "~3.8.8"

install

npm install simple-mongoose-rbac --save-dev

Usage & Test

'use strict';

var mongoose = require('mongoose'),
    rbac = require('./index.js');

var UserSchema = new mongoose.Schema({ name: String });

// usage of rbac: just init with config

rbac.init({
    grants: {
        'user': {
            'comment': ['add'],
        },
        'admin': {
            'comment': ['delete'],
        },
    },
    callback: function (user, ope, res) {
        if (user.name === 'John' && ope === 'do') {
            return true;
        } else {
            return false;
        }
    },
    schema: UserSchema, 
});
// end

var User = mongoose.model('User', UserSchema);

var John = new User({ name: 'John', roles: ['user'] }),
    Oliver = new User({ name: 'Oliver', roles: ['admin'] }),
    Lee = new User({ name: 'Lee', roles: ['admin', 'user'] });

var expects = [
    John.can('add', 'comment')  === true,
    John.can('delete', 'comment')  === false,
    Oliver.can('add', 'comment')  === false,
    Oliver.can('delete', 'comment')  === true,
    Lee.can('add', 'comment')  === true,
    Lee.can('delete', 'comment')  === true,
    John.can('do', 'sths')  === true,
], passed = true;

expects.forEach(function (res, i) {
    if (!res) {
        console.log('The ' + i + ' th test result failed.');
        passed = false;
    }
});

if (passed) {
    console.log('Test passed.');
}

Keywords

FAQs

Last updated on 08 Dec 2016

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc