Installation & Configuration - Supermood/ngRights GitHub Wiki

Installation

bower install ngRights

Configuration

To configure the ngRights library, you may use the ngRightsProvider provider, which API is defined as follows:

setRules(ruleset)

Sets the rules used by the library to assess if an element should be displayed. The ruleset parameter is an array of rules that follow the [rule structure](Rule structure)

ngRightsProvider.setRules(ruleset)

setRulesUrl(url)

Tell the provider to load rules from a specific URL. This will check the cache first before attepting to download the file. The file must be a ruleset (array of rules) in JSON format.

ngRightsProvider.setRulesUrl(url)

setSubjectGenerator(callback)

Sets the function that will be called to generate a subject every time a rights check is done.

ngRightsProvider.setSubjectGenerator(callback)

As an example, if you have a User provider that handles the logged in User, you can do

ngRightsProvider.setSubjectGenerator(userProvider.generateSubject);

yourModule.provider('user', function () {
  var user = {};
  // the generateSubect
  this.generateSubject = function () { 
    return {
      id: user.id, 
      commentLikes: user.commentLikes,
    }; 
  };
  this.$get = ['userResource', '$rootScope', function(userResource, $rootScope) {
    return {
      login: function(username, password) {
         userResource.login({username: username, password: password}).$promise.then(function(response) {
           angular.extend(user, response); // write user properties
           $rootScope.$broadcast('ngRights-update'); // tell ngRights to update the interface
         }, function(error) {});
      }
    };
  }]
});

The setSubjectGenerator function is also available from the ngRights service, for the case in which you do not have access to a userProvider at configuration time.