ngRights service - Supermood/ngRights GitHub Wiki

ngRights service

The ngRights service allows you to interact directly with ngRights inside your JavaScript code, especially if you need some JS to execute only depending on the rights that the user has. It makes code clearer by replacing complex if statements by simple ngRights calls.

The API follows:

ngRights.ruleset : ruleset

This contains the ruleset. Even if you can, you should not edit this ruleset manually.

ngRights.setRules(rules : ruleset) : void

This is an alias to ngRightsProvider.setRules.

// example usage :
ngRights.setRules({ 
  pages: { 
    posts: {
      delete: function (subject, post) { return subject.id == post.ownerId; }
    }
  }
});
// if pages.posts.create was defined before, it's not anymore

ngRights.addRules(rules : ruleset) : void

addRules extends the current ruleset with the specified rules. You can use this to re-define existing rules, as new rules having an already used name will override the old ones.

rules is the ruleset that will be added to the ruleset.

// example usage :
ngRights.addRules({ 
  pages: { 
    posts: {
      create: true, // if pages.posts.create was already defined, it will be overriden
      delete: function (subject, post) { return subject.id == post.ownerId; }
    }
  }
});

ngRights.hasRights(right : string, object : object) : boolean

HasRight calls the specified right function on the given object and evaluates the result. It returns true if the right is allowed, false otherwise. If the computation fails, it will return the onErrorValue.

right is The name of the right to check. It can be a dot separated string or an Array of strings.

object is The object that will be fed to the rule, if found.

// example usage :
if (ngRights.hasRights('pages.posts.delete', myPost)) {
  // do something
}

ngRights.getRule(name : string | Array of strings) : function (throws Error)

This function uses the given name to fetch the rule in the ngRights ruleset. Rules must be defined prior to this function call. If the rule you are referring to exists, it will return that rule (a function). Otherwise, it will throw an Error.

name is an Array of strings or dot-separated string ('example.test.rule') that represents the rule name.

Please prefer using hasRights instead of getRule and then evaluating your rule manually.

// example usage :
ngRights.getRule('pages.posts.create');
ngRights.getRule(['pages', 'posts', 'create']);

ngRights.refresh() : void

Forces the computation of all ngRights directives. You should use it after redefining rules.

// example usage :
ngRights.refresh()