npm

ember-declarative-authorization

0.0.3 • Public • Published

Ember Declarative Authorization

Build Status

declarative authorization component for Ember.js.

Installation

$ npm install ember-declarative-authorization

or ...

$ bower install ember-declarative-authorization

or just grab your preferred distribution from dist/.

Then include the script(s) into your application:

npm+browserify

require('ember-declarative-authorization')

amd

Register ember-declarative-authorization as a package, then:

define(['ember-declarative-authorization'], ...)

named-amd

You ought to know what you're doing if this is the case.

globals

<script src="bower_components/ember-declarative-authorization/dist/globals/main.js"></script>

Usage (e.g how does it work?)

To enable Ember.DeclartiveAuth in an Application, simply add a custom initializer:

//user defined rules object. Where it is located doesn't matter, but
//it must be registered on 'rules:main' like shown below.
import RulesMain from 'appkit/rules/main';

Ember.Application.initializer({
  name: 'authorization',
  initialize: function (container, application) {
    
    container.register('rules:main', RulesMain);

    Ember.DeclarativeAuthorization.setup(container,application);

  }
});

This initializer parses and validates the rules you as a user define, and constructs a quick way to lookup rules at runtime.

You must define a module with your own rules in 'rules/main.js' which you can import in app.js using 'import RulesMain from 'appkit/rules/main':

export default Ember.Object.extend({
    //Can a specific user edit edit a specific post?
	'posts.edit': [
	{
	    actor:  "user",
	    object: "post"
		can: function(actor, object, target){
		    //do an arbitrary check here
              	    return true;
		}
	}
});

Rule Format

A rule is specified in the format where **only the activity-verb and defining the 'can' function is required:

activity-verb: {
	actor:   object-type-1,
	object:  object-type-2,
	target:  object-type-3,

	can: function(actor, object, target){
          //arbitrary javascript function that 
          //returns true/false
          return true;
    }
}

where object-type-# is the expected type of object provided in that argument.

The activity verb is the name of an activity like for instance 'posts.edit'.

The actor is the entity that is seeking authorization.

The object may be the entity performing the activity, or the entity on which the activity was performed. e.g John(actor) shared a video(act_object)

The target is the object that the verb is enacted on. e.g. Geraldine(actor) posted a photo(object) to her album(target).

Rule Activity Verbs with Multiple Associated Rules

An activity verb can be used for multiple rules by putting all rules for that verb into an array.

activity-verb: [
	{
		actor:   object-type-1,
		object:  object-type-2,
		target:  object-type-3,

		can: function(actor, object, target){
	       //arbitrary javascript function that 
	       //returns true/false
               return true;
	   
	    }
	},
    {
		actor:   object-type-1,
		can: function(actor, object, target){
	       //arbitrary javascript function that 
	       //returns true/false
               return false;
	    }
	}

]

Invoking a Rule in a Template

To selectively show content in a template invoke the #can-do component, e.g:

{{#can-do activity="edit" actor=user object=post }}
MARK
{{/can-do}}

To selectively hide content in a template invoke the #cannot-do component, e.g:

{{#cannot-do activity="edit" actor=user object=post }}
MARK
{{/cannot-do}}

Declaratively Protecting a Route

You can declaratively protect a controller route by extending the 'AuthorizeRouteMixin' mixin, e.g:

var PostsRoute = Ember.Route.extend(Ember.DeclarativeAuthorization.AuthorizeRouteMixin, {
  //Optional definition of actor
  actor: function(){
  	return this.store.find('user','1');
  },
  model: function() {
    return this.store.find('post');
  }
});

export default PostsRoute;

The definition of the actor method on the route is optional, but if it does then it is expected to return a promise the resolves to the current actor.

The activity verb of the controller is then inferred to be the route name, like e.g 'posts.index', and the model on the controller is assumed to be the object. For example, for the route above the following rule must be defined:

"posts.index": {
	actor:   "user",
	object:  "post",

	can: function(actor, object, target){
          //arbitrary javascript function that 
          //returns true/false
          return true;
    }
}

Contributing

$ git clone <this repo>
$ npm install
$ npm test
# during dev
$ broccoli serve
# localhost:4200/globals/main.js instead of dist/globals/main.js
# new tab
$ karma start

Make a new branch, send a pull request, squashing commits into one change is preferred.

Readme

Keywords

none

Package Sidebar

Install

npm i ember-declarative-authorization

Weekly Downloads

1

Version

0.0.3

License

MIT

Last publish

Collaborators

  • asabjorn