• Stars
    star
    326
  • Rank 128,668 (Top 3 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 14 years ago
  • Updated 11 months ago

Reviews

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

Repository Details

RoleModel is the framework agnostic, efficient and declarative way to do user roles.

RoleModelΒΆ ↑

RoleModel is the framework agnostic, efficient and declarative way to do (user) roles. Assigned roles will be efficiently stored as a bitmask directly into your model within a configurable attribute.

It works like this:

# given a User class with a roles_mask attribute
require 'rubygems'
require 'role_model'

class User
  attr_accessor :roles_mask   # just for demo purposes
  # in real life this would usually be a persistent attribute,
  # e.g. if your User model is persisted in a SQL-DB add an integer
  # column named roles_mask to your users table -- just remove/replace
  # above attr_accessor line with whatever is needed for your
  # persistence solution

  include RoleModel

  # if you want to use a different integer attribute to store the
  # roles in, set it with roles_attribute :my_roles_attribute,
  # :roles_mask is the default name
  roles_attribute :roles_mask

  # declare the valid roles -- do not change the order if you add more
  # roles later, always append them at the end!
  roles :admin, :manager, :author
end

#
# Test drive (check the RDoc or source for additional finesse)
#

>> u = User.new
=> #<User ...>

# role assignment
>> u.roles = [:admin]  # ['admin'] works as well
=> [:admin]

# adding roles (remove via delete or re-assign)
>> u.roles << :manager
=> [:admin, :manager]

# querying roles...

# get all valid roles that have been declared
>> User.valid_roles
=> [:admin, :manager, :author]

# ... retrieve all assigned roles
>> u.roles # also: u.role_symbols for DeclarativeAuthorization compatibility
=> [:admin, :manager]

# ... check for individual roles
>> u.has_role? :author  # has_role? is also aliased to is?
=> false

# ... check for individual roles with dynamic methods (set dynamic: false to disable)
>> u.is_author?
=> false

# ... check for multiple roles
>> u.has_any_role? :author, :manager  # has_any_role? is also aliased to is_any_of?
=> true

>> u.has_all_roles? :author, :manager  # has_all_roles? is also aliased to is?
=> false

# see the internal bitmask representation (3 = 0b0011)
>> u.roles_mask
=> 3

# see the role mask for certain role(s)
>> User.mask_for :admin, :author
=> 5

Once you have included RoleModel, your model is perfectly fit to be used together with a role-based authorization solution, such as github.com/ryanb/cancan or github.com/stffn/declarative_authorization .

InstallationΒΆ ↑

gem install role_model

ReasoningΒΆ ↑

Whenever I introduce a role-based authorization scheme into a project, the code gets coupled somehow to the available roles. So it usually does not make any sense to have a separate Role model stored within the database. This Role model will contain a predefined set of roles anyhow – changing that set will need to be reflected within the authorization code. Putting the available roles right into the model that actually uses them, makes things much easier and efficient.

Note on Patches/Pull RequestsΒΆ ↑

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with Rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

CreditsΒΆ ↑

RoleModel is an implementation of the Role Based Authorization scheme proposed by Ryan Bates (wiki.github.com/ryanb/cancan/role-based-authorization).

Copyright Β© 2010 Martin Rehfeld. See LICENSE for details.

More Repositories

1

ext_scaffold

Please note: This project has not been adapted to Rails >2, sorry! Generate full-featured scaffolds using the ExtJS framework.
Ruby
83
star
2

docker-haskell-platform

Docker file to setup the Haskell Plattform 2013.2.0.0
Shell
10
star
3

erlang-kata

My erlang exercises
JavaScript
8
star
4

heroku-buildpack-f77

A custom FORTRAN-77 buildpack for the Heroku cedar platform
Shell
6
star
5

telefon

Initiate SIP calls via web interface and the Sipgate API
Ruby
6
star
6

elli_basicauth

Basic Authentication middleware for the Elli webserver
Erlang
5
star
7

wai-example

Example webservice project for WAI (Haskell Web Application Interface)
Haskell
4
star
8

htmlunit_testrunner

A headless "browser" that runs unittest.js / jsunittets.js based Javascript unit tests and reports back results
Java
4
star
9

elixir-metrics-statsd

Elixir wrapper around the Erlang metrics app from Hackney (benoitc/erlang-metrics)
Elixir
3
star
10

glacier-proxy

Web service to proxy streaming uploads to Amazon Glacier via a simple REST API (simple enough to use it with curl on the command line)
JavaScript
3
star
11

elli-example

Example webservice app build with Elli
Shell
3
star
12

elli_throttle

Request throttling middleware for the elli webserver
Erlang
3
star
13

gufm1-webservice

Four centuries of geomagnetic secular variation from historical records
Fortran
3
star
14

w14g-app

What Was I Thinking // Twitter analysis -- CoffeeScript app
CoffeeScript
2
star
15

dotfiles

dotfiles I might need on multiple machines
Erlang
2
star
16

rolib

Remote Operations Libary. Vintage software from 1996.
C
2
star
17

formative

Simple yet useful form builder for Rails 2 and 3
Ruby
2
star
18

cruisecontrol.rb-plugins

Custom plugins for cruisecontrol.rb
Ruby
2
star
19

hot_code_reload

Proof of concept for hot code reloading without breaking model/gen_server state or configuration
Erlang
2
star
20

weathertime

Automatically exported from code.google.com/p/weathertime
Perl
1
star
21

focus-fengshui

www.focus-fengshui.de site
CSS
1
star
22

ruby-kata

a little Ruby environment for test driving experiments/katas
Ruby
1
star
23

ua-parser-erlang

User Agent Parser for Erlang
Erlang
1
star
24

fb_signed_request

Erlang implementation for parsing and generating Facebook Signed Requests
Erlang
1
star
25

inside.glnetworks.de

Ma blog -- I guess, I should add some recent content ;-)
JavaScript
1
star
26

fun-club-wordcount-erlang

Programming exercise/presentation for the Fun-Club meetup on 2013-02-28
Erlang
1
star
27

bloomurl

Erlang: Tiny bloom filters with URL encoded serialization
Erlang
1
star
28

haproxy2rpm

Ruby
1
star
29

w14g-build

What Was I Thinking // Twitter analysis -- Middleman-based build system
JavaScript
1
star