AuthStrategist

Gem Version

AuthStrategist is a simple gem to define and use authorization strategies.

Installation

gem 'auth_strategist'

And then execute:

$ bundle

Generate initializer if using Rails:

$ rails g auth_strategist:install

Configuration

AuthStrategist.configure do |c|
  # Set strategies directory and load them
  # Required if strategies files are not already loaded
  c.strategies_path = 'lib/auth_strategist/strategies'
  c.load_strategies!
end

Usage

Defining a Strategy

  • Define a strategy ```ruby class OwnershipAuthStrategy include AuthStrategist::StrategyInterface

define_components :user, :item

def authorized? item.user_id != user.id end end

* Register it
```ruby
  AuthStrategist.strategies do |s|
    s.register :ownership, OwnershipAuthStrategy
  end

Using strategies

  • Using strategy with authorize! method ```ruby class SomethingsController < ApplicationController include AuthStrategist::Authorization

def show authorize! strategy: :ownership, user: current_user, item: book 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