Home

Awesome

Build Status Gem Version Coverage Status

What is the purpose of this Gem?

First of all this is not an other authorization gem! The whole concept is about "Feature rolling" and "Feature flipping" which we can resume to "Feature enabling"!

The idea is to dynamically enable (and disable) application feature depending on the user status (ex: beta, standard) and the feature stage (ex: beta, production). Then the purpose is to make this process easy, dynamic and as much automatic as possible!

So how does it works?

The Gem let you describe in a simple DSL a set of possible status for user, instance (group of users) and feature. Then you describe the relation (mapping) between each feature status and a set of user and instance status.

Finally you describe all your application feature and their respective release status.

At the end you get access to the (not so) magic access_to?(:feature_name) method that does all the hard work to tell you true or false!

Terminology and Concept

In order to use this gem you need to understand those keywords:

The gem will give you the access status (true or false) of a feature by checking in this exact order:

  1. Is the feature available for the current locale (I18n.locale)
  2. If it does, check if the user has access to the feature (based on the relations)
  3. If not, check if the instance has access to the feature (also based on the relations)
  ## helper method
  def access_to?(feature, *actions)
    return false if !locale_access_to?(feature, *actions)
    return true if DSL.roles.user.present? && user_access_to?(feature, *actions)
    return true if DSL.roles.instance.present? && instance_access_to?(feature, *actions)
    return false
  end

Setup

1) Install the Gem

Add to your Gemfile:

  gem 'helioth', '~> 0.1.0'

2) DSL Configuration

  roles do
    user :beta, :standard
    instance :beta, :standard, :critical
    feature :disabled, :beta, :pre_release, :production
  end
  relations do
    feature :disabled

    feature :beta do
      instance :beta
      user :beta
    end

    feature :pre_release do
      instance :beta, :standard
      user :beta
    end

    feature :production do
      instance :beta, :standard, :critical
      user :beta, :standard
    end
  end
  features do
    feature :no_name do
      status :disabled
    end

    feature :tutoring do
      status :pre_release

      actions :search, :send do ## this is optional
        status :beta
        locales :fr ## this is optional
      end

      actions :index do
        status :production
      end
    end

    feature :social_learning do
      status :beta
      locales :fr, :en ## this is optional
    end
  end

As you can see :actions and :locales are optional. Those give you more flexibility over the rollout process. You can find this complete DSL example inside the /examples directory.

3) Model configuration

  class MyUser < ActiveRecord::Base
    ...
    has_helioth_role :user
    ...
  end

  class MyInstance < ActiveRecord::Base
    ...
    has_helioth_role :instance
    ...
  end
  class MyUser < ActiveRecord::Base
    ...
    has_helioth_role :user, column: "my_role_column"
    ...
  end

How to use in your code

  access_to?(:feature_name)
  #OR
  access_to?(:feature_name, :action_name)
  if access_to?(:tutoring, :search)
    link_to tutoring_path()
  end
  ## Declare if an entire controller is accessible based on a specific feature
  load_and_authorize_for :feature_name

  ## Declare if a controller method (:index) is accessible based on an action (:index) related to a feature (:tutoring)
  load_and_authorize_for :tutoring, :action=>:index, :only => :index

  ## Declare if a controller method (:search) is accessible based on a multiple actions (:index, :search) related to a feature (:tutoring)
  load_and_authorize_for :tutoring, :actions=>[:search, :index], :only => :search

  ## All before_filter keywords are available:
  :only, :except, :if, :unless
  ## Access DSL object
  Helioth::DSL.method_name

  ## For ex. retrieved all features
  Helioth::DSL.features

  ## For ex. retrieve info about a specific feature
  Helioth::DSL.feature(:feature_name)

  ## Etc.. for more information check the lib/helioth/dsl.rb file

Requirements

  current_user
  #AND
  current_instance

Those helpers must return an instance of User and Instance class where your defined the has_helioth_role class method.

FAQ

Testing the Gem

bundle exec rake
  cd test/dummy && bundle install && rails s

License

Copyright © 2014 Guillaume Montard and Vodeclic SAS released under the MIT license