https://github.com/leafoftree/simple-mongoose-rbac
Role based access control for mongoose, a simple user authority manager solution.
https://github.com/leafoftree/simple-mongoose-rbac
Last synced: 7 months ago
JSON representation
Role based access control for mongoose, a simple user authority manager solution.
- Host: GitHub
- URL: https://github.com/leafoftree/simple-mongoose-rbac
- Owner: leafOfTree
- Created: 2016-12-06T02:48:38.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2016-12-08T03:21:26.000Z (about 9 years ago)
- Last Synced: 2025-03-15T23:35:36.940Z (12 months ago)
- Language: JavaScript
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# simple-mongoose-rbac
Role based access control for mongoose, a simple user authority manager solution.
[](https://travis-ci.org/leafOfTree/simple-mongoose-rbac)
## require
> "mongoose": "~3.8.8"
## install
```bash
npm install simple-mongoose-rbac --save-dev
```
## Usage & Test
```js
'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.');
}
```