• Stars
    star
    110
  • Rank 316,770 (Top 7 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 14 years ago
  • Updated about 13 years ago

Reviews

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

Repository Details

Flexible access control mechanism!

ACLatraz

Extremaly fast, flexible and intuitive access control mechanism, powered by fast key value stores like Redis.

Installation

You can simple install ACLatraz via rubygems:

sudo gem install aclatraz

Configuration

Before you’ll start play with access controll in your apps you have to correctly configure datastore for permissions (at this moment ACLatraz is only supporting Redis database as storage). Redis datastore configuration looks very simple:

Aclatraz.init :redis, "redis://localhost:6379/0"

Remember that using Redis, you should specify database dedicated only for ACLatraz.

Suspects

Suspects are objects which we can assign specific permissions. There is only one condition which object have to meet to be suspect - it must have the #id method which returns an unique identifier after which we will be able to reference this object. To enable suspect behaviour you have to include the Aclatraz::Suspect module to specified class, eg:

class Account < ActiveRecord::Base
  include Aclatraz::Suspect
end

Now your suspect have few methods which will helps you manage it permissions.

Managing roles

ACLatraz distinguishes between three types of roles:

  • global: simple roles, which are most commonly assigned to many users. We can say that they are kind of groups. Global roles are eg. guest, admin, customer.

  • class-related: roles that affects management of a particular class. Example of this kind of role can be manager of Pages, admin of Products, etc.

  • object-related: roles that affects management of an object. For example author of specified page, owner of specified product, etc.

Now there is two ways for managing roles. You can use #roles or semanticaly look like #is and #is_not proxies.

Assigning

To add given role you can use #assign method from #roles proxy or use semantic shortcuts. Semantic shortcut have to ends with “!”, and can have optional suffixes: _on, _of, _at, _for, _in, _by. Take a look at the following examples to get everything to be clear:

@account.roles.assign(:admin) # or ...
@account.is.admin!

…will assign global admin role to the account.

@account.roles.assign(:responsible, Foo) # or...
@account.is.responsible_for!(Foo)

…will assign to the account responsible role related with Foo class.

@account.roles.assign(:author, Page.find(15)) # or...
@account.is.author_of!(Page.find(15))

…will assign to the account author role related with given Page object.

Checking

Using #roles proxy you can call <tt>#has?</t> method on it, eg:

@account.roles.has?(:admin)                # => true
@account.roles.has?(:responsible, Foo)     # => true
@account.roles.has?(:author, Page.find(15) # => true

With semantic shortcuts your method name have to ends with “?” and can contain any of suffixes listed above, eg:

@account.is.admin?                    # => true
@account.is.responsible_for?(Foo)     # => true
@account.is.author_of?(Page.find(15)) # => true

Few more examples with semantic negation:

@account.is_not.admin?                # => false
@account.is_not.responsible_for?(Foo) # => false

You can also check permissions using nice block-style syntax. The code inside the block will be executed only when object has given role. An quick example:

@account.is.admin? do 
  # only admins can se this...
end

Deleting

To unassign given role from object use #delete method from #roles, eg:

@account.roles.delete(:admin)
@account.roles.delete(:responsible, Foo)

Another way is to use semantic negation, where method name have to ends with “!” and can contain one of allowed suffixes, eg:

@account.is_not.admin!
@account.is_not.author_of!(Page.find(15))

Guards

To enable access control in your for your objects you have to include to it the Aclatraz::Guard module. This module provides methods for defining and checking permissions of an suspected object. Take a look for this basic example:

class Foo
  include Aclatraz::Guard

  suspects :account do 
    deny all # notice that it's a method, not symbol
    allow :admin
  end
end

The #suspects block is passing one argument - suspected object. When there is symbol given, like in example above, then will treat #account instance method result as suspected object. When you will use string, eg:

suspects "account" do # or suspects "@account" do ...

… it will treat @account instance variable as suspect. You can also specify suspected object directly, eg:

account = Account.find(1)
suspects account do # ...

Setting up permissions

As you probably noticed, there is two methods responsible for access control, namely #allow and #deny. As its argument you can pass simple name of role or permission statement, eg:

allow :admin
deny :guest
allow :responsible_for => Foo
allow :author_of => "@page"

Like you see, you can easy specify access for each kind of role. The object-related permissions behaviour is similar to #suspects method. When given related object is string then applies permissions for an instance variable, when symbol then applies it for instance method, otherwise directly for given object.

Actions

In your access control block you can specify separate action with its own permissions, eg:

suspects :account do 
  deny all
  allow :admin

  action :manage do 
    allow :responsible_for => Foo
  end

  action :delete do 
    allow :author_of => "@page"
  end
end

Obviously all actions inherits all permissions from main block.

Authorizing

The Aclatraz::Guards module provides #guard! method for checking permissions. When suspected object don’t have any of allowed permissions or have any of defnied then it will raise the Aclatraz::AccessDenied error. Here’s an comprehensive example:

class Foo
  suspects "@account" do 
    deny all
    allow :admin
    action :foo
      allow :foo
    end
    action :bar
      allow :bar
      deny :admin
    end
  end

  def initialize(account)
    @account = account
  end

  def simple
    guard!
    # only for accounts with :admin role...
  end

  def foo
    guard!(:foo)
    # only for accounts with :admin or :foo role...
  end

  def bar
    guard!(:bar)
    # only for accounts with :bar role...
  end

  def foobar
    guard!(:foo, :bar)
    # only for accounts with :foo or :bar, and mandatory without :admin role...
  end
end

There is also a very nice feature. You can define additional permissions directly in #guard! block, eg:

def foo
  guard! do
    allow :foo
    deny :bar
  end
  # ...
end

Inheritance

ACLatraz access control supports inheritance. It means that when you define your ACL in parent class it will be applied also for all child classes. Obviously in each child class you can freely modify permissions. Here’s an example:

class Foo 
  suspects :account do
    deny all
    allow :admin
  end
end

class Bar < Foo
  suspects do 
    # notice, that in child class don't have to again specify suspect...
    allow :foobar
  end
end  

class Spam < Foo
  suspects :egg do 
    # but of course you can specify different suspect than in parent class...
  end  
end

Aliases

If you prefer you can use aliases: #access_control instead of #suspects and #authorize! instead of #guard!.

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.

Copyright © 2010 Kriss ‘nu7hatch’ Kowalik. See LICENSE for details.

More Repositories

1

gmail

A Rubyesque interface to Gmail, with all the tools you'll need.
Ruby
664
star
2

gouuid

Go binding for libuuid
Go
417
star
3

desantapp

Desant is an open source landing page system
JavaScript
210
star
4

mustang

(proof-of-concept) Awesome V8 JavaScript engine embedded into Ruby's shiny body.
Ruby
106
star
5

rails3-ujs-example

Very simple and quick example of rails 3 app with jquery UJS
Ruby
46
star
6

konfigurator

Small and flexible configuration toolkit inspired i.a. by Sinatra settings.
Ruby
35
star
7

angular-foundation-on-yeoman

AngularJS powered by Zurb Foundation, running on Yeoman
JavaScript
31
star
8

webrocket

(under-development) Evented WebSocket server in Go
Go
31
star
9

gochanio

Go package which allows to bind channel with any IO reader or writer interface.
Go
26
star
10

golaroid

Simple assets server with ability to apply filters to the images
Go
25
star
11

egoistat

Website statistics from the most popular social networks
JavaScript
23
star
12

shaven

(proof-of-concept) Templating without mustaches!
Ruby
22
star
13

padrino-responders

This component is used to create slim controllers without unnecessery and repetitive code.
Ruby
16
star
14

gonetbench

Small TCP benchmarking tool in Go-lang
Go
15
star
15

gopqueue

Simple priority queue in Go
Go
14
star
16

redis-aid

First aid with Redis!
Ruby
13
star
17

areyoufuckingcoding.me

Ruby
10
star
18

padrino-form-errors

Form validation errors helper for Padrino
Ruby
7
star
19

persival

Programatic, persistent, pseudo key-value storage in Go
Go
7
star
20

authtools

Usefull stuff for unique tokens and secured password hashes generation.
Ruby
5
star
21

weedwarelicense.org

Weedware license website.
Ruby
5
star
22

wtf

WTF toolkit extracted from WebKit
C++
5
star
23

etoile

Etoile source code mirror. Etoile is a user environment designed from the ground up around the things people do with computers: create, collaborate, and learn.
Objective-C
5
star
24

rspec-jasmine

Jasmine runner and reporter for RSpec
JavaScript
4
star
25

objectpool

Magical thread pool implementation
Ruby
4
star
26

gotrail

Yet another super simple go logging...
Go
3
star
27

awesome_country_select

Provides a advanced helper to get an HTML select list of countries. The list of countries comes from the ISO 3166 standard. While it is a relatively neutral source of country names, it will still offend some users.
Ruby
3
star
28

gocolor

Simple colorizing at top of fmt package
Go
3
star
29

cgoecho

Example app using cgo interface to extend go with C code.
Go
3
star
30

no-logic-in-your-views-startechconf-2011

My talk on StarTechConf 2011 in Chile
Ruby
2
star
31

react

Redis based remote command executor.
Ruby
2
star
32

krug-redis-presentation

Presentation about Redis at Kraków Ruby User Group meeting
2
star
33

ryori

Projects generator and recipes compiler.
Ruby
2
star
34

exoteric

Website statistics from the most popular social networks (prototype in ruby)
Ruby
2
star
35

gostepper

Small helper for dealing with setup steps
Go
2
star
36

haml-magic-translations

Provides automaticaly translations in haml templates
Ruby
2
star
37

telegraph

Telegraph is DSL for quickly creating TCP servers in Ruby. It's running on EventMachine and is very similar to sinatra.
Ruby
2
star
38

nu7macs

Emacs configuration for nu7s!
Emacs Lisp
1
star
39

krug-padrino-presentation

Presentation about Padrino at Kraków Ruby User Group meeting
1
star
40

distributed-hell-wrocloverb-2012

JavaScript
1
star
41

mad-macs

...
Emacs Lisp
1
star
42

nu7hat.ch

nu7hatch's piece of web...
CSS
1
star
43

hop

Hop in the project!
Shell
1
star
44

cgoecho2

Modular version of the cgo echo example
1
star
45

go-web-services-london-go-meetup

Slides for my talk about web-services in Go at London Go Meetup
Ruby
1
star
46

rails-block-labels

Hack for using i18n powered block labels in rails3 app!
Ruby
1
star
47

i-hate-ruby-rubyconfuy-2011

Slides from the lightning talk at RubyConf Uruguay 2011
Ruby
1
star
48

rubykaigi2011

Presentation for RubyKaigi 2011
1
star
49

jagger

Jagger is your frontman!
Ruby
1
star
50

rage

Rage against the browsers!
JavaScript
1
star
51

agile-git-hooks

A set of git hooks improving work in agile teams.
Shell
1
star
52

trolley

High performence background jobs broker
Ruby
1
star
53

aur-packages

My packages submitted to AUR
1
star
54

selenium-webdriver

git clone of http://selenium.googlecode.com/svn/trunk/rb/
Ruby
1
star
55

leech

Simple TCP client/server framework with commands handling
Ruby
1
star
56

grapcha

Captcha reinvented!
Ruby
1
star
57

freebsd-dell-vostro

FreeBSD Dell Vostro 3300 Configuration...
Shell
1
star
58

webrocket-client-py

(under-development) WebRocket client library for Python
Python
1
star