[gem]: https://github.com/buszu/auth_strategist [codeclimate]: https://codeclimate.com/github/buszu/auth_strategist/ AuthStrategist ====== [![Code Climate](https://codeclimate.com/github/buszu/auth_strategist/badges/gpa.svg)](https://codeclimate.com/github/buszu/auth_strategist) AuthStrategist is a simple gem to define and use authorization strategies. ## Installation ```ruby gem 'auth_strategist' ``` And then execute: $ bundle Generate initializer if using Rails: $ rails g auth_strategist:install Or install it yourself as: $ gem install auth_strategist ## Configuration ```ruby AuthStrategist.configure do |c| # Set default strategy components (its attributes). # Optional and empty by default. c.default_strategy_components = [:application, :ref] # Set strategies directory # Required if strategies files are not already loaded c.strategies_path = 'lib/auth_strategist/strategies' # Register your strategies # Required for each strategy you have defined c.strategies do |s| # E.g. PasswordStrategy will be available under :password key s.password = PasswordStrategy end end ``` ## Usage ### Defining a Strategy * Define a strategy ```ruby class PasswordStrategy include AuthStrategist::StrategyInterface define_components :user, :password def authorize! raise StandardError unless user.password == password end end ``` * Register it ```ruby AuthStrategist.strategies do |s| s.password = PasswordStrategy end ``` or ```ruby AuthStrategist.configure do |c| c.strategies do |s| s.password = PasswordStrategy end end ``` ### Using strategies * Using strategy by calling authorization Service Object ```ruby AuthStrategist::Authorize.call strategy: :password, user: current_user, password: password ``` * Using strategy with authorize! method ```ruby class SomethingsController < ApplicationController include AuthStrategist::Authorization def show authorize! strategy: :password, user: current_user, password: password end end ``` ## Contributing 1. Fork it ( https://github.com/[my-github-username]/auth_strategist/fork ) 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Add some feature'`) 4. Push to the branch (`git push origin my-new-feature`) 5. Create a new Pull Request