public
Description: Demonstrates a reference implementation for handling role management
Homepage: http://www.pluginaweek.org
Clone URL: git://github.com/pluginaweek/has_roles.git
obrie (author)
Tue Jun 09 20:33:30 -0700 2009
commit  f11c42e22d8fb21c0a66f8e2bbba2c07e61dc945
tree    b26b54e365efbec9246acb25cf28fb65ce459b28
parent  15109522112393f66418c799f19a80607c577615
name age message
file .gitignore Fri Jul 04 15:49:52 -0700 2008 Ignore test/app_root/script [obrie]
file CHANGELOG.rdoc Thu Apr 30 19:39:18 -0700 2009 Tag 0.3.0 release [obrie]
file LICENSE Tue Apr 28 20:45:33 -0700 2009 Replace acts_as_enumeration with enumerate_by A... [obrie]
file README.rdoc Fri May 01 17:58:42 -0700 2009 Add required enumerate_by version to README [obrie]
file Rakefile Tue Jun 09 20:33:30 -0700 2009 Add gemspec [obrie]
directory app/ Tue Apr 28 20:45:33 -0700 2009 Replace acts_as_enumeration with enumerate_by A... [obrie]
directory db/ Tue Apr 28 20:45:33 -0700 2009 Replace acts_as_enumeration with enumerate_by A... [obrie]
file has_roles.gemspec Tue Jun 09 20:33:30 -0700 2009 Add gemspec [obrie]
file init.rb Mon May 14 23:40:04 -0700 2007 Initial release [obrie]
directory lib/ Tue Apr 28 20:45:33 -0700 2009 Replace acts_as_enumeration with enumerate_by A... [obrie]
directory test/ Tue Apr 28 20:45:33 -0700 2009 Replace acts_as_enumeration with enumerate_by A... [obrie]
README.rdoc

has_roles

has_roles demonstrates a reference implementation for handling role management.

Resources

API

Bugs

Development

Source

  • git://github.com/pluginaweek/has_roles.git

Description

One of the easiest and most straightforward techniques for adding role management and authorization to specific parts of your application is restricting usage on a controller/action-basis. Each role defined in your system is mapped to one or more permissions. Each permission is a combination of a controller and action.

Usage

Note that this is a reference implementation and, most likely, should be modified for your own usage.

Adding permissions

To add permissions, you can create an initializer like so:

config/initializers/permissions.rb:

  Permission.bootstrap(
    {:id => 1, :controller => 'application'},
    {:id => 2, :controller => 'admin/stats'},
    {:id => 3, :controller => 'comments', :action => 'create'},
    ...
  )

Adding / Updating roles

To add / update roles, you can create an initializer like so:

config/initializers/roles.rb:

  Role.bootstrap(
    {:id => 1, :name => 'admin'},
    {:id => 2, :name => 'developer'},
    ...
  )

  RolePermission.bootstrap(
    {:role => 'admin', :permission => 'application/'},
    {:role => 'admin', :permission => 'admin/states/'},
    {:role => 'developer', :permission => 'comments/create'},
    {:role => 'developer', :permission => 'admin/stats/'},
    ...
  )

Checking a user’s authorization

Below is an example of checking a user’s authorization for a url before displaying information:

app/views/layouts/application.rhtml:

  <% if authorized_for?(:controller => 'admin/users') %>
  <p>Read to start administering your website?</p>
  <% end %>

Testing

Before you can run any tests, the following gem must be installed:

To run against a specific version of Rails:

  rake test RAILS_FRAMEWORK_ROOT=/path/to/rails

Dependencies