GithubHelp home page GithubHelp logo

shawnb457 / role Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fantactuka/role

0.0 2.0 0.0 212 KB

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

License: MIT License

role's Introduction

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

role's People

Contributors

fantactuka avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.