GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: Demonstrates a reference implementation for handling role management
Homepage: http://www.pluginaweek.org
Clone URL: git://github.com/pluginaweek/has_roles.git
name age message
file .gitignore Fri Jul 04 15:49:52 -0700 2008 Ignore test/app_root/script [obrie]
file CHANGELOG.rdoc Sun Oct 26 15:16:33 -0700 2008 Tag 0.1.2 release [obrie]
file LICENSE Wed Jun 25 20:23:39 -0700 2008 Rename MIT-LICENSE to LICENSE [obrie]
file README.rdoc Sun Oct 26 15:15:06 -0700 2008 Fix permissions for new roles always defaulting... [obrie]
file Rakefile Sun Oct 26 15:16:33 -0700 2008 Tag 0.1.2 release [obrie]
directory app/ Sun Oct 26 15:15:06 -0700 2008 Fix permissions for new roles always defaulting... [obrie]
directory db/ Sun May 04 15:59:45 -0700 2008 Add use of named_scope instead of class finders [obrie]
file init.rb Mon May 14 23:40:04 -0700 2007 Initial release [obrie]
directory lib/ Sun Oct 26 15:15:06 -0700 2008 Fix permissions for new roles always defaulting... [obrie]
directory test/ Thu Dec 04 17:56:05 -0800 2008 Assert invalid attributes using invalid? rather... [obrie]
README.rdoc
= has_roles

+has_roles+ demonstrates a reference implementation for handling role management.

== Resources

API

* http://api.pluginaweek.org/has_roles

Bugs

* http://pluginaweek.lighthouseapp.com/projects/13277-has_roles

Development

* http://github.com/pluginaweek/has_roles

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
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 either define your own Permission model or create
an initializer like so:

config/initializers/role_management:
  Permission.create :id => 2, :controller => 'admin/stats'
  Permission.create :id => 3, :controller => 'comments', :action => 'create'
  ...

=== Adding/Updating roles

To add or update roles, you can either define your own Role model or create an
initializer like so:

config/initializers/roles.rb:
  # Edit the default role called "administrator"
  Role[:administrator].permissions << 'admin/stats/'
  
  # Create a new role called "developer"
  Role.create :id => 2, :name => 'developer', :permissions => %w(
    comments/create
    admin/stats/
  )

=== Default Permissions/Roles

By default, the following permissions are define:
* application - Permission for accessing any part of the application

By default, the following roles are defined:
* admin - An administrator with the default permissions

You can remove the default permissions/roles by adding the following to your
initializer:

config/initializers/roles.rb:
  Role.destroy_all
  Permission.destroy_all
  ...

=== 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:
* plugin_test_helper[http://github.com/pluginaweek/plugin_test_helper]

To run against a specific version of Rails:

  rake test RAILS_FRAMEWORK_ROOT=/path/to/rails

== Dependencies

* Rails 2.1 or later
* acts_as_enumeration[http://github.com/pluginaweek/acts_as_enumeration]
* plugins_plus[http://github.com/pluginaweek/plugins_plugins] (optional if app files are copied to your project tree)