Mongoid::ACL

mongoid_acl allows you to easily add access control lists to your Mongoid::Document objects. This implementation assumes you need to check acl's when loading an object, it's not efficient if you want to retrieve all the objects an actor has rights on.

Installation

To install add the following line to your gemfile

gem 'mongoid_acl'

If you're living on the edge and are using bundler 1.1, try the master branch

gem 'mongoid_acl', :hg => 'https://bitbucket.org/nielsv/mongoid_acl'

After that, remember to run “bundle install”

Usage

An example mongoid document object that on creation automatically adds permissions for the user it belongs to.

class Comment
    include Mongoid::Document
    include Mongoid::ACL
    field :text

    belongs_to :user

    set_callback(:create,:after) do |comment|
        comment.can_manage!(self.user_id)
    end
end

class User
    include Mongoid::Document

    field :name
end

A quick example using the classes above

user_a = User.create(:name => "user a",:_id => "a")
comment_1 = Comment.create(:text => 'some text',:user_id => user_a.id)

comment_1.can_read?(user_a)
>> true
comment_1.can_update?(user_a)
>> true

comment_1.can_destroy?(user_a)
>> true

user_b = User.create(:name => "user b",:_id => "b")
comment_1.can_destroy?(user_b)
>> false

comment_1.can_update?(user_b)
>> false

comment_1.can_update!(user_b)

comment_1.can_update?(user_b)
>> true

Documentation

API documentation is available on rubydoc: http://rubydoc.info/gems/mongoid_acl/

Credits

(c) 2011 Niels Vandekeybus Licensed under the apache license, version 2.0 (see LICENSE.md for details)