• This repository has been archived on 24/Jun/2019
  • Stars
    star
    252
  • Rank 161,312 (Top 4 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 16 years ago
  • Updated over 10 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple role based security for restful_authentication I am no longer involved in this project. If you are interested in becoming the new maintainer and making it your own, please contact me. I will no longer be responding to bug reports or questions. Thanks, Tim

No longer supported. The last version (1.6) is compatible only with Rails 2.3.

Role Requirement

Version 1.6 – April 28, 2009

role_requirement first assumes that the controller is accessible to everyone.

Each require_role line serves to add one or more required roles before executing actions within the controller. IE:

  require_role "admin" # Require that the current_user must have role admin
                       # before executing any action within this controller.

“Limiting” Methods

role_requirement accepts several methods as an options hash of key/value pairs. These methods work like clauses that limit the require_role line.

  • :for – Evaluate this role requirement before executing ONLY the following actions in this controller…
    * :for_all_except – Evaluate this role requirement before executing ALL actions in this controller EXCEPT the following…

(note: :only and :except also work, similarly to before_filter)

These methods can be thought of as telling role_requirement when to evaluate current_user’s roles.


  # Anytime an action is called, require the role admin in order to execute, 
  # EXCEPT when index is called.  So, index passes through this require_role
  # line allowing anyone to access it, ALL other actions are evaluated against
  # the specified role(s).
  require_role "admin", :for_all_except => :index

  # Means, "Evaluate current_user's role(s) ONLY when index is called... and
  # require admin in order to execute index." So, every action except index
  # passes through this require_role line, ONLY index is evaluated.
  require_role "admin", :for => :index

How require_role Lines Work In Combination

Each line serves as an additional requirement to any other lines in the controller… like AND… not OR.


  require_role "admin"
  require_role "executive"
  # This means that role_requirement will look for BOTH roles for EVERY action
  # in the controller, i. e. current_user must have both admin and executive
  # roles to do anything in this controller.


  require_role "admin"
  require_role "executive", :except => [:index]
  # This means that admin can access only index and executive can access
  # everything in the controller except index.
  # The controller requires admin for every action AND looks for executive in
  # order to execute every action except index. (Probably not a very useful
  # configuration!)

  require_role "admin"
  require_role "executive", :only => [:create, :update, :edit, :destroy]
  # This means that admin can access the entire controller and executive is
  # required only for index.
  # The controller always requires admin and requires executive only when
  # index is called. Useful for allowing only executive to call certain, more
  # restricted, actions, for example. 

Passing Arrays

role_requirement accepts arrays of roles and/or actions. When more than one role name or action name is passed, they work like “OR” in the requirement phrase.


  # current_user can do anything in this controller if they have either role,
  # admin OR executive.
  require_role ["admin", "executive"]

  # The role admin is required to perform all actions in this controller
  # EXCEPT list or show. So, anyone can list and show. admin can do anything.
  require_role "admin", :except => [:list, :show]

  # The role admin is required to perform ONLY delete OR edit. Other actions
  # are not evaluated. So, only admin can delete or edit. Others can do
  # anything other than delete or edit.
  require_role "admin", :only => [:delete, :edit]

Admin

role_requirement’s generator will automatically add a method to your user model, User#has_role?

This method, by default, always returns true for the role named “admin.” This makes it easy to create a role that can access all actions, just name it “admin.”


  # Allow finance to access every action but delete.  Admin can access
  # everything, including delete.
  require_role :finance
  require_role :admin, :only => :delete
  # Explanation: Finance can access the entire controller (and so can admin
  # because admin always returns true for every role) AND admin is required to
  # execute only the delete action.

If you don’t want this behavior, comment out the line that causes admin to always return true:


	def has_role?(role_in_question)
		@_list ||= self.roles.collect(&:name)
		# return true if @_list.include?("admin") #This is the culprit.
		(@_list.include?(role_in_question.to_s) )
	end

You can also change the name of the “all access” role here. You can use any name that makes sense to you.

Syntax

If you use strings as keys for the options hash, it will throw an error. ie:


  # throws an error
  require_role "admin", "only" => "index"

  # works just fine
  require_role "admin", :only => "index"

RoleRequirement does not care if the values are symbols or strings, regarding action names.


  require_role "admin", :only => "index"
  require_role "admin", :only => :index

Roles are passed to User#has_role? exactly as specified. By default, RoleRequirement generates this method to not care. If you customize User#has_role? in such a way that it does care, then you’ll have problems.


  # ultimately calls User#has_role?("admin")
  require_role "admin", :only => :index

  # ultimately calls User#has_role?(:admin)
  require_role :admin, :only => :index

More Repositories

1

calendar_date_select

A previously popular but no longer maintained JavaScript DatePicker for RubyOnRails (and others)
JavaScript
409
star
2

git-tmbundle

James Conroy-Finn is the new maintainer. Please visit his repository ( http://github.com/jcf/git-tmbundle ) for the new official home of the git-TextMate bundle. Thank you!
Ruby
284
star
3

git_osx_installer

Installer for OS X
Makefile
254
star
4

rubyamp

TextMate bundle - Amplified Mate Productivity
Ruby
116
star
5

bundle-fu

Ruby on Rails plugin - CSS/JS asset bundling in 10 seconds or less!
Ruby
78
star
6

reactive-viz

Reactive streams (akka-streams) visualizer
Scala
75
star
7

caprese

Various actions to help you communicate your Pomodoro status, and concentrate on your task at hand.
Ruby
56
star
8

textile.vim

Textile for VIM
Vim Script
52
star
9

homebridge-vivint

Integrates Vivint security system with Apple Home
JavaScript
34
star
10

calendar_date_select_demo

Demo app for calendar date select
Ruby
30
star
11

gitty

Unobtrusively extend git repositories with hooks. Additionally, share hooks between developers.
Ruby
26
star
12

git-helpers

various git helper scripts to augment your git experience
Ruby
22
star
13

acked-stream

Message Acknowledgement for Akka Streams
Scala
18
star
14

railswhere

An SQL condition builder tool
Ruby
16
star
15

background_process

The ultimate library for UNIX process control
Ruby
14
star
16

campfire-el

Campfire client for Emacs (why not?)
Emacs Lisp
14
star
17

homebridge-opensprinkler

OpenSprinkler plugin for Homebridge
JavaScript
13
star
18

vimpulse-surround.el

Tim Pope's surround.vim, in emacs
Emacs Lisp
11
star
19

uber-builder

A handy and flexible table / css form builder
Ruby
10
star
20

screenmanager

Screen Manager starter kit
10
star
21

mcli

jq powered mesos cli tool
Shell
9
star
22

active_resource_config

Environment-specific configuration for active-record, stored in config/active_resource.yml. Adds some more config parameters.
Ruby
8
star
23

tightvnc-dns

a stripped down version of tightvnc, with the purpose of acting solely as a key board transporter (allowing you to use Dragon NaturallySpeaking to dictate into applications running on Mac OS 10, or Linux)
C
6
star
24

gitosis.vim

gitosis-admin.conf syntax highlighting for vim
Vim Script
5
star
25

indicator

Easily add progress indicators for rails
JavaScript
4
star
26

bccalc.vim

Use bc from vim
Vim Script
4
star
27

wordnet.vim

WortNet plugin for VIM
Vim Script
4
star
28

samza-hello-scala

Scala implementation of samza-hello-samza
Scala
4
star
29

ng-admin-demo

Demo of using Akka HTTP, Slick, and ng-admin
Scala
4
star
30

vines.tmbundle

A textmate bundle to swing around your code
3
star
31

sicp-study

SICP Study
Scheme
3
star
32

cassandra-talks-scala

Simple integration library which enriches a Cassandra session so you can easily return Scala Futures and Akka Streams
Scala
3
star
33

web-sickle

Ruby
2
star
34

myfamily_blog_postinator

Postinating the country side, postinating the people
Ruby
2
star
35

puppet-demo

puppet demo
2
star
36

sbt-jnaerator

Run JNAerator from SBT
Scala
2
star
37

ward-directory

A ward-directory generator that integrates with LDS church membership program MLS
Ruby
2
star
38

vimpulse

my fork of git://git.assembla.com/vimpulse.git
Emacs Lisp
2
star
39

advancedGit

AdvanceGit
1
star
40

git-svn-installer

Git SVN Installer for Yosemite
Makefile
1
star
41

l-unit

git://repo.or.cz/lunit.git
Lua
1
star
42

akka-stream-examples

Scala
1
star
43

as-actorref-repro

Scala
1
star