GitXplorerGitXplorer
f

role

public
18 stars
3 forks
1 issues

Commits

List of commits on branch master.
Unverified
d62d913aaed7d135d4d07a340c62671c52d0dae6

Update README.md

ffantactuka committed 11 years ago
Unverified
156161018d78e1a16742fe09d7a7ad4800e019ce

Update README.md

ffantactuka committed 11 years ago
Unverified
cdf8ff2fd7e724507c5425d8ce7872819e9a4e14

Proper version in bower.json and src

ffantactuka committed 11 years ago
Unverified
a2983d26cac6f6e4552bccae199ed6d18be1a19e

Update to v0.0.2

ffantactuka committed 11 years ago
Unverified
15d1d0fb02e12d9e8bbec5dd618331220cd8a500

Do not polute global namespace, when using AMD

ffantactuka committed 11 years ago
Unverified
d94258a1056ba6b738eb446f80b7746f96bd8ea1

Merge branch 'master' of github.com:fantactuka/role

ffantactuka committed 11 years ago

README

The README file for this repository.

Role.js Build Status

Role allows you to manage user's access depending on his current roles and abilities map

Installation

Using Bower bower install role or just copy role.js

Usage

// Defining current user role ("guest" by default)
Role.current = 'admin';

// or
Role.current = ['user', 'moderator'];

// or
var CurrentUser = require('my-current-user-instance');
Role.current = function() {
  return CurrentUser.roles;
}

// Defining roles with entity->action mapping
Role.define('user', {
  books: {
    read: true,
    update: function(book) {
      return book && book.authorId === CurrentUser.id
    }
  }
});

// Inheriting existing models
Role.define('admin', 'user', {
  books: {
    update: true
  }
});

// After that you're able to use "can" helper to check if current user's role is allowed to
// perform actions on passed entities.
// E.g. somewhere in code:

if (Role.can('read', 'books')) {
  ...
}

// or

var book = books.get(1);

if (Role.can('update', 'books', book)) {
  ...
}

// or somewhere in Backbone.Router or whatever router that has 'before' filter

... 
before: {
  'books/new': function() {
    if (!Role.can('create', 'books')) {
      this.navigate('/home');
      return false;
    }
  }
}
...

Using roles in templates

Handlebars

Handlebars.registerHelper('can', function() {
  var abilityArgs = _.initial(arguments),
    able = Role.can.apply(null, abilityArgs),
    options = _.last(arguments);

  return able ? options.fn(this) : options.inverse(this);
});

after that you can have following in templates:

{{#can 'create' 'books'}}
  <a href="#/books/new">Add book</a>
{{else}}  
  <a href="#/access/request">Request access to add new books</a>
{{/can}}

Running tests

You can use karma runner via

npm install && grunt test