Get unlimited public & private packages + package-based permissions with npm Pro.Get started »

kasta

0.0.1 • Public • Published

Kasta

Given a JSON-based config

var config = {
  /**
   * list of roles 
   */
  roles :[
    "public",
    "user",
    "admin"],
 
  /**
   * access levels
   */
  accessLevels : {
    "public" : "*",
    "anonymous" : [ "public" ],
    "user" : [ "user", "admin" ],
    "admin" : [ "admin" ]
  }
}
var kasta = require ("kasta");
console.log (kasta(config));

Transform it into

{
  userRoles: {
    public: {
        bitMask: 1,
        title: 'public'
    },
    user: {
        bitMask: 2,
        title: 'user'
    },
    admin: {
        bitMask: 4,
        title: 'admin'
    }
  },
  accessLevels: {
    public: {
        bitMask: 7
    },
    anonymous: {
        bitMask: 1
    },
    user: {
        bitMask: 6
    },
    admin: {
        bitMask: 4
    }
  }
}

Then we can check if someone with specific roles can access a defined level

 
var role = kasta(config).userRoles["admin"];
var accessLevel = kasta(config).accessLevels["user"];
 
var allowed = role.bitMask & accessLevel.bitMask;
 
if (allowed) {
  // congratulate the user!
} else {
  // throw error
}

License

MIT

Install

npm i kasta

DownloadsWeekly Downloads

1

Version

0.0.1

License

MIT

Last publish

Collaborators

  • avatar