npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dignity

v0.0.1

Published

A simple layer for creating and checking user roles/permissions

Downloads

7

Readme

Dignity

Dignity is a node js module that creates a layer based on user to check his/her roles and permissions

Build Status

ORMs Covered (for now)

Quick use:

var dignity = require('dignity');

// sequelize
User.find(userId).then(function (result) {
  var layer = new dignity.SequelizeLayer(result);
  layer.analyzeDignity().then(function () {
    layer.is('ironman'); // check if user has role ironman
    layer.hasRole('destroyer'); // layer.is === layer.hasRole

    layer.can('play-dota') // check if user can play dota
    layer.hasPermission('play-dota') // layer.can === layer.hasPermission
  });
});


// bookshelf
(new User({
    id: 1
}))
.fetch()
.then(function (result) {
  var layer = new dignity.BookshelfLayer(result);
  layer.analyzeDignity().then(function () {
    layer.is('ironman'); // check if user has role ironman
    layer.hasRole('destroyer'); // layer.is === layer.hasRole

    layer.can('play-dota') // check if user can play dota
    layer.hasPermission('play-dota') // layer.can === layer.hasPermission
  });
});

To use dignity properly, you have to follow these conventions:

  • Many to Many relation between User and Role, the name of the relation in User must be roles
  • Many to Many relation between Role and Permission, the name of the relation in Role must be permissions

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 in knexfile.js
  • create the migration dignity create-migration or dignity 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 run analyzeDignity, so u can use it like this layer.setUser(user).then(function () { /* do something */ }).
  • getPermissions() returns an array of permissions.
  • getRoles() returns an array of roles.
  • is(String|Array) check role, example layer.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, example layer.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.