CanCan provides a simple API for handling authorization of actions.
Permissions are defined for each class using a simple can
function.
$ npm install cancan --save
Read the interactive user guide on Bucket
const cancan = require('cancan');
const can = cancan.can;
// example classes
class AdminUser {}
class User {}
class Product {}
// define permissions
cancan.configure(User, function (user) {
// this user can view
// all instances of Product
this.can('view', Product);
});
cancan.configure(AdminUser, function (user) {
// this user can:
// 1. view all products
// 2. create a new product
this.can('view', Product);
this.can('create', Product);
});
// check access
let product = new Product();
let adminUser = new AdminUser();
let user = new User();
can(adminUser, 'view', product); // true
can(adminUser, 'create', product); // true
can(user, 'view', product); // true
can(user, 'create', product); // false
$ npm test
MIT © Vadym Demedes