Role Requirement plugin

Plugin details

There are many role-based security plug-ins out there. Often times, they are very complicated and much more than you need.

RoleRequirement focuses on a simple approach to role-based authentication. You don't have to learn a new language in order to specify roles; instead, RoleRequirement leverages the power of Ruby to strike a marvelous balance between simplicity and flexibility.

Websitehttp://code.google.com/p/rolerequirement/ Repositoryhttp://rolerequirement.googlecode.com/svn/tags/role_requirement/ Author Tim Harper Tags UserManagement LicenseMIT

Documentation

Install the plugin:
ruby script/plugin install http://rolerequirement.googlecode.com/svn/tags/role_requirement/

1. Install acts_as_authenticated and role_requirement

You must generate your User model from acts_as_authenticated and include AuthenticatedSystem in your ApplicationController, as usual.

2. Add has_role? to your User model

class User < ActiveRecord::Base
  # has_role? simply needs to return true or false whether a user has a role or not.  
  # It may be a good idea to have "admin" roles return true always
  # You can use either a habtm relationship for roles, or a simple enum field.
  # This example uses a habtm
  def has_role?(role)
    @roles ||= self.roles.collect(&:name)
    return true if @roles.include?("admin")
    (@roles.include?(role.to_s) )
  end



3. Require a roles in your controllers

class Admin::Users < ApplicationController
  require_role "admin"
end



Unlimited flexibility without the mess!

class Admin::Listings < ApplicationController
  require_role "contractor"
  require_role "admin", :only => :destroy # don't allow contractors to destroy

  # leverage ruby to prevent contractors from updating listings they don't have access to.
  require_role "admin", :only => :update, :unless => "current_user.authorized_for_listing?(params[:id]) "

end

Further Documentation

There is currently no advanced documentation for this plugin.

New documentation

Edit plugin | (0 older versions) | Last edited by: Guest, 8 months ago