https://github.com/tecfu/roleguard
A role based acess control library that includes some extra features I couldn't find elsewhere.
https://github.com/tecfu/roleguard
rbac
Last synced: over 1 year ago
JSON representation
A role based acess control library that includes some extra features I couldn't find elsewhere.
- Host: GitHub
- URL: https://github.com/tecfu/roleguard
- Owner: tecfu
- License: gpl-3.0
- Created: 2019-09-20T23:38:52.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2022-12-30T18:42:08.000Z (over 3 years ago)
- Last Synced: 2025-02-06T14:32:08.635Z (over 1 year ago)
- Topics: rbac
- Language: JavaScript
- Size: 58.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Roleguard
A role based access control library with some unconventional options:
- Add a conditional validation constraint to an access rule (facilitates more specificity than packages that limit access to roles and their granted actions).
- Report which rule granted access.
## Install
```js
npm install https://github.com/tecfu/roleguard.git
```
### Example
```js
const RoleGuard = require('@tecfu/roleguard')
// or as ES6 module use
// import RoleGuard from '@tecfu/roleguard'
const AccessRules = {
user: {
can: [
{
resource: 'chat',
actions: ['update'],
condition: (ctx) => {
// rule will only positive match if function returns boolean `true`
return ctx.request.body.id === ctx.state.jwt.sub.id
}
}
]
}
}
const abilities = RoleGuard(AccessRules)
const ctx = {
request: {
body: {
id: 2
}
},
state: {
jwt: {
sub: {
id: 2
}
}
}
}
const test = abilities.can('update', 'chat', ['user'], ctx)
console.log(test)
//{
// can: true,
// message: 'user can update chat subject to rule condition',
// rule: {
// resource: 'chat',
// actions: [ 'update' ],
// condition: '(ctx) => {\n' +
// ' return ctx.request.body.id === ctx.state.jwt.sub.id\n' +
// ' }'
// },
// roles: [ 'user' ],
// requestedAction: 'update',
// requestedResource: 'chat'
//}
```
## Test
```
npm run-script test
```
## License
GPL 3.0