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

cantina-permissions

v4.1.2

Published

Permissions system for Cantina apps

Downloads

9

Readme

cantina-permissions

Utilizes node-relations for permissions stored in redis.

Table of Contents

Usage

Include the cantina-permissions plugin in your cantina application and define your relations contexts. You'll then have the API for granting, revoking, and querying for your application's permissions.

var app = require('cantina').createApp();

app.boot(function (err) {
  // Handle err.
  if (err) throw err;

  // Load cantina-permissions.
  app.require('cantina-permissions');

  // Start the app.
  app.start();
});
module.exports = function (app) {
  var permissionDefinitions = {
    event: {
      author: ['read', 'edit', 'delete'],
      viewer: ['read'],
      collaborator: ['read', 'edit']
    },
    site: {
      admin: ['administrate']
    }
  };

  Object.keys(permissionDefinitions).forEach(function (ctx) {
    app.permissions.define(ctx, permissionDefinitions[ctx]);
  });
};

Example

module.exports = function (app) {
  var controller = app.controller();

  controller.get('/event/:id', function (req, res, next) {
    app.permissions.event.can('read', {user: req.user, object: req.params.id},
    function (err, hasAccess) {
      if (err) return next(err);
      if (hasAccess) {
        app.collections.event.load(req.params.id, function (err, event) {
          if (err) return next(err);
          res.vars.event = event;
          res.render('event', res.vars);
        });
      }
      else {
        res.renderStatus(403);
      }
    });
  });

  return controller;
};

API Reference

app.permissions

Namespace for permission-related APIs.

app.permissions.define(context, roles)

Proxies relations to create a context, which contains a list of roles which map to actions.

  • context: A name for the context
  • roles: A hash of roles and verbs
app.permissions.define('event', {
  author: ['read', 'edit', 'delete'],
  attendee: ['read'],
  admin: ['administrate']
});

app.permissions[context].grant(role, args, cb)

Grants a relations role to the user.

  • role: The role to grant
  • args: may be a user model, a user id, or a hash of:
    • user: The user model or id to grant the role to
    • object: (optional) The object model or id that the role relates to
  • cb: The callback

Runs the stact-hook permissions:grant(options, done) so other plugins may react to the event.

app.permissions.event.grant('author', {
  user: userModel,
  object: eventModel
}, function (err) {
  if (err) return app.emit('error', err);
);

app.permissions.event.grant('admin', userModel, function (err) {
  if (err) return app.emit('error', err);
);

app.permissions[context].revoke(role, args, cb)

Revokes a relations role from the user.

  • role: The role to grant
  • args: may be a user model, a user id, or a hash of:
    • user: The user model or id to grant the role to
    • object: (optional) The object model or id that the role relates to
  • cb: The callback

Runs the stact-hook permissions:revoke(options, done) so other plugins may react to the event.

app.permissions.event.revoke('author', {
  user: userModel,
  object: eventModel
}, function (err) {
  if (err) return app.emit('error', err);
);

app.permissions.event.revoke('admin', userModel, function (err) {
  if (err) return app.emit('error', err);
);

app.permissions[context].hasRole(role, args, cb)

Checks whether a user has a role.

  • role: The role to check for
  • args: may be a user model, a user id, or a hash of:
    • user: The user model or id to grant the role to
    • object: (optional) The object model or id that the role relates to
  • cb: The callback
app.permissions.event.hasRole('author', {
  user: userModel,
  object: eventModel
}, function (err, hasRole) {
  if (err) return app.emit('error', err);
  if (hasRole) {
    // do something
  }
);

app.permissions.event.hasRole('admin', userModel, function (err, hasRole) {
  if (err) return app.emit('error', err);
  if (hasRole) {
    // do something
  }
);

app.permissions[context].can(verb, args, cb)

Checks whether a user can perform an action.

  • verb: The action to check for
  • args: may be a user model, a user id, or a hash of:
    • user: The user model or id to grant the role to
    • object: (optional) The object model or id that the role relates to
  • cb: The callback
app.permissions.event.can('edit', {
  user: userModel,
  object: eventModel
}, function (err, hasAccess) {
  if (err) return app.emit('error', err);
  if (hasAccess) {
    // do something
  }
);

app.permissions.event.can('administrate', userModel, function (err, hasAccess) {
  if (err) return app.emit('error', err);
  if (hasAccess) {
    // do something
  }
);

app.permissions[context].any(verbs, args, cb)

Checks whether a user can perform at least one of an array of actions

  • verbs: an array of actions to check for
  • args: may be a user model, a user id, or a hash of:
    • user: The user model or id to grant the role to
    • object: (optional) The object model or id that the role relates to
  • cb: The callback
app.permissions.event.any(['delete', 'edit'], {
    user: 'erin',
    object: 'doc1'
  }, function (err, hasAnyAccess) {
  if (err) return app.emit('error', err);
  if (hasAnyAccess) {
    // do something
  }
);

app.permissions[context].all(verbs, args, cb)

Checks whether a user can perform all of an array of actions.

  • verbs: an array of actions to check for
  • args: may be a user model, a user id, or a hash of:
    • user: The user model or id to grant the role to
    • object: (optional) The object model or id that the role relates to
  • cb: The callback
app.permissions.event.all(['delete', 'edit' ], {
    user: 'erin',
    object: 'doc1'
  }, function (err, hasAllAccess) {
  if (err) return app.emit('error', err);
  if (hasAllAccess) {
    // do something
  }
);

app.permissions[context].whoCan(verb, object, cb)

Returns an array of user ids who can perform an action on an object.

  • verb: The verb to check for
  • object: The object model or id that the query relates to
  • cb: The callback
app.permissions.event.whoCan('read', eventModel, function (err, userIds) {
  if (err) return app.emit('error', err);

  // do something with userIds
);

app.permissions[context].whoIs(role, object, cb)

Returns an array of user ids who have a role over an object.

  • role: The role to check for
  • object: The object model or id that the query relates to
  • cb: The callback
app.permissions.event.whoIs('author', eventModel, function (err, userIds) {
  if (err) return app.emit('error', err);

  // do something with userIds
);

app.permissions[context].whatCan(user, verb, cb)

Returns an array of object ids on which a user can perform an action.

  • user: The user model or id to check access for
  • verb: The verb to check for
  • cb: The callback
app.permissions.event.whatCan(userModel, 'edit', function (err, objectIds) {
  if (err) return app.emit('error', err);

  // do something with objectIds
);

app.permissions[context].whatIs(user, role, cb)

Returns an array of object ids on which a user has a role

  • user: The user model or id to check access for
  • role: The role to check for
  • cb: The callback
app.permissions.event.whatIs(userModel, 'author', function (err, objectIds) {
  if (err) return app.emit('error', err);

  // do something with objectIds
);

app.permissions[context].whatActions(user, object, cb)

Returns an array of verbs a user can perform on an object.

  • user: The user model or id to check access for
  • object: The object model or id that the query relates to
  • cb: The callback
app.permissions.event.whatActions(userModel, eventModel, function (err, verbs) {
  if (err) return app.emit('error', err);

  // do something with verbs
);

Developed by TerraEclipse

Terra Eclipse, Inc. is a nationally recognized political technology and strategy firm located in Santa Cruz, CA and Washington, D.C.