Dignity
Dignity is a node js module that creates a layer based on user to check his/her roles and permissions
ORMs Covered (for now)
Quick use:
var dignity = ; // sequelizeUser; // bookshelf id: 1;
To use dignity
properly, you have to follow these conventions:
- Many to Many relation between
User
andRole
, the name of the relation in User must beroles
- Many to Many relation between
Role
andPermission
, the name of the relation in Role must bepermissions
Too lazy to make migration file?
dignity
has a command line tool to make migration file (based on knex)
Steps:
- install knex
npm install knex -g
- init the knexfile
knex init
and input your db config inknexfile.js
- create the migration
dignity create-migration
ordignity create-migration > ./migrations/dignity-migration.js
- now you can run the migration
knex migrate:latest
:D
Layer API
getUser()
get the user.setUser(user)
set user,it will automaticaly runanalyzeDignity
, so u can use it like thislayer.setUser(user).then(function () { /* do something */ })
.getPermissions()
returns an array of permissions.getRoles()
returns an array of roles.is(String|Array)
check role, examplelayer.is('ironman')
layer.is(['ironman', 'admin'])
. For array convention, it returns true satisfy if user has one of the given roles.can(String|Array)
check permission, examplelayer.can('playing')
layer.is(['playing', 'edit post'])
. For array. convention, it returns true satisfy if user has one of the given permission
Testing
You must have sqlite3
installed in your system, npm test
NOTE
For now dignity
leaves the creation of roles and permissions by yourself since it's responsibility is only to check the dignity
of the given user.