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

granted

v0.2.3

Published

An object agnostic authorization layer for javascript

Downloads

13

Readme

Granted

An object agnostic authorization layer for javascript. Features both a promise (recommended) and callback api for checking permissions.

account.grants(User, 'admin', function(user) {
  return (user.role === 'admin' || this.owner_id === user.id)
});

user.can('admin', account).then(function(user) {
  // .. the user is allowed to admin
}).catch(function(e) {
  // .. the user is not allowed to admin
});

Introduction:

Sometimes granting access between different objects gets hairy. You have edge cases and lots of ways that permissions can work and things end up being quite a mess.

Granted looks to simplify all of that, taking inspiration from projects like can can in ruby, it defines a few simple methods on an object, allowing us to reliably determine whether one object can perform an action on another.

Let's look at a simple example, say we have three objects, SuperUser, User and Document.

We want a document to be managed by any SuperUser, but only to a User if they have a "role" of "admin", or if they own the account. How would we do that?

First, we would define the granted permissions on the Account:

var doc = new Document({owner_id: 2, title: 'My Secret Account'});

// Allow any "authenticated" user to read the document.
doc.grants(User, 'read', function(user) {
  return user.isAuthenticated();
});

// Allow SuperUsers to do anything to the document.
doc.grants(SuperUser, ['read', 'write', 'destroy'], true);

// Allow Users to do anything to the document if they're
doc.grants(User, ['read', 'write', 'destroy'], function(user) {

  // Check that the user's ID matches with the owner_id of the document
  return user.id === this.get('owner_id')
});

// Allow anyone to read the metadata about an document, unless the
// object contains an is_robot flag.
doc.grants('readMeta', function(obj) {
  return obj.is_robot !== true;
});

Now, we can elsewhere call the can method on the object we're checking permissions on:

// Assumes `granted` has been mixed-in to each constructor
var su      = new SuperUser();
var authed  = new User({authenticated: true});
var user    = new User({id: 2});
var visitor = new Generic();
var robot   = new Generic({is_robot: true});

su.can('write', doc).then(function(su) {
  // ..
})

su.can('destroy', doc).then(function() {
  // ..
})

authed.can('read').then(function() {
  // true, because the user has been authed.
})

visitor.can('write', doc).catch(function(e) {
  // e instanceof granted.Errors.NotGranted
})

// or:

visitor.can('write', doc, function(e, visitor) {
  // e instanceof granted.Errors.NotGranted
});

API:

.grants([Constructor | predicate], name, predicate)

Grants the permission specified by name, (or permissions, if name is an array) if the predicate returns true, resolves with a successful promise, or

.can(name, [options]).then(...).catch(...)

.can(name, [options], callback)

.deny([Constructor | predicate], name, predicate)

.ungrant([Constructor | predicate], [name], [predicate])

.undeny([Constructor | predicate], [name], [predicate])