Coder Social home page Coder Social logo

hal's Introduction

HAL. Minimalist authorization framework for Ruby

HAL is a class-based authorization framework for Ruby. It lets you define authorization rules on classes, which will then be applied to all instances of said classes. For example:

class User < Struct.new(:name); end
dave = User.new("Dave")

HAL.rules_for User do
  def open_the_pod_bay_doors?
    subject.name != "Dave"
  end
end

HAL.can(dave).open_the_pod_bay_doors? #=> false

When defining rules you can access a subject method, which will reference whatever object you pass to can.

You also have a more powerful version of can, called can!, which takes a block and will raise HAL::ICantLetYouDoThat if the block evaluates to false:

HAL.can!(dave) {|can| can.open_the_pod_bay_doors? } #=> raises!

Finally, you can include HAL into your classes to get can and can! as helper methods.

class UsersController < ApplicationController
  include HAL

  before_filter :check_permissions, :only => [:edit, :update, :destroy]

  def check_permissions
    unless can(current_user).edit_user?(current_user)
      redirect_to users_path, :alert => "I can't let you do that, Dave"
    end
  end
end

Installing

As a RubyGem

gem install hal

For rails 2.3, add to your config/environment.rb:

config.gem "hal", :lib => "hal/rails"

For rails 3, add to your Gemfile:

gem "hal", :require => "hal/rails"

hal's People

Contributors

foca avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.