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

ember-declarative-authorization

v0.0.3

Published

declarative authorization ember component

Downloads

4

Readme

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.